protected function getTerm($term_name, $vocabulary_id = 'tags') {
    $term_name = trim($term_name);
    $term_storage = $this->entityTypeManager->getStorage('taxonomy_term');
    $terms = $term_storage->loadByProperties([
      'name' => $term_name,
      'vid' => $vocabulary_id,
    ]);
    if (!$terms) {
      $term = $term_storage->create([
        'name' => $term_name,
        'vid' => $vocabulary_id,
        'path' => ['alias' => '/' . Html::getClass($vocabulary_id) . '/' . Html::getClass($term_name)],
      ]);
      $term->save();
      $this->storeCreatedContentUuids([$term->uuid() => 'taxonomy_term']);
      return $term->id();
    }
    $term = reset($terms);
    return $term->id();
  }
protected function getTerm($term_name, $vocabulary_id = 'tags') {
    $term_name = trim($term_name);
    $term_storage = $this->entityTypeManager->getStorage('taxonomy_term');
    $terms = $term_storage->loadByProperties([
      'name' => $term_name,
      'vid' => $vocabulary_id,
    ]);
    if (!$terms) {
      $term = $term_storage->create([
        'name' => $term_name,
        'vid' => $vocabulary_id,
        'path' => ['alias' => '/' . Html::getClass($vocabulary_id) . '/' . Html::getClass($term_name)],
      ]);
      $term->save();
      $this->storeCreatedContentUuids([$term->uuid() => 'taxonomy_term']);
      return $term->id();
    }
    $term = reset($terms);
    return $term->id();
  }
protected function getTerm ( $ term_name , $ vocabulary_id = 'tags' ) { $ term_name = trim ( $ term_name ) ; $ term_storage = $ this -> entityTypeManager -> getStorage ( 'taxonomy_term' ) ; $ terms = $ term_storage -> loadByProperties ( [ 'name' => $ term_name , 'vid' => $ vocabulary_id , ] ) ; if ( ! $ terms ) { $ term = $ term_storage -> create ( [ 'name' => $ term_name , 'vid' => $ vocabulary_id , 'path' => [ 'alias' => '/' . Html :: getClass ( $ vocabulary_id ) . '/' . Html :: getClass ( $ term_name ) ] , ] ) ; $ term -> save ( ) ; $ this -> storeCreatedContentUuids ( [ $ term -> uuid ( ) => 'taxonomy_term' ] ) ; return $ term -> id ( ) ; } $ term = reset ( $ terms ) ; return $ term -> id ( ) ; }
Looks up a term by name, if it is missing the term is created.

@param string $term_name
Term name.
@param string $vocabulary_id
Vocabulary ID.

@return int
Term ID.
Looks up a term by name if it is missing the term is created .