You are here

function tca_form in Token Content Access 7

Form structure for the Token Content Access configuration.

This should be used by other modules that wish to implement the Token Content Access configurations in any form.

Parameters

array $attach: The form that the Token Content Access form should be attached to.

string $entity_type: The entity type that we're adding the form for, e.g. 'node'.

string $bundle: The bundle that we're adding the form to, e.g. the content type for nodes. This might be an empty string if we're creating a new bundle.

string $module: The name of the module that invokes this function.

object $entity: The entity that we're adding the form to, e.g. a node. This will be NULL if the form is being attached to the bundle configuration form.

2 calls to tca_form()
tca_node_form_node_form_alter in tca_node/tca_node.module
Implements hook_form_FORM_ID_alter().
tca_node_form_node_type_form_alter in tca_node/tca_node.module
Implements hook_form_FORM_ID_alter().

File

./tca.module, line 205
The Token Content Access module file.

Code

function tca_form(array &$attach, $form_state, $entity_type, $bundle, $module, $entity = NULL) {
  if (!user_access('administer ' . $module)) {

    // The user doesn't have access, exit.
    return;
  }
  if (isset($entity) && !tca_get_active_bundle($entity_type, $bundle)) {

    // The form is about to be attached to an entity, but the bundle isn't
    // allowed to use Token Content Access, exit.
    return;
  }

  // Get information about the entity.
  $entity_info = entity_get_info($entity_type);
  $entity_label = strtolower(isset($entity_info['plural label']) ? $entity_info['plural label'] : $entity_info['label']);

  // Get the label for the bundle. This won't be set when the user is creating a
  // new bundle. In that case, fallback to "this bundle".
  $bundle_label = isset($entity_info['bundles'][$bundle]['label']) ? $entity_info['bundles'][$bundle]['label'] : 'this bundle';

  // Wrap everything in a fieldset.
  $form['tca'] = array(
    '#type' => 'fieldset',
    '#title' => t('Token Content Access settings'),
    '#collapsed' => FALSE,
    '#collapsible' => TRUE,
    '#group' => 'additional_settings',
    '#attributes' => array(
      'class' => array(
        'tca-settings-form',
      ),
    ),
  );

  // Add the invoking module to the internal values.
  $form['tca']['tca_module'] = array(
    '#type' => 'value',
    '#value' => $module,
  );

  // Add the entity type to the internal values.
  $form['tca']['tca_entity_type'] = array(
    '#type' => 'value',
    '#value' => $entity_type,
  );

  // Add override setting if we're editing a bundle.
  if (!isset($entity)) {
    $form['tca']['tca_' . $entity_type . '_active'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable Token Content Access protection.'),
      '#default_value' => tca_get_active_bundle($entity_type, $bundle),
      '#description' => t('If this is checked, users with the %permission permission will be able to enable Token Content Access protection for individual entities.', array(
        '%permission' => t('Administer Token Content Access settings for @entity_type', array(
          '@entity_type' => $entity_label,
        )),
      )),
    );
  }

  // Add Token activation if we're editing a new or existing entity.
  if (isset($entity)) {

    // Add activation settings.
    $form['tca']['tca_active'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable Token Content Access protection.'),
      '#default_value' => isset($entity) ? tca_get_active_entity($entity_type, $entity) : FALSE,
      '#description' => t('Prevent users from viewing an this @bundle page w/o providing an access token via the URL.', array(
        '@bundle' => strtolower(isset($entity_info['plural label']) ? $entity_info['plural label'] : $bundle_label),
      )),
      '#attributes' => array(
        'class' => array(
          'tca-active-setting',
        ),
      ),
    );

    // Only populate token information if we are editing an existing entity.
    list($entity_id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity);
    if ($entity_id) {

      // Wrap the token settings in a fieldset.
      $form['tca']['token'] = array(
        '#prefix' => '<div id="tca-token-form-entity">',
        '#suffix' => '</div>',
        '#type' => 'fieldset',
        '#title' => t('Token settings'),
        '#description' => t('Token settings'),
        '#attributes' => array(
          'class' => array(
            'tca-redirect-options',
          ),
        ),
        '#states' => array(
          'visible' => array(
            ':input[name="tca_active"]' => array(
              'checked' => TRUE,
            ),
          ),
        ),
      );
      if (!isset($form_state['values']['tca_token'])) {
        $token = tca_get_token_entity($entity_type, $entity);
        $token = !empty($token) ? tca_get_token_entity($entity_type, $entity) : tca_get_token($entity_type, $entity, $value = '');
      }
      else {
        $token = $form_state['values']['tca_token'];
      }
      $entity_uri = entity_uri($entity_type, $entity);
      $description = array();
      $description[] = t('Append this access token to the URL as the value for the "tca" query parameter, for example:');
      $description[] = '<pre>' . url($entity_uri['path'], array(
        'absolute' => TRUE,
        'query' => array(
          'tca' => $token,
        ),
      )) . '</pre>';
      $form['tca']['token']['tca_token'] = array(
        '#type' => 'textfield',
        '#title' => t('Access Token'),
        '#default_value' => $token,
        '#description' => '<p>' . implode('</p><p>', $description) . '</p>',
        '#disabled' => TRUE,
        '#attributes' => array(
          'class' => array(
            'tca-access-token',
          ),
        ),
      );

      // Add replace token functionality.
      $form['tca']['tca_token_replace'] = array(
        '#type' => 'submit',
        '#value' => t('Regenerate Token'),
        '#description' => t('Generate a new token.'),
        '#submit' => array(
          'tca_token_replace',
        ),
        '#ajax' => array(
          'callback' => 'tca_token_replace_callback',
          'wrapper' => 'tca-token-form-entity',
        ),
      );
    }
  }

  // Attach the Token Content Access form to the main form, and add a custom
  // validation callback.
  $attach += $form;
  $attach['#validate'][] = 'tca_form_validate';

  // If the implementing module provides a submit function for the bundle form,
  // we'll add it as a submit function for the attached form. We'll also make
  // sure that this won't be added for entity forms.
  $submit_function = $module . '_bundle_form_submit';
  if (function_exists($submit_function) && !isset($entity)) {
    $attach['#submit'][] = $submit_function;
  }
}