29

In Drupal 7, we can load the term using name for ex. taxonomy_get_term_by_name($name)

Is there any way to load term via given name in Drupal 8?

0

5 Answers 5

29

This functionality appears to be deprecated in Drupal 8.
Use the taxonomy_term_load_multiple_by_name function instead.

Example

<?php

  /**
   * Utility: find term by name and vid.
   * @param null $name
   *  Term name
   * @param null $vid
   *  Term vid
   * @return int
   *  Term id or 0 if none.
   */
  protected function getTidByName($name = NULL, $vid = NULL) {
    $properties = [];
    if (!empty($name)) {
      $properties['name'] = $name;
    }
    if (!empty($vid)) {
      $properties['vid'] = $vid;
    }
    $terms = \Drupal::entityManager()->getStorage('taxonomy_term')->loadByProperties($properties);
    $term = reset($terms);

    return !empty($term) ? $term->id() : 0;
  }

?>
2
  • Linked blog post doesn't seem to load anymore. Found this one that may be helpful for others trying to figure this out. btmash.com/article/2016-04-26/…
    – gcalex5
    Commented Dec 5, 2017 at 13:44
  • From performance perspective, which is more effective - \Drupal::entityManager() or taxonomy_term_load_multiple_by_name?
    – NKP
    Commented Jan 4, 2021 at 6:56
39

You can use snippet code like by using entityTypeManager :

$term_name = 'Term Name';
$term = \Drupal::entityTypeManager()
      ->getStorage('taxonomy_term')
      ->loadByProperties(['name' => $term_name]);
1
  • 2
    Maybe also pass the 'vid' vocabulary ID along like in the answer by @RenukaKulkarni below. Imagine having multiple vocabularies and every vocabulary has a term with the $term_name = 'Term Name'.
    – leymannx
    Commented Mar 4, 2020 at 13:34
9

To load a single term ID by term name and vocabulary in Drupal 8 you can use the following snippet.

$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')
  ->loadByProperties(['name' => $term_name, 'vid' => 'job_category']);
$term = reset($term);
$term_id = $term->id();
1
  • I just needed the term ID as well and just used $term_tid = array_keys($term)[0];
    – scotself
    Commented Jun 17, 2021 at 22:47
2

As per Renamed the taxonomy functions that returned multiple values, taxonomy_get_term_by_name($name, $vocabulary = NULL) has been renamed taxonomy_term_load_multiple_by_name($name, $vocabulary = NULL). If you look at the code of the first function and you compare it with the code of the second function, you will notice that the most relevant difference is having replaced the call to taxonomy_term_load_multiple(array(), $conditions) with the call to entity_load_multiple_by_properties('taxonomy_term', $values).

// Drupal 7
function taxonomy_get_term_by_name($name, $vocabulary = NULL) {
  $conditions = array('name' => trim($name));
  if (isset($vocabulary)) {
    $vocabularies = taxonomy_vocabulary_get_names();
    if (isset($vocabularies[$vocabulary])) {
      $conditions['vid'] = $vocabularies[$vocabulary]->vid;
    }
    else {
      // Return an empty array when filtering by a non-existing vocabulary.
      return array();
    }
  }
  return taxonomy_term_load_multiple(array(), $conditions);
}
// Drupal 8
function taxonomy_term_load_multiple_by_name($name, $vocabulary = NULL) {
  $values = array('name' => trim($name));
  if (isset($vocabulary)) {
    $vocabularies = taxonomy_vocabulary_get_names();
    if (isset($vocabularies[$vocabulary])) {
      $values['vid'] = $vocabulary;
    }
    else {
      // Return an empty array when filtering by a non-existing vocabulary.
      return array();
    }
  }
  return entity_load_multiple_by_properties('taxonomy_term', $values);
}

Since taxonomy_term_load_multiple_by_name() has not been marked as deprecated, you can still use that function where you used to use taxonomy_get_term_by_name(). They both require the same arguments, so converting code for Drupal 7 in code for Drupal 8, in this case, is just matter of replacing the function name.

1

You can also use entity field queries to load by fields on the term

$result = \Drupal::entityQuery('taxonomy_term')
          ->condition('field_my_field_name', 'Whatever Value')
          ->execute();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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