Created
November 29, 2017 10:16
-
-
Save devudit/bdfadb6784a584c58e2e637699b2cd32 to your computer and use it in GitHub Desktop.
Find tid by name in drupal 8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment