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 ?? Jun 21 '16 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
    – Yuseferi
    Jun 21 '16 at 7:39
  • Are you using any module for translation?? Jun 21 '16 at 8:00
  • @ShreyaShetty drupal8 build-in support multi language.
    – Yuseferi
    Jun 21 '16 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?
    – Eyal
    Jun 21 '16 at 19:51
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
0

Similar to Stef Van Looveren's answer but instead of loading all tids and then checking if each one has a specific translation we can include the language condition in the entity query. (Using Drupal 9)

$vocabulary = 'MY_VOCABULARY_NAME';
$language =  \Drupal::languageManager()->getCurrentLanguage()->getId();
$tids = \Drupal::entityQuery('taxonomy_term');
  ->condition('vid', $vocabulary);
  ->condition('language', $language);
  ->sort('weight');
  ->execute();
$terms = \Drupal\taxonomy\Entity\Term::loadMultiple($tids);
$termList = array();

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 translation $language while using loadByProperties() only returns the entity if the original language is $language

New contributor
mhawwari is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.