Answers for "Load taxonomy term by name Drupal 8"

PHP
0

Load taxonomy term by name Drupal 8

/**
   * Utility: find term by name and vid.
   *
   * @param string $name
   *   Term name.
   * @param string $vid
   *   Term vid.
   *
   * @return int
   *   Term id, or 0 if none.
   */
  public static function getTidByName($name = NULL, $vid = NULL) {
    if (empty($name) || empty($vid)) {
      return 0;
    }
    $properties = [
      'name' => $name,
      'vid' => $vid,
    ];
    $terms = \Drupal::service('entity_type.manager')->getStorage('taxonomy_term')->loadByProperties($properties);
    $term = reset($terms);
    return !empty($term) ? $term->id() : 0;
  }
Posted by: Guest on September-01-2021

Code answers related to "Load taxonomy term by name Drupal 8"

Browse Popular Code Answers by Language