You are here

function taxonomy_term_machine_name_load in Taxonomy Machine Name 8

Same name and namespace in other branches
  1. 7 taxonomy_machine_name.module \taxonomy_term_machine_name_load()

Try to map a string to an existing term, as for glossary use.

Provides a case-insensitive and trimmed mapping, to maximize the likelihood of a successful match.

Parameters

string $machine_name: Name of the term to search for.

string $vocabulary: Vocabulary machine name to limit the search. Defaults to NULL.

\Drupal\Core\Form\FormStateInterface $form_state: Form state.

Return value

\Drupal\taxonomy\TermInterface|null Taxonomy term object or NULL.

1 call to taxonomy_term_machine_name_load()
taxonomy_machine_name_uniquify in ./taxonomy_machine_name.module
Check and alter machine name to generate a unique value.
1 string reference to 'taxonomy_term_machine_name_load'
taxonomy_machine_name_form_taxonomy_term_form_alter in ./taxonomy_machine_name.module
Implements hook_form_FORM_ID_alter().

File

./taxonomy_machine_name.module, line 144
This is the Taxonomy Machine Name module.

Code

function taxonomy_term_machine_name_load($machine_name, $vocabulary, FormStateInterface $form_state = NULL) {

  // Support for machine_name form callback.
  if (NULL !== $form_state) {
    $buildInfo = $form_state
      ->getBuildInfo();

    /** @var \Drupal\taxonomy\TermForm $callbackObject */
    $callbackObject = $buildInfo['callback_object'];

    /** @var \Drupal\taxonomy\Entity\Term $term */
    $term = $callbackObject
      ->getEntity();
    $vocabulary = $term
      ->bundle();
  }
  $conditions = [
    'machine_name' => $machine_name,
    'vid' => $vocabulary,
  ];
  if ($terms = \Drupal::entityTypeManager()
    ->getStorage('taxonomy_term')
    ->loadByProperties($conditions)) {
    if (isset($term) && key($terms) == $term
      ->id()) {
      return NULL;
    }
    return reset($terms);
  }
  return NULL;
}