You are here

class ACKNodeTaxonomyIndex in Access Control Kit 7

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

Hierarchy

Expanded class hierarchy of ACKNodeTaxonomyIndex

1 string reference to 'ACKNodeTaxonomyIndex'
AckNodeAccessTest::testTaxonomyAccess in ack_node/ack_node.test
Test controlling access with the taxonomy index.

File

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

View source
class ACKNodeTaxonomyIndex extends AccessControlKitHandler {

  /**
   * The machine name of the vocabulary that defines the realms.
   *
   * @var string
   */
  protected $vocabulary;

  /**
   * Overrides AccessControlKitHandler::__construct().
   */
  public function __construct($scheme, array $settings = array()) {
    parent::__construct($scheme, $settings);

    // Get the vocabulary from the scheme settings.
    $this->vocabulary = isset($scheme->settings['vocabulary']) ? $scheme->settings['vocabulary'] : NULL;
  }

  /**
   * Overrides AccessControlKitHandler::description().
   */
  public function description() {
    return t('Content will be considered a part of an access realm if it is tagged with that realm in any available taxonomy term reference field.');
  }

  /**
   * Overrides AccessControlKitHandler::objectRealms().
   */
  public function objectRealms($object_type, $node) {
    $query = db_select('taxonomy_index', 'i');
    $query
      ->join('taxonomy_term_data', 't', 'i.tid = t.tid');
    $query
      ->join('taxonomy_vocabulary', 'v', 't.vid = v.vid');
    $query
      ->fields('i', array(
      'tid',
    ))
      ->condition('i.nid', $node->nid)
      ->condition('v.machine_name', $this->vocabulary);
    $result = $query
      ->execute();
    return $result
      ->fetchCol();
  }

  /**
   * Overrides AccessControlKitHandler::objectFormAlter().
   */
  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;
              }
            }
          }
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AccessControlKitHandler::$settings protected property The access handler configuration.
AccessControlKitHandler::getSettings public function Implements AccessControlKitHandlerInterface::getSettings(). Overrides AccessControlKitHandlerInterface::getSettings
AccessControlKitHandler::objectFormSubmit public function Implements AccessControlKitHandlerInterface::objectFormSubmit(). Overrides AccessControlKitHandlerInterface::objectFormSubmit 1
AccessControlKitHandler::objectFormValidate public function Implements AccessControlKitHandlerInterface::objectFormValidate(). Overrides AccessControlKitHandlerInterface::objectFormValidate
AccessControlKitHandler::settingsForm public function Implements AccessControlKitHandlerInterface::settingsForm(). Overrides AccessControlKitHandlerInterface::settingsForm 3
AccessControlKitHandler::viewsDataAlter public function Implements AccessControlKitHandlerInterface::viewsDataAlter(). Overrides AccessControlKitHandlerInterface::viewsDataAlter 1
ACKNodeTaxonomyIndex::$vocabulary protected property The machine name of the vocabulary that defines the realms.
ACKNodeTaxonomyIndex::description public function Overrides AccessControlKitHandler::description(). Overrides AccessControlKitHandler::description
ACKNodeTaxonomyIndex::objectFormAlter public function Overrides AccessControlKitHandler::objectFormAlter(). Overrides AccessControlKitHandler::objectFormAlter
ACKNodeTaxonomyIndex::objectRealms public function Overrides AccessControlKitHandler::objectRealms(). Overrides AccessControlKitHandler::objectRealms
ACKNodeTaxonomyIndex::__construct public function Overrides AccessControlKitHandler::__construct(). Overrides AccessControlKitHandler::__construct