You are here

function synonyms_autocomplete_taxonomy_term_validate in Synonyms 7

Form element validate handler.

Handle validation for taxonomy term synonym-friendly autocomplete element.

1 string reference to 'synonyms_autocomplete_taxonomy_term_validate'
synonyms_field_widget_form in ./synonyms.module
Implements hook_field_widget_form().

File

./synonyms.module, line 435
Provide synonyms feature for Drupal entities.

Code

function synonyms_autocomplete_taxonomy_term_validate($element, &$form_state) {

  // After taxonomy_autocomplete_validate() has finished its job any terms it
  // didn't find have been set for autocreation. We need to:
  // (a) Double-check that those terms are not synonyms.
  // (b) Check that synonyms' configurable auto-creation option is enabled.
  $value = drupal_array_get_nested_value($form_state['values'], $element['#parents']);
  $tids = array();
  $field = field_widget_field($element, $form_state);
  foreach ($value as $delta => $term) {
    if ($term['tid'] == 'autocreate') {
      $synonym_tid = 0;
      foreach ($field['settings']['allowed_values'] as $tree) {
        $behavior_implementations = synonyms_behavior_get('autocomplete', 'taxonomy_term', $tree['vocabulary'], TRUE);
        foreach ($behavior_implementations as $behavior_implementation) {
          $synonyms = $behavior_implementation['object']
            ->synonymsFind(db_and()
            ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $term['name']));
          foreach ($synonyms as $synonym) {
            $synonym_tid = $synonym->entity_id;
            break 2;
          }
        }
      }
      if ($synonym_tid != 0 && !in_array($synonym_tid, $tids)) {
        $value[$delta]['tid'] = $synonym_tid;
        $tids[] = $synonym_tid;
      }
      elseif (!$element['#auto_creation']) {
        unset($value[$delta]);
      }
    }
    else {
      $tids[] = $term['tid'];
    }
  }
  $value = array_values($value);
  form_set_value($element, $value, $form_state);
}