You are here

public function ACKNodeTaxonomyIndex::objectFormAlter in Access Control Kit 7

Overrides AccessControlKitHandler::objectFormAlter().

Overrides AccessControlKitHandler::objectFormAlter

File

ack_node/handlers/ack_node_taxonomy_index.inc, line 54
Contains the handler class for the taxonomy term node index.

Class

ACKNodeTaxonomyIndex
Controls access to a node based on its relationship to a term in the index.

Code

public function objectFormAlter($object_type, $node, &$form, &$form_state, $form_id, $realms = NULL) {
  if (!empty($this->vocabulary)) {

    // Find all term reference fields that work with the realm vocabulary.
    $fields = field_read_fields(array(
      'type' => 'taxonomy_term_reference',
    ));
    foreach ($fields as $field_name => $field) {
      if (!empty($field['settings']['allowed_values']) && $field['settings']['allowed_values'][0]['vocabulary'] == $this->vocabulary) {

        // See if the field is attached to this node.
        if (!empty($form[$field_name])) {
          $language = $form[$field_name]['#language'];
          $element =& $form[$field_name][$language];

          // Lock the field if no realms are allowed.
          if (!isset($realms)) {
            $element['#disabled'] = TRUE;
          }
          elseif (isset($element['#options'])) {
            $options = array();

            // Preserve the empty option, if one was specified.
            if (isset($element['#options']['_none'])) {
              $options['_none'] = $element['#options']['_none'];
            }

            // Include only those options that correspond to allowed realms.
            foreach ($realms as $realm) {
              if (isset($element['#options'][$realm])) {
                $options[$realm] = $element['#options'][$realm];
              }
            }

            // If the element is required and only one option remains besides
            // the empty value, then remove the empty value as an option.
            if (!empty($element['#required']) && isset($options['_none']) && count($options) == 2) {
              unset($options['_none']);
            }
            $element['#options'] = $options;

            // If only one option remains, select it for the user.
            if (count($element['#options']) == 1) {
              $element['#disabled'] = TRUE;
              $key = key($element['#options']);
              $element['#default_value'] = is_array($element['#default_value']) ? array(
                $key,
              ) : $key;
            }
          }
        }
      }
    }
  }
}