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?
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;
}
?>
You can use snippet code like by using entityTypeManager :
$term_name = 'Term Name';
$term = \Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->loadByProperties(['name' => $term_name]);
'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'
.
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();
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.
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();