You are here

function access_scheme_form in Access Control Kit 7

Form constructor for the access scheme add/edit form.

Parameters

array $form: A Forms API array

array &$form_state: An array representing the current state of the form.

object $scheme: The access scheme to edit.

Return value

array The access scheme form.

See also

access_scheme_form_validate()

access_scheme_form_submit()

access_scheme_form_delete_submit()

2 string references to 'access_scheme_form'
access_menu in ./access.module
Implements hook_menu().
access_scheme_add in ./access_schemes.admin.inc
Menu page callback; add an access scheme of a given type.

File

./access_schemes.admin.inc, line 124
Access schemes administrative UI for the access control kit module.

Code

function access_scheme_form($form, &$form_state, $scheme) {

  // During initial form build, add the scheme entity to the form state for use
  // during form building and processing. During a rebuild, use what is in the
  // form state.
  if (!isset($form_state['scheme'])) {
    $form_state['scheme'] = $scheme;
  }
  else {
    $scheme = $form_state['scheme'];
  }

  // Determine whether grants already exist for this scheme.
  $is_new = empty($scheme->sid);
  if ($is_new) {
    $has_data = FALSE;
  }
  else {
    $query = new EntityFieldQuery();
    $query
      ->entityCondition('entity_type', 'access_grant')
      ->entityCondition('bundle', $scheme->machine_name)
      ->range(0, 1);
    $result = $query
      ->execute();
    $has_data = !empty($result['access_grant']);
  }

  // Human-readable name.
  $form['name'] = array(
    '#title' => t('Name'),
    '#type' => 'textfield',
    '#default_value' => $scheme->name,
    '#description' => t('The human-readable name of this access scheme. It is recommended that this name be plural, begin with a capital letter, and contain only letters, numbers, and spaces. This name must be unique.'),
    '#required' => TRUE,
    '#size' => 28,
  );

  // Machine name.  Not editable after creation.
  $form['machine_name'] = array(
    '#type' => 'machine_name',
    '#default_value' => $scheme->machine_name,
    '#maxlength' => 28,
    '#machine_name' => array(
      'exists' => 'access_scheme_machine_name_load',
    ),
    '#description' => t('A unique machine-readable name for this access scheme. It must only contain lowercase letters, numbers, and underscores.'),
    '#disabled' => !$is_new,
  );

  // Load the callbacks' include file, if one exists.
  if (!empty($scheme->info['include file'])) {
    require_once DRUPAL_ROOT . '/' . $scheme->info['include file'];
  }

  // Add any additional settings defined for this scheme type.
  if (!empty($scheme->info['settings callback']) && function_exists($scheme->info['settings callback'])) {
    $form['settings'] = call_user_func_array($scheme->info['settings callback'], array(
      $scheme,
      $has_data,
    ));
    $form['settings']['#type'] = 'container';
    $form['settings']['#tree'] = TRUE;
  }

  // Description.
  $form['description'] = array(
    '#title' => t('Description'),
    '#type' => 'textarea',
    '#default_value' => $scheme->description,
    '#description' => t('A brief description of this access scheme.'),
  );

  // Hide the basic properties in a collapsed fieldset on existing schemes.
  if (!$is_new) {
    $form = array(
      'basic' => $form,
    );
    $form['basic']['#type'] = 'fieldset';
    $form['basic']['#title'] = t('Basic settings');
    $form['basic']['#collapsible'] = TRUE;
    $form['basic']['#collapsed'] = TRUE;
  }

  // Realm-enabled roles.
  $form['roles'] = array(
    '#type' => 'fieldset',
    '#title' => t('Roles'),
    '#collapsible' => TRUE,
    '#collapsed' => !$is_new,
  );
  $roles = user_roles(TRUE);
  unset($roles[DRUPAL_AUTHENTICATED_RID]);
  $form['roles']['roles'] = array(
    '#type' => 'checkboxes',
    '#title' => t('User roles available for use within this access scheme'),
    '#options' => $roles,
    '#default_value' => isset($scheme->roles) ? array_keys($scheme->roles) : array(),
    '#description' => t('Users with the <em>administer access grants</em> permission will be able to grant access to users with these roles within one or more access realms.'),
  );

  // Handler settings only become available after the scheme has been saved, and
  // only if we have access-controllable object types defined.
  if (!$is_new) {
    $access_info = access_info();
    if (empty($access_info)) {
      $form['handlers_empty'] = array(
        '#markup' => t('No access-controllable objects available. In order to control access to content, menu links, or other Drupal objects, you must first <a href="@link">enable one or more modules</a> that provide Access Control Kit compatibility for those objects, such as the ACK node module.', array(
          '@link' => url('admin/modules'),
        )),
      );
    }
    else {
      $handler_info = access_handler_info();
      $form['handler_tabs'] = array(
        '#type' => 'vertical_tabs',
        '#prefix' => '<h2>' . t('Object access handlers') . '</h2>',
        '#attached' => array(
          'js' => array(
            drupal_get_path('module', 'access') . '/access_schemes.js',
          ),
        ),
      );

      // Add a tab for each access-controllable object type.
      $form['handlers'] = array(
        '#tree' => TRUE,
      );
      foreach ($access_info as $object_type => $object_info) {
        $form['handlers'][$object_type] = array(
          '#type' => 'fieldset',
          '#title' => check_plain($object_info['label']),
          '#group' => 'handler_tabs',
        );

        // Add elements for each usable handler.
        $handlers = array();
        foreach ($object_info['handlers'] as $handler_class) {

          // Make sure that the handler supports this scheme type.
          if (isset($handler_info[$handler_class]) && in_array($scheme->type, $handler_info[$handler_class]['scheme types'])) {
            $handlers[$handler_class] = access_scheme_form_handler_element($scheme, $object_type, $handler_class);
          }
        }
        if (empty($handlers)) {
          $form['handlers'][$object_type]['#description'] = t('No handlers available.');
          $form['handlers'][$object_type]['empty'] = array(
            '#markup' => t('No object access handlers are available to manage @object_type objects in a @scheme_type scheme.', array(
              '@object_type' => $object_info['label'],
              '@scheme_type' => $scheme->info['label'],
            )),
          );
        }
        else {
          $form['handlers'][$object_type]['empty'] = array(
            '#type' => 'container',
            'handler' => array(
              '#type' => 'radio',
              '#title' => t('Not managed'),
              '#return_value' => '',
              '#default_value' => isset($scheme->handlers[$object_type]) ? FALSE : '',
              '#description' => t('Objects of this type will not be managed by the access scheme.'),
              '#parents' => array(
                'handlers',
                $object_type,
                'handler',
              ),
            ),
          );
          $form['handlers'][$object_type] += $handlers;
        }
      }
    }
  }
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => $is_new ? t('Save access scheme and continue') : t('Save access scheme'),
  );
  if (!$is_new) {
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete access scheme'),
      '#submit' => array(
        'access_scheme_form_delete_submit',
      ),
    );
  }
  return $form;
}