You are here

public function ACKEntityField::objectFormAlter in Access Control Kit 7

Overrides AccessControlKitHandler::objectFormAlter().

Overrides AccessControlKitHandler::objectFormAlter

File

handlers/ack_entity_field.inc, line 69
Contains the handler class for Field API fields on entities.

Class

ACKEntityField
Controls access to a fieldable entity based on a Field API field.

Code

public function objectFormAlter($object_type, $object, &$form, &$form_state, $form_id, $realms = NULL) {
  if (!empty($this->fieldName) && !empty($form[$this->fieldName])) {
    $language = $form[$this->fieldName]['#language'];
    $element =& $form[$this->fieldName][$language];

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

        // Preserve options that correspond to allowed realms.
        // Also preserve the empty option, if one was provided.
        if (in_array($option_value, $realms) || $option_value == '_none') {
          continue;
        }

        // Remove unmatched options.
        unset($element['#options'][$option_value]);
      }

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

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