You are here

function _taxonomy_access_autocomplete_alter in Taxonomy Access Control 7

Implements the create grant for autocomplete fields.

  • Denies access if the user cannot alter the field values.
  • Determines whether the user can autocreate new terms for the field.
  • Removes default values disallowed by create.
  • Adds information on autocreate and disallowed defaults to the element so it is available to the validator.
  • Adds the custom validator.
  • Sets a custom autocomplete path to filter autocomplete by create.

Some of the logic here is borrowed from taxonomy_autocomplete_validate().

See also

taxonomy_access_field_widget_taxonomy_autocomplete_form_alter()

Related topics

1 call to _taxonomy_access_autocomplete_alter()
taxonomy_access_field_widget_taxonomy_autocomplete_form_alter in ./taxonomy_access.module
Implements hook_field_widget_WIDGET_TYPE_form_alter().

File

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

Code

function _taxonomy_access_autocomplete_alter(&$element, &$form_state, $context) {

  // Collect a list of terms and filter out those disallowed by create.
  $filtered = array();
  foreach ($context['items'] as $item) {
    $filtered[$item['tid']] = $item;
  }
  $disallowed_defaults = taxonomy_access_create_disallowed(array_keys($filtered));
  foreach ($disallowed_defaults as $tid) {
    unset($filtered[$tid]);
  }

  // Assemble a list of all vocabularies for the field.
  $vids = array();
  foreach ($context['field']['settings']['allowed_values'] as $tree) {
    if ($vocab = taxonomy_vocabulary_machine_name_load($tree['vocabulary'])) {
      $vids[] = $vocab->vid;
    }
  }

  // Determine whether the user has create for any terms in the given vocabs.
  $allowed_terms = FALSE;
  foreach ($vids as $vid) {
    $terms = taxonomy_access_user_create_terms_by_vocab($vid);
    if (!empty($terms)) {
      $allowed_terms = TRUE;
      break;
    }
  }

  // Filter the vids to vocabs in which the user may create new terms.
  $allowed_vids = taxonomy_access_create_default_allowed($vids);

  // If the field already has the maximum number of values, and all of these
  // values are disallowed, deny access to the field.
  if ($context['field']['cardinality'] != FIELD_CARDINALITY_UNLIMITED) {
    if (sizeof($disallowed_defaults) >= $context['field']['cardinality']) {
      $element['#access'] = FALSE;
    }
  }

  // If the user may not create any terms on this field, deny access.
  if (empty($allowed_vids) && !$allowed_terms) {
    $element['#access'] = FALSE;
  }

  // Set the default value from the filtered item list.
  $element['#default_value'] = taxonomy_access_autocomplete_default_value($filtered);

  // Custom validation.  Set values for the validator indicating:
  // 1. Whether the user can autocreate terms in this field (vocab. default).
  // 2. Which tids were removed due to create restrictions.
  $element['#allow_autocreate'] = empty($allowed_vids) ? FALSE : TRUE;
  $element['#disallowed_defaults'] = $disallowed_defaults;
  $element['#element_validate'] = array(
    'taxonomy_access_autocomplete_validate',
  );

  // Use a custom autocomplete path to filter by create rather than list.
  $element['#autocomplete_path'] = 'taxonomy_access/autocomplete/' . $context['field']['field_name'];
  unset($context);
}