19

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?

17

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;
  }

?>
30

You can use snippet code like by using entityTypeManager :

$term_name = 'Term Name';
$term = \Drupal::entityTypeManager()
      ->getStorage('taxonomy_term')
      ->loadByProperties(['name' => $term_name]);
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.

0

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, privacy policy and cookie policy

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