You are here

function taxonomy_unique_term_name_validate in Taxonomy unique 8

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

Implements _form_validate() for taxonomy_form_term().

1 string reference to 'taxonomy_unique_term_name_validate'
taxonomy_unique_form_taxonomy_term_form_alter in ./taxonomy_unique.module

File

./taxonomy_unique.module, line 53

Code

function taxonomy_unique_term_name_validate($form, \Drupal\Core\Form\FormStateInterface $form_state) {
  $triggering_element = $form_state
    ->getTriggeringElement();

  // If we don't want to save, don't validate the term name.
  if ($triggering_element['#button_type'] != 'primary') {
    return;
  }

  // Get the needed variables from $form_state.
  $name = $form_state
    ->getValue('name');
  $name = $name[0]['value'];
  $vocabulary_machine_name = $form_state
    ->getFormObject()
    ->getEntity()
    ->getVocabularyId();
  $tid = $form_state
    ->getFormObject()
    ->getEntity()
    ->id();

  // If the name isn't empty and unique check failed, mark field as invalid.
  if ($name != '' && !taxonomy_unique_is_term_unique($name, $vocabulary_machine_name, $tid)) {
    $error_message = \Drupal::config('taxonomy_unique.settings')
      ->get($form_state
      ->getFormObject()
      ->getEntity()
      ->getVocabularyId() . '_message');
    if (empty($error_message)) {
      $error_message = TAXONOMY_UNIQUE_DEFAULT_MESSAGE;
    }
    $form_state
      ->setErrorByName('name', \Drupal\Component\Utility\Xss::filter(\Drupal\Component\Utility\SafeMarkup::format($error_message, array(
      '@term' => $name,
      '@vocabulary' => $vocabulary_machine_name,
    ))));
  }
}