Skip to main content
added 167 characters in body
Source Link

I've just wrote a function, feel free to edit and use :) I needed the id of the term, but you can return whatever you want.

function checkTaxonomyTerm($vocab_name, $term_name){ $query = \Drupal::entityQuery('taxonomy_term'); $query->condition('vid', $vocab_name); $tids = $query->execute(); $terms = Term::loadMultiple($tids); foreach($terms as $term) { $name = $term->getName(); $query->conditionif('vid',$name $vocab_name== $term_name) { print_r($term->id());   if ($nameis_null($term->id())) =={  $term_name) return null; } else{   return array(true, $term->id()); } } else {return null;addTaxonomyTerm($term->getVocabularyId(), $name);} } } 

I've just wrote a function, feel free to edit and use :) I needed the id of the term, but you can return whatever you want.

function checkTaxonomyTerm($vocab_name, $term_name){ $query = \Drupal::entityQuery('taxonomy_term'); $tids = $query->execute(); $terms = Term::loadMultiple($tids); foreach($terms as $term) { $name = $term->getName(); $query->condition('vid', $vocab_name); if($name == $term_name){ return $term->id(); } else return null; } } 

I've just wrote a function, feel free to edit and use :) I needed the id of the term, but you can return whatever you want.

function checkTaxonomyTerm($vocab_name, $term_name){ $query = \Drupal::entityQuery('taxonomy_term'); $query->condition('vid', $vocab_name); $tids = $query->execute(); $terms = Term::loadMultiple($tids); foreach($terms as $term) { $name = $term->getName(); if($name == $term_name) { print_r($term->id());   if (is_null($term->id())) {   return null; } else{   return array(true, $term->id()); } } else {return addTaxonomyTerm($term->getVocabularyId(), $name);} } } 
Source Link

I've just wrote a function, feel free to edit and use :) I needed the id of the term, but you can return whatever you want.

function checkTaxonomyTerm($vocab_name, $term_name){ $query = \Drupal::entityQuery('taxonomy_term'); $tids = $query->execute(); $terms = Term::loadMultiple($tids); foreach($terms as $term) { $name = $term->getName(); $query->condition('vid', $vocab_name); if($name == $term_name){ return $term->id(); } else return null; } }