View source  
  <?php
define('TAXONOMY_UNIQUE_DEFAULT_MESSAGE', 'Term %term already exists in vocabulary %vocabulary.');
define('TAXONOMY_UNIQUE_TAXONOMY_MANAGER_ADD_MESSAGE', 'Term %term listed more than once in the input');
function taxonomy_unique_form_taxonomy_form_vocabulary_alter(&$form, &$form_state, $form_id) {
  $form['unique_container'] = array(
    '#type' => 'fieldset',
    '#title' => t('Taxonomy unique'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['unique_container']['unique'] = array(
    '#type' => 'checkbox',
    '#title' => t('Terms should be unique.'),
    '#default_value' => variable_get('taxonomy_unique_' . $form['#vocabulary']->machine_name, ''),
  );
  $form['unique_container']['unique_message'] = array(
    '#type' => 'textfield',
    '#title' => t('Message to show if term already exists'),
    '#description' => t('Placeholders: %term and %vocabulary'),
    '#default_value' => variable_get('taxonomy_unique_' . $form['#vocabulary']->machine_name . '_message', TAXONOMY_UNIQUE_DEFAULT_MESSAGE),
  );
  
  $form['#submit'][] = 'taxonomy_unique_taxonomy_form_vocabulary_submit';
}
function taxonomy_unique_taxonomy_form_vocabulary_submit($form, &$form_state) {
  
  variable_set('taxonomy_unique_' . $form_state['values']['machine_name'], $form_state['values']['unique']);
  variable_set('taxonomy_unique_' . $form_state['values']['machine_name'] . '_message', $form_state['values']['unique_message']);
}
function taxonomy_unique_form_taxonomy_form_term_alter(&$form, &$form_state, $form_id) {
  
  if (variable_get('taxonomy_unique_' . $form['vocabulary_machine_name']['#value'], FALSE)) {
    $form['#validate'][] = 'taxonomy_unique_term_name_validate';
  }
}
function taxonomy_unique_term_name_validate($form, &$form_state) {
  
  if ($form_state['values']['op'] != t('Save')) {
    return;
  }
  
  $name = $form_state['values']['name'];
  $vocabulary_machine_name = $form_state['values']['vocabulary_machine_name'];
  $tid = !empty($form_state['values']['tid']) ? $form_state['values']['tid'] : NULL;
  
  if ($name != '' && !taxonomy_unique_is_term_unique($name, $vocabulary_machine_name, $tid)) {
    $error_message = variable_get('taxonomy_unique_' . $vocabulary_machine_name . '_message', TAXONOMY_UNIQUE_DEFAULT_MESSAGE);
    form_set_error('name', filter_xss(format_string($error_message, array(
      '%term' => $name,
      '%vocabulary' => $vocabulary_machine_name,
    ))));
  }
}
function taxonomy_unique_form_taxonomy_manager_form_alter(&$form, &$form_state, $form_id) {
  $vocabulary_machine_name = $form['voc']['#value']->machine_name;
  if (variable_get('taxonomy_unique_' . $vocabulary_machine_name, FALSE)) {
    $form['add']['add']['#validate'] = array(
      'taxonomy_unique_form_taxonomy_manager_form_add_validate',
    );
  }
}
function taxonomy_unique_form_taxonomy_manager_form_add_validate($form, &$form_state) {
  
  if (isset($form_state['values']['add']['mass_add'])) {
    $input = $form_state['values']['add']['mass_add'];
    
    $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;
    }
  }
  
  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)));
  }
}
function taxonomy_unique_is_term_unique($term_name, $vocabulary_machine_name, $tid = NULL) {
  $terms = taxonomy_get_term_by_name($term_name, $vocabulary_machine_name);
  $term = current($terms);
  
  if (empty($terms) || count($terms) == 1 && $term->tid == $tid) {
    return TRUE;
  }
  return FALSE;
}