You are here

function taxonomy_unique_form_taxonomy_manager_form_add_validate in Taxonomy unique 7

Validation handler for taxonomy_manager_form() 'add' button.

(Support for taxonomy_manager.module)

1 string reference to 'taxonomy_unique_form_taxonomy_manager_form_add_validate'
taxonomy_unique_form_taxonomy_manager_form_alter in ./taxonomy_unique.module
Implements hook_form_FORM_ID_alter() for taxonomy_manager_form().

File

./taxonomy_unique.module, line 99
Module file for the Taxonomy unique module.

Code

function taxonomy_unique_form_taxonomy_manager_form_add_validate($form, &$form_state) {

  // Get the needed variables from $form_state.
  if (isset($form_state['values']['add']['mass_add'])) {
    $input = $form_state['values']['add']['mass_add'];

    // taxonomy_manager_mass_add_terms() splits the string this way.
    $names = explode("\n", str_replace("\r", '', $input));
    $names = array_filter($names);
  }
  if (empty($names)) {
    return;
  }
  $vocabulary_machine_name = $form_state['values']['voc']->machine_name;
  $db_duplicates = array();
  $input_duplicates = array();
  while ($name = array_shift($names)) {
    if (in_array($name, $names, TRUE)) {
      $input_duplicates[$name] = TRUE;
    }
    elseif (!taxonomy_unique_is_term_unique($name, $vocabulary_machine_name)) {
      $db_duplicates[$name] = TRUE;
    }
  }

  // Assigning multiple errors to a form field is problematic; so for
  // simplicity we only report on $input_duplicates if there are no
  // $db_duplicates.  In practice input duplicates should be uncommon,
  // so this isn't likely to be a problem.
  if ($db_duplicates) {
    $error_message = variable_get('taxonomy_unique_' . $vocabulary_machine_name . '_message', TAXONOMY_UNIQUE_DEFAULT_MESSAGE);
    $args = array(
      '%term' => implode(', ', array_keys($db_duplicates)),
      '%vocabulary' => $vocabulary_machine_name,
    );
    form_set_error('add', filter_xss(t($error_message, $args)));
  }
  elseif ($input_duplicates) {
    $error_message = variable_get('taxonomy_unique_taxonomy_manager_add_message', TAXONOMY_UNIQUE_TAXONOMY_MANAGER_ADD_MESSAGE);
    $args = array(
      '%term' => implode(', ', array_keys($input_duplicates)),
    );
    form_set_error('add', filter_xss(t($error_message, $args)));
  }
}