You are here

function redirect_edit_form in Redirect 7

Same name and namespace in other branches
  1. 7.2 redirect.admin.inc \redirect_edit_form()

Form builder to add or edit an URL redirect.

See also

redirect_element_validate_source()

redirect_element_validate_redirect()

redirect_edit_form_validate()

redirect_edit_form_submit()

1 string reference to 'redirect_edit_form'
redirect_menu in ./redirect.module
Implements hook_menu().

File

./redirect.admin.inc, line 328
Administrative page callbacks for the redirect module.

Code

function redirect_edit_form($form, &$form_state, $redirect = NULL) {
  if (empty($redirect)) {
    $redirect = new stdClass();
  }

  // Merge default values.
  redirect_object_prepare($redirect, array(
    'source' => isset($_GET['source']) ? urldecode($_GET['source']) : '',
    'source_options' => isset($_GET['source_options']) ? drupal_get_query_array($_GET['source_options']) : array(),
    'redirect' => isset($_GET['redirect']) ? urldecode($_GET['redirect']) : '',
    'redirect_options' => isset($_GET['redirect_options']) ? drupal_get_query_array($_GET['redirect_options']) : array(),
    'language' => isset($_GET['language']) ? urldecode($_GET['language']) : LANGUAGE_NONE,
  ));
  $form['rid'] = array(
    '#type' => 'value',
    '#value' => $redirect->rid,
  );
  $form['type'] = array(
    '#type' => 'value',
    '#value' => $redirect->type,
  );
  $form['hash'] = array(
    '#type' => 'value',
    '#value' => $redirect->hash,
  );
  $form['uid'] = array(
    '#type' => 'value',
    '#value' => $redirect->uid,
  );
  $form['source'] = array(
    '#type' => 'textfield',
    '#title' => t('From'),
    '#description' => t("Enter an internal Drupal path or path alias, of up to 900 characters, to redirect (e.g. %example1 or %example2). Fragment anchors (e.g. %anchor) are <strong>not</strong> allowed. ", array(
      '%example1' => 'node/123',
      '%example2' => 'taxonomy/term/123',
      '%anchor' => '#anchor',
    )),
    '#maxlength' => 900,
    '#default_value' => $redirect->rid || $redirect->source ? redirect_url($redirect->source, $redirect->source_options + array(
      'alter' => FALSE,
    )) : '',
    '#required' => TRUE,
    '#field_prefix' => $GLOBALS['base_url'] . '/' . (variable_get('clean_url', 0) ? '' : '?q='),
    '#element_validate' => array(
      'redirect_element_validate_source',
    ),
  );
  $form['source_options'] = array(
    '#type' => 'value',
    '#value' => $redirect->source_options,
    '#tree' => TRUE,
  );
  $form['redirect'] = array(
    '#type' => 'textfield',
    '#title' => t('To'),
    '#maxlength' => 900,
    '#default_value' => $redirect->rid || $redirect->redirect ? redirect_url($redirect->redirect, $redirect->redirect_options, TRUE) : '',
    '#required' => TRUE,
    '#description' => t('Enter an internal Drupal path, path alias, or complete external URL (like http://example.com/) to redirect to. The value length is up to 900 characters. Use %front to redirect to the front page.', array(
      '%front' => '<front>',
    )),
    '#element_validate' => array(
      'redirect_element_validate_redirect',
    ),
  );
  $form['redirect_options'] = array(
    '#type' => 'value',
    '#value' => $redirect->redirect_options,
    '#tree' => TRUE,
  );

  // This will be a hidden value unless locale module is enabled.
  $form['language'] = array(
    '#type' => 'value',
    '#value' => $redirect->language,
  );
  $form['status'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enabled'),
    '#description' => t('If this box is checked, this redirect will be enabled.'),
    '#default_value' => $redirect->status,
    '#required' => FALSE,
  );
  $form['advanced'] = array(
    '#type' => 'fieldset',
    '#title' => t('Advanced options'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['advanced']['status_code'] = array(
    '#type' => 'select',
    '#title' => t('Redirect status'),
    '#description' => t('You can find more information about HTTP redirect status codes at <a href="@status-codes">@status-codes</a>.', array(
      '@status-codes' => 'http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#3xx_Redirection',
    )),
    '#default_value' => $redirect->status_code,
    '#options' => array(
      0 => t('Default (@default)', array(
        '@default' => variable_get('redirect_default_status_code', 301),
      )),
    ) + redirect_status_code_options(),
  );
  $form['advanced']['passthrough_querystring'] = array(
    '#type' => 'select',
    '#title' => t('Retain query string through redirect.'),
    '#default_value' => isset($redirect->redirect_options['passthrough_querystring']) ? $redirect->redirect_options['passthrough_querystring'] : '',
    '#options' => array(
      '' => 'Use global setting',
      '0' => 'No',
      '1' => 'Yes',
    ),
    '#description' => t('For example, given a redirect from %source to %redirect, if a user visits %sourcequery they would be redirected to %redirectquery. The query strings in the redirection will always take precedence over the current query string.', array(
      '%source' => 'source-path',
      '%redirect' => 'node?a=apples',
      '%sourcequery' => 'source-path?a=alligators&b=bananas',
      '%redirectquery' => 'node?a=apples&b=bananas',
    )),
  );
  $form['override'] = array(
    '#type' => 'checkbox',
    '#title' => t('I understand the following warnings and would like to proceed with saving this URL redirect'),
    '#default_value' => FALSE,
    '#access' => FALSE,
    '#required' => FALSE,
    '#weight' => -100,
    '#prefix' => '<div class="messages warning">',
    '#suffix' => '</div>',
  );
  if (!empty($form_state['storage']['override_messages'])) {
    $form['override']['#access'] = TRUE;

    //$form['override']['#required'] = TRUE;
    $form['override']['#description'] = theme('item_list', array(
      'items' => $form_state['storage']['override_messages'],
    ));

    // Reset the messages.
    $form_state['storage']['override_messages'] = array();
  }
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  $form['actions']['cancel'] = array(
    '#type' => 'link',
    '#title' => t('Cancel'),
    '#href' => isset($_GET['destination']) ? $_GET['destination'] : 'admin/config/search/redirect',
  );
  return $form;
}