17

How do I retrieve a specified taxonomy term translated (if translation exists for this term) from the current language context programmatically with D8?

1

4 Answers 4

28

Use the following code:

$curr_langcode = \Drupal::languageManager()->getCurrentLanguage(\Drupal\Core\Language\LanguageInterface::TYPE_CONTENT)->getId(); // retrieve term $taxonomy_term = \Drupal\taxonomy\Entity\Term::load($tid); // retrieve the translated taxonomy term in specified language ($curr_langcode) with fallback to default language if translation not exists $taxonomy_term_trans = \Drupal::service('entity.repository')->getTranslationFromContext($taxonomy_term, $curr_langcode); // get the value of the field "myfield" $myfield_translated = $taxonomy_term_trans->myfield->value; 
11

You should (must) use service instead at the first line for language_manager. Also I would shorten the code by using use tags.

Somewhere in the beginning of file:

use Drupal\taxonomy\Entity\Term; use Drupal\Core\Language\LanguageInterface; 

and later in the code in some function

$curr_langcode = \Drupal::service('language_manager')->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId(); // Retrieve term. $taxonomy_term = Term::load($tid); // Retrieve the translated taxonomy term in specified language // ($curr_langcode) with fallback to default language if translation not // exists. $taxonomy_term_trans = \Drupal::service('entity.repository')->getTranslationFromContext($taxonomy_term, $curr_langcode); // Get the value of the field "myfield". $myfield_translated = $taxonomy_term_trans->myfield->value; 
4

The snippets above will return untranslated terms also. You must check if a term is translated with the hasTranslation function:

$vocabulary = 'MY_VOCABULARY_NAME'; $language = \Drupal::languageManager()->getCurrentLanguage()->getId(); $query = \Drupal::entityQuery('taxonomy_term'); $query->condition('vid', $vocabulary); $query->sort('weight'); $tids = $query->execute(); $terms = \Drupal\taxonomy\Entity\Term::loadMultiple($tids); $termList = array(); foreach($terms as $term) { if($term->hasTranslation($language)){ $translated_term = \Drupal::service('entity.repository')->getTranslationFromContext($term, $language); $tid = $term->id(); $termList[$tid] = $translated_term->getName(); } } // To print a list of translated terms. foreach($termList as $tid => $name) { print $name; } 

To link the tags to their term page: See: Get taxonomy terms

2

An approach in preprocess node (handles multiple term references):

function HOOK_preprocess_node(&$variables) { $node = $variables['node']; $langcode = \Drupal::languageManager()->getCurrentLanguage()->getId(); $pathAliasManager = \Drupal::service('path_alias.manager'); if ($node->hasField("field_name") && !$node->get("field_name")->isEmpty()) { $terms = $node->get("field_name")->referencedEntities(); foreach ($terms as $term) { $name = $term->name->value; if ($term->hasTranslation($langcode)) { $name = $term->getTranslation($langcode)->name->value; } $variables['termValues'][] = [ 'pathAlias' => $pathAliasManager->getAliasByPath('/taxonomy/term/' . $term->id()), 'name' => $name, ]; } } } 

Then in Twig:

{% for items in termValues %} <a href="{{ items.pathAlias }}"> {{ items.name }} </a> {% endfor %} 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.