You are here

function rabbit_hole_form in Rabbit Hole 7.2

Form structure for the Rabbit Hole configuration.

This should be used by other modules that wish to implement the Rabbit Hole configurations in any form.

Parameters

array $attach: The form that the Rabbit Hole 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.

16 calls to rabbit_hole_form()
rh_bean_form_bean_admin_ui_type_form_alter in modules/rh_bean/rh_bean.module
Implements hook_form_FORM_ID_alter().
rh_bean_form_bean_form_alter in modules/rh_bean/rh_bean.module
Implements hook_form_FORM_ID_alter().
rh_field_collection_form_field_collection_item_form_alter in modules/rh_field_collection/rh_field_collection.module
Implements hook_form_FORM_ID_alter().
rh_field_collection_form_field_ui_field_edit_form_alter in modules/rh_field_collection/rh_field_collection.module
Implements hook_form_FORM_ID_alter().
rh_file_form_file_entity_add_upload_alter in modules/rh_file/rh_file.module
Implements hook_form_FORM_ID_alter().

... See full list

File

./rabbit_hole.module, line 101
Main module file for Rabbit Hole.

Code

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

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

    // The form is about to be attached to an entity, but the bundle isn't
    // allowed to be overridden. 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['rabbit_hole'] = array(
    '#type' => 'fieldset',
    '#title' => t('Rabbit Hole settings'),
    '#collapsed' => FALSE,
    '#collapsible' => TRUE,
    '#group' => 'additional_settings',
    '#attributes' => array(
      'class' => array(
        'rabbit-hole-settings-form',
      ),
    ),
  );

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

  // Add override setting if we're editing a bundle.
  if (!isset($entity)) {
    $form['rabbit_hole']['rh_' . $entity_type . '_override'] = array(
      '#type' => 'checkbox',
      '#title' => t('Allow these settings to be overridden for individual entities'),
      '#default_value' => rabbit_hole_get_override_bundle($entity_type, $bundle),
      '#description' => t('If this is checked, users with the %permission permission will be able to override these settings for individual entities.', array(
        '%permission' => t('Administer Rabbit Hole settings for @entity_type', array(
          '@entity_type' => $entity_label,
        )),
      )),
    );
  }

  // Build the options for the action setting.
  $action_options = array(
    RABBIT_HOLE_DISPLAY_CONTENT => t('Display the page'),
    RABBIT_HOLE_ACCESS_DENIED => t('Access denied'),
    RABBIT_HOLE_PAGE_NOT_FOUND => t('Page not found'),
    RABBIT_HOLE_PAGE_REDIRECT => t('Page redirect'),
  );
  if (isset($entity)) {

    // Add an option if we are editing an entity. This will allow us to use the
    // configuration for the bundle.
    $action_bundle = rabbit_hole_get_action_bundle($entity_type, $bundle);
    $action_options = array(
      RABBIT_HOLE_USE_DEFAULT => t('Global @bundle behavior (@setting)', array(
        '@bundle' => strtolower($bundle_label),
        '@setting' => $action_options[$action_bundle],
      )),
    ) + $action_options;
  }

  // Add action setting.
  $action_setting_name = isset($entity) ? 'rh_action' : 'rh_' . $entity_type . '_action';
  $form['rabbit_hole'][$action_setting_name] = array(
    '#type' => 'radios',
    '#title' => t('Behavior'),
    '#options' => $action_options,
    '#default_value' => isset($entity) ? rabbit_hole_get_action_entity($entity_type, $entity) : (!empty($bundle) ? rabbit_hole_get_action_bundle($entity_type, $bundle) : RABBIT_HOLE_DISPLAY_CONTENT),
    '#description' => t('What should happen when someone tries to visit an entity page for @bundle?', array(
      '@bundle' => strtolower(isset($entity_info['plural label']) ? $entity_info['plural label'] : $bundle_label),
    )),
    '#attributes' => array(
      'class' => array(
        'rabbit-hole-action-setting',
      ),
    ),
  );

  // Wrap the redirect settings in a fieldset.
  $form['rabbit_hole']['redirect'] = array(
    '#type' => 'fieldset',
    '#title' => t('Redirect settings'),
    '#attributes' => array(
      'class' => array(
        'rabbit-hole-redirect-options',
      ),
    ),
    '#states' => array(
      'visible' => array(
        ':input[name="' . $action_setting_name . '"]' => array(
          'value' => '3',
        ),
      ),
    ),
  );

  // Get the default value for the redirect path.
  $redirect_default_value = isset($entity) ? rabbit_hole_get_redirect_entity($entity_type, $entity) : (!empty($bundle) ? rabbit_hole_get_redirect_bundle($entity_type, $bundle) : RABBIT_HOLE_PAGE_REDIRECT_DEFAULT);

  // Build the descriptive text. Add some help text for PHP, if the user has the
  // permission to use PHP for evaluation.
  $description = array();
  $description[] = t('Enter the relative path or the full URL that the user should get redirected to. Query strings and fragments are supported, such as %example.', array(
    '%example' => 'http://www.example.com/?query=value#fragment',
  ));
  if (rabbit_hole_access_php($module)) {
    $placeholders = array(
      '!surround' => '<code>&lt;?php</code> and <code>?&gt;</code>',
      '!abort' => '<code>FALSE</code>',
      '!variable' => '<code>$entity</code>',
    );
    $description[] = t("You are able to evaluate PHP to determine the redirect. Surround your code by !surround. The returned string will replace the PHP part. However, you are able to return !abort if the user shouldn't get redirected. The !variable variable is available for use.", $placeholders);
  }
  $description[] = t('You may enter tokens in this field.');

  // Add the redirect path setting.
  $redirect_setting_name = isset($entity) ? 'rh_redirect' : 'rh_' . $entity_type . '_redirect';
  $form['rabbit_hole']['redirect']['redirect_setting_name'] = array(
    '#type' => 'value',
    '#value' => $redirect_setting_name,
  );
  $form['rabbit_hole']['redirect'][$redirect_setting_name] = array(
    '#type' => rabbit_hole_access_php($module) ? 'textarea' : 'textfield',
    '#title' => t('Redirect path'),
    '#default_value' => $redirect_default_value,
    '#description' => '<p>' . implode('</p><p>', $description) . '</p>',
    '#attributes' => array(
      'class' => array(
        'rabbit-hole-redirect-setting',
      ),
    ),
    '#rows' => substr_count($redirect_default_value, "\r\n") + 2,
    '#maxlength' => 2000,
  );

  // Display a list of tokens if the Token module is enabled.
  if (module_exists('token')) {
    $entity_info = entity_get_info($entity_type);
    $form['rabbit_hole']['redirect']['token_info'] = array(
      '#theme' => 'token_tree',
      '#token_types' => array(
        $entity_info['token type'],
      ),
      '#dialog' => TRUE,
    );
  }

  // Add the redirect respons setting.
  $redirect_response_setting_name = isset($entity) ? 'rh_redirect_response' : 'rh_' . $entity_type . '_redirect_response';
  $form['rabbit_hole']['redirect'][$redirect_response_setting_name] = array(
    '#type' => 'select',
    '#title' => t('Response code'),
    '#options' => array(
      301 => t('301 (Moved Permanently)'),
      302 => t('302 (Found)'),
      303 => t('303 (See other)'),
      304 => t('304 (Not modified)'),
      305 => t('305 (Use proxy)'),
      307 => t('307 (Temporary redirect)'),
    ),
    '#default_value' => isset($entity) ? rabbit_hole_get_redirect_response_entity($entity_type, $entity) : (!empty($bundle) ? rabbit_hole_get_redirect_response_bundle($entity_type, $bundle) : RABBIT_HOLE_PAGE_REDIRECT_RESPONSE_DEFAULT),
    '#description' => t('The response code that should be sent to the users browser. Follow !link for more information on response codes.', array(
      '!link' => l(t('this link'), 'http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_goto/7'),
    )),
    '#attributes' => array(
      'class' => array(
        'rabbit-hole-redirect-response-setting',
      ),
    ),
  );

  // If the redirect path contains PHP, and the user doesn't have permission to
  // use PHP for evaluation, we'll disable access to the path setting, and print
  // some helpful information about what's going on.
  if (rabbit_hole_contains_php($redirect_default_value) && !rabbit_hole_access_php($module)) {
    $form['rabbit_hole']['redirect']['#description'] = t("You're not able to edit the redirect path since it contain's PHP, and you're not allowed to evaluate PHP for this redirect.");
    $form['rabbit_hole']['redirect'][$redirect_setting_name]['#access'] = FALSE;
    if (isset($form['rabbit_hole']['redirect']['token_info'])) {
      $form['rabbit_hole']['redirect']['token_info']['#access'] = FALSE;
    }
  }

  // Attach the Rabbit Hole form to the main form, and add a custom validation
  // callback.
  $attach += $form;
  $attach['#validate'][] = 'rabbit_hole_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;
  }
}