Skip to main content
improved formatting
Source Link
avpaderno
  • 98.1k
  • 15
  • 165
  • 284

SimilarThis is similar to Stef Van Looveren's answer, but instead of loading all tidsthe taxonomy term IDs and then checking if each one has a specific translation we can, I 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('langcode', $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(); } 
$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 translation $language translation, while using loadByProperties() only returns the entity if the original language is $language.

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('langcode', $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

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.

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''langcode', $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

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

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('langcode', $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

Source Link

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