5

I know I can get list of of taxonomy of a vocabulary with

 $terms = \Drupal::entityManager()->getStorage('taxonomy_term')->loadTree('course_category'); kint($terms); 

but it returned all that vocabulary terms, I am working on multi language site,I want retrieve terms of specified language.

my Drupal version is 8.1.3

6
  • did u pass vid of a particular vocabulary term u wanted to retrieve ?? Commented Jun 21, 2016 at 7:38
  • yes ,but it return all of terms,` $terms = \Drupal::entityManager()->getStorage('taxonomy_term')->loadTree('course_category'); ` . I just limit to specified language Commented Jun 21, 2016 at 7:39
  • Are you using any module for translation?? Commented Jun 21, 2016 at 8:00
  • @ShreyaShetty drupal8 build-in support multi language. Commented Jun 21, 2016 at 8:39
  • Do you care about the tree structure provided by the loadTree method, or do you just want to load all the terms of said language? Commented Jun 21, 2016 at 19:51

3 Answers 3

3

I had some difficulties with printing tags in the current language in code too. This is what worked to get ONLY the translated terms of a vocabulary:

$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)){ $tid = $term->id(); $translated_term = \Drupal::service('entity.repository')->getTranslationFromContext($term, $language); $termList[$tid] = $translated_term->getName(); } } // To print a list of translated terms. foreach($termList as $tid => $name) { print $name; } 

To link the tags, see https://drupal.stackexchange.com/a/243160/71941

3

You can use the loadByProperties method to load all the entities that their fields correspond to certain values.

For a more advance selection you can use the entity query service.

\Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties([ 'vid' => $vid, 'langcode' => $language_id ]); 
0
1

This is similar to Stef Van Looveren's answer, but instead of loading all the taxonomy term IDs and then checking if each one has a specific translation, I include the language condition in the query.

$vocabulary = 'MY_VOCABULARY_NAME'; $language = \Drupal::languageManager()->getCurrentLanguage()->getId(); $tids = \Drupal::entityQuery('taxonomy_term') ->condition('vid', $vocabulary) ->condition('langcode', $language) ->sort('weight') ->execute(); $terms = \Drupal\taxonomy\Entity\Term::loadMultiple($tids); $termList = []; foreach ($terms as $term) { $tid = $term->id(); $translated_term = \Drupal::service('entity.repository')->getTranslationFromContext($term, $language); $termList[$tid] = $translated_term->getName(); } 

This would return any term that has the $language translation, while loadByProperties() only returns the entity if the original language is $language.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.