Drupal 8

Order taxonomy from name

Submitted by root on Tue, 11/23/2021 - 20:20
Code
SELECT tid, langcode, name, SUBSTRING_INDEX(name, ' ', 1), SUBSTRING_INDEX(TRIM (TRAILING '.' FROM SUBSTRING_INDEX(name, ' ', 1)), '.', -1) pisu FROM taxonomy_term_field_data WHERE vid = 'sailkapen' UPDATE taxonomy_term_field_data SET weight = SUBSTRING_INDEX(TRIM (TRAILING '.' FROM SUBSTRING_INDEX(name, ' ', 1)), '.', -1) WHERE vid = 'sailkapen'

Change language switcher links with langcodes

Submitted by root on Thu, 11/26/2020 - 16:54
Code
function CUSTOM_THEME_NAME_preprocess_links__language_block(&$variables) { if( isset($variables['links']['en']) ) { $variables['links']['en']['link']['#title'] = 'EN'; } $variables['links']['es']['link']['#title'] = 'ES'; $variables['links']['eu']['link']['#title'] = 'EU'; if( isset($variables['links']['fr']) ) { $variables['links']['fr']['link']['#title'] = 'FR'; } }

Delete all terms

Submitted by root on Thu, 11/19/2020 - 07:28
Code
<?php $tids = \Drupal::entityQuery('taxonomy_term')->condition('vid', 'mycustomvocab')->execute(); $controller = \Drupal::entityTypeManager()->getStorage('taxonomy_term'); $entities = $controller->loadMultiple($tids); $controller->delete($entities); ?>

Save node terms

Submitted by root on Thu, 11/05/2020 - 17:08
Code
<?php $term_names = ['Bat', 'Bi']; $node_terms = []; foreach ($term_names as $name) { $tids = \Drupal::entityQuery('taxonomy_term')->condition('vid', 'vocabulary_id')->condition('name', $name)->execute(); if(!$tids) { $term = \Drupal\taxonomy\Entity\Term::create([ 'vid' => 'vocabulary_id', 'name' => $name, ]); $term->save(); $node_terms[] = ['target_id' => $term->id()]; } else { $node_terms[] = ['target_id' => array_shift($tids)]; } } $node->field_field_name->setValue($node_terms); ?>

Get translated taxonomy

Submitted by root on Thu, 11/05/2020 - 16:31
Code
<?php $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree('vid', 0, NULL, TRUE); $current_langcode = \Drupal::languageManager()->getCurrentLanguage()->getId(); foreach ($terms as &$term) { if ($term->hasTranslation($current_langcode)) { $term = $term->getTranslation($current_langcode); } } ?>

Import node and translate

Submitted by root on Tue, 10/27/2020 - 12:13
Code
<?php $bundle = 'story'; $default_langcode = 'eu'; $translation_langcode = 'es'; $vid = 'tags'; $uid = 1; $taxonomy_terms = self::getAllTerms($vid); $author = \Drupal::entityTypeManager()->getStorage('user')->load($uid); $taxonomy_term = $sailkapen_terms[$tid] ?? NULL; // check if exists $nodes = \Drupal::entityTypeManager() ->getListBuilder('node') ->getStorage() ->loadByProperties([ 'field_[key]' => $key, ]); // create or load node if($nodes) { $node = array_shift($nodes); } else { $values = array( // 'nid' => $nid, 'langcode' => $default_langcode, 'type' => $bundle, 'uid' => $uid, 'status' => 1, 'promote' => 0, 'title' => $title, 'field_signatura' => $signatura, ); $node = \Drupal\node\Entity\Node::create($values); $node->save(); } // create or load translation if( !$node->hasTranslation($translation_langcode) ) { $node_tr = $node->addTranslation($translation_langcode); $node_tr->uid = $uid; } else { $node_tr = $node->getTranslation($translation_langcode); } $node_tr->body->format = 'basic_html'; $node_tr->body->value = $pp[2]; $node_tr->title = self::getTitleFromDescription($pp[2]); $node_tr->setOwnerId($author); $node_tr->save(); if(!empty($body_content)) { $node->body = array( 'summary' => NULL, 'value' => $body_content, 'format' => 'basic_html' // 'full_html'... ); } // set plain text value if( !empty($field_value) ) { $node->field_hasiera->setValue( $field_value ); } // set entity reference value if(!empty($sailkapen)) { $node->field_field_key->setValue(['target_id' => $tid]); } $node->field_raw->setValue($r); $node->save(); ?>