You are here

function icon_select_taxonomy_form_validate in Icon Select 8

Form validation for taxonomy term save.

1 string reference to 'icon_select_taxonomy_form_validate'
icon_select_form_taxonomy_term_icons_form_alter in ./icon_select.module
Implements hook_form_FORM_ID_alter().

File

./icon_select.module, line 114
Basic module file for icon_select module.

Code

function icon_select_taxonomy_form_validate(&$form, FormStateInterface $form_state) {
  $vocabulary_id = $form_state
    ->getFormObject()
    ->getEntity()
    ->get('vid')->target_id;

  /** @var \Drupal\taxonomy\TermStorageInterface $term_storage */
  $term_storage = \Drupal::entityTypeManager()
    ->getStorage('taxonomy_term');
  $terms = $term_storage
    ->loadTree($vocabulary_id, 0, NULL, TRUE);
  $symbol_id = $form_state
    ->getValue('field_symbol_id')[0]['value'];

  // Check for unique class name.
  foreach ($terms as $term) {
    if ($term->field_symbol_id->value == $symbol_id) {

      // Skip current term.
      if (!empty($form_state
        ->getFormObject()
        ->getEntity())) {
        if ($term
          ->id() == $form_state
          ->getFormObject()
          ->getEntity()
          ->id()) {
          continue;
        }
      }
      $form_state
        ->setErrorByName('field_symbol_id', t('Symbol ID must be unique'));
      break;
    }
  }
}