You are here

function _taxonomy_access_autocomplete_validate in Taxonomy Access Control 7

Validates taxonomy autocomplete values for create grants.

For now we essentially duplicate the code from taxonomy.module, because it calls form_set_value() without providing the logic separately.

We use two properties set in hook_field_widget_form_alter():

  • $element['#allow_autocreate']
  • $element['#disallowed_defaults']

@todo Specify autocreate per vocabulary?

See also

taxonomy_autocomplete_validate()

taxonomy_access_autocomplete()

taxonomy_access_field_widget_taxonomy_autocomplete_form_alter()

Related topics

1 call to _taxonomy_access_autocomplete_validate()
taxonomy_access_autocomplete_validate in ./taxonomy_access.module
Form element validation handler for taxonomy autocomplete fields.

File

./taxonomy_access.create.inc, line 473
Implements the Add Tag (create) grant on editing forms.

Code

function _taxonomy_access_autocomplete_validate($element, &$form_state) {

  // Autocomplete widgets do not send their tids in the form, so we must detect
  // them here and process them independently.
  $value = array();
  if ($tags = $element['#value']) {

    // Collect candidate vocabularies.
    $field = field_widget_field($element, $form_state);
    $vocabularies = array();
    foreach ($field['settings']['allowed_values'] as $tree) {
      if ($vocabulary = taxonomy_vocabulary_machine_name_load($tree['vocabulary'])) {
        $vocabularies[$vocabulary->vid] = $vocabulary;
      }
    }

    // Translate term names into actual terms.
    $typed_terms = drupal_explode_tags($tags);
    foreach ($typed_terms as $typed_term) {

      // See if the term exists in the chosen vocabulary and return the tid;
      // otherwise, create a new 'autocreate' term for insert/update.
      if ($possibilities = taxonomy_term_load_multiple(array(), array(
        'name' => trim($typed_term),
        'vid' => array_keys($vocabularies),
      ))) {
        $term = array_pop($possibilities);
      }
      elseif ($element['#allow_autocreate']) {
        $vocabulary = reset($vocabularies);
        $term = array(
          'tid' => 'autocreate',
          'vid' => $vocabulary->vid,
          'name' => $typed_term,
          'vocabulary_machine_name' => $vocabulary->machine_name,
        );
      }
      else {
        form_error($element, t('You may not create new tags in %name.', array(
          '%name' => t($element['#title']),
        )));
      }
      if ($term) {
        $value[] = (array) $term;
      }
    }
  }

  // Add in the terms that were disallowed.
  // taxonomy.module expects arrays, not objects.
  $disallowed = taxonomy_term_load_multiple($element['#disallowed_defaults']);
  foreach ($disallowed as $key => $term) {
    $disallowed[$key] = (array) $term;
  }
  $value = array_merge($value, $disallowed);

  // Subsequent validation will be handled by hook_field_attach_validate().
  // Set the value in the form.
  form_set_value($element, $value, $form_state);
}