You are here

function _taxonomy_access_options_alter in Taxonomy Access Control 7

Implements the create grant for options widgets.

  • Denies access if the user cannot alter the field values.
  • Attaches jQuery to disable values disallowed by create.
  • Adds the disallowed values from the element so they are available to the custom validator.
  • Adds the custom validator.

We use jQuery to disable the options because of FAPI limitations:

See also

http://drupal.org/node/284917

http://drupal.org/node/342316

http://drupal.org/node/12089

taxonomy_access_field_widget_form_alter()

Related topics

1 call to _taxonomy_access_options_alter()
taxonomy_access_field_widget_form_alter in ./taxonomy_access.module
Implements hook_field_widget_form_alter().

File

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

Code

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

  // Check for an existing entity ID.
  $entity_id = 0;
  if (!empty($form_state['build_info']['args'][0])) {
    $info = entity_get_info($context['instance']['entity_type']);
    $pseudo_entity = (object) $form_state['build_info']['args'][0];
    if (isset($pseudo_entity->{$info['entity keys']['id']})) {
      $entity_id = $pseudo_entity->{$info['entity keys']['id']};
    }
  }

  // Collect a list of terms and determine which are allowed
  $tids = array_keys($element['#options']);

  // Ignore the "none" option if present.
  $key = array_search('_none', $tids);
  if ($key !== FALSE) {
    unset($tids[$key]);
  }
  $allowed_tids = taxonomy_access_create_allowed($tids);
  $disallowed_tids = taxonomy_access_create_disallowed($tids);

  // If no options are allowed, deny access to the field.
  if (empty($allowed_tids)) {
    $element['#access'] = FALSE;
  }

  // On node creation, simply remove disallowed default values.
  if (!$entity_id) {
    $disallowed_defaults = array();
    if (is_array($element['#default_value'])) {
      foreach ($element['#default_value'] as $key => $value) {
        if (in_array($value, $disallowed_tids)) {
          unset($element['#default_value'][0]);
        }
      }
    }
    elseif (in_array($element['#default_value'], $disallowed_tids)) {
      unset($element['#default_value']);
    }
  }
  else {
    $defaults = is_array($element['#default_value']) ? $element['#default_value'] : array(
      $element['#default_value'],
    );
    $disallowed_defaults = array_intersect($defaults, $disallowed_tids);
    if ($context['field']['cardinality'] != FIELD_CARDINALITY_UNLIMITED) {
      if (sizeof($disallowed_defaults) >= $context['field']['cardinality']) {
        $element['#access'] = FALSE;
      }
    }
  }

  // If there are disallowed, terms, add CSS and JS for jQuery.
  // We use jQuery because FAPI does not currently support attributes
  // for individual options.
  if (!empty($disallowed_tids)) {

    // Add a css class to the field that we can use in jQuery.
    $class_name = 'tac_' . $element['#field_name'];
    $element['#attributes']['class'][] = $class_name;

    // Add js for disabling create options.
    $settings[] = array(
      'field' => $class_name,
      'disallowed_tids' => $disallowed_tids,
      'disallowed_defaults' => $disallowed_defaults,
    );
    $element['#attached']['js'][] = drupal_get_path('module', 'taxonomy_access') . '/tac_create.js';
    $element['#attached']['js'][] = array(
      'data' => array(
        'taxonomy_access' => $settings,
      ),
      'type' => 'setting',
    );
  }
  $element['#disallowed_defaults'] = $disallowed_defaults;
  $element['#element_validate'] = array(
    'taxonomy_access_options_validate',
  );
}