You are here

function taxonomy_unique_is_term_unique in Taxonomy unique 8

Same name and namespace in other branches
  1. 7 taxonomy_unique.module \taxonomy_unique_is_term_unique()

Checks if term name already exists in vocabulary.

Parameters

string $term_name: Name to check

string $vocabulary_machine_name: Machine name of the vocabulary the term belongs to

int $tid: The term ID when updating an existing term

Return value

bool TRUE when term name is unique, FALSE if not

1 call to taxonomy_unique_is_term_unique()
taxonomy_unique_term_name_validate in ./taxonomy_unique.module
Implements _form_validate() for taxonomy_form_term().

File

./taxonomy_unique.module, line 93

Code

function taxonomy_unique_is_term_unique($term_name, $vocabulary_machine_name, $tid = NULL) {
  $terms = \Drupal::entityTypeManager()
    ->getStorage('taxonomy_term')
    ->loadByProperties([
    'name' => $term_name,
    'vid' => $vocabulary_machine_name,
  ]);
  $term = current($terms);

  // If no terms are found, or only one term is found which has
  // the same tid as the one we're trying to save, the name must be unique.
  if (empty($terms) || count($terms) == 1 && $term
    ->id() == $tid) {
    return TRUE;
  }
  return FALSE;
}