You are here

function field_redirection_field_formatter_settings_form in Field Redirection 7.2

Implements hook_field_formatter_settings_from().

File

./field_redirection.module, line 55
Provides a field formatter to redirect to another path.

Code

function field_redirection_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {

  // Shortcuts to make the rest of the code simpler.
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $codes = field_redirection_http_codes();

  // Load the current selection, default to "301".
  $code = 301;
  if (!empty($settings['code']) && isset($codes[$settings['code']])) {
    $code = $settings['code'];
  }

  // Build a new structure for this formatter settings form element.
  $element = array();

  // Choose the redirector.
  $element['code'] = array(
    '#title' => 'HTTP status code',
    '#type' => 'select',
    '#options' => field_redirection_http_codes(),
    '#default_value' => $code,
  );

  // 404 if the field value is empty.
  $element['404_if_empty'] = array(
    '#type' => 'checkbox',
    '#title' => t('404 if URL empty'),
    '#default_value' => !empty($settings['404_if_empty']),
    '#description' => t('Optionally display a 404 error page if the associated URL field is empty.'),
  );
  $element['note'] = array(
    '#markup' => t('Note: If the destination path is the same as the current path it will behave as if it is empty.'),
    '#prefix' => '<p>',
    '#suffix' => '</p>',
  );

  // Provide targeted URL rules to trigger this action.
  $element['page_restrictions'] = array(
    '#type' => 'radios',
    '#title' => t('Redirect page restrictions'),
    '#default_value' => empty($settings['page_restrictions']) ? 0 : $settings['page_restrictions'],
    '#options' => array(
      0 => t('Redirect on all pages.'),
      1 => t('Redirect only on the following pages.'),
      2 => t('Redirect on all pages except the following pages.'),
    ),
  );
  $element['pages'] = array(
    '#type' => 'textarea',
    '#title' => t('Paths'),
    '#default_value' => empty($settings['pages']) ? '' : $settings['pages'],
    '#description' => t("Enter one page per line as Drupal paths. The '@wildcard' character is a wildcard. Example paths are '@example_blog' for the blog page and '@example_all_personal_blogs' for every personal blog. '@frontpage' is the front page. You can also use tokens in this field, for example '@example_current_node' can be used to define the current node path.", array(
      '@wildcard' => '*',
      '@example_blog' => 'blog',
      '@example_all_personal_blogs' => 'blog/*',
      '@frontpage' => '<front>',
      '@example_current_node' => 'node/[node:nid]',
    )),
    '#states' => array(
      'invisible' => array(
        ':input[name~="page_restrictions"]' => array(
          array(
            'value' => '0',
          ),
        ),
      ),
    ),
  );

  // Show the token browser.
  $element['available_tokens'] = array(
    '#type' => 'container',
    '#weight' => 100,
    '#states' => $element['pages']['#states'],
  );
  $element['available_tokens']['tokens'] = array(
    '#value' => 'Browse available tokens',
    '#theme' => 'token_tree',
    '#token_types' => array(
      $instance['entity_type'],
    ),
    '#dialog' => TRUE,
  );
  return $element;
}