1 redirect.admin.inc redirect_edit_form($form, &$form_state, $redirect = NULL)

Form builder to add or edit an URL redirect.

Parameters

Redirect|NULL $redirect: The redirect object to be edited or NULL if creating a new redirect.

See also

redirect_element_validate_source()

redirect_element_validate_redirect()

redirect_edit_form_validate()

redirect_edit_form_submit()

Related topics

File

core/modules/redirect/redirect.admin.inc, line 342
Admin page callbacks for the Redirect module.

Code

function redirect_edit_form($form, &$form_state, $redirect = NULL) {
  if (!isset($redirect)) {
    // backdrop_set_title() used to "manually" change the page title. See:
    // https://github.com/backdrop/backdrop-issues/issues/3315
    // @todo Check if this is needed after/if this gets fixed in core
    // menu.module. See: https://www.drupal.org/project/drupal/issues/891892
    backdrop_set_title(t('Add redirect'));
    $redirect = new Redirect(array(
      'source' => isset($_GET['source']) ? urldecode($_GET['source']) : '',
      'source_options' => isset($_GET['source_options']) ? backdrop_get_query_array($_GET['source_options']) : array(),
      'redirect' => isset($_GET['redirect']) ? urldecode($_GET['redirect']) : '',
      'redirect_options' => isset($_GET['redirect_options']) ? backdrop_get_query_array($_GET['redirect_options']) : array(),
      'langcode' => isset($_GET['langcode']) ? urldecode($_GET['langcode']) : 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 Backdrop path or URL alias 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' => 560,
    '#default_value' => $redirect->rid || $redirect->source ? redirect_url($redirect->source, $redirect->source_options + array('alter' => FALSE)) : '',
    '#required' => TRUE,
    '#field_prefix' => $GLOBALS['base_url'] . '/' . (config_get('system.core', 'clean_url') ? '' : '?q='),
    '#element_validate' => array('redirect_element_validate_source'),
    '#autocomplete_path' => 'path-autocomplete',
  );
  $form['source_options'] = array(
    '#type' => 'value',
    '#value' => $redirect->source_options,
    '#tree' => TRUE,
  );
  $form['redirect'] = array(
    '#type' => 'textfield',
    '#title' => t('To'),
    '#maxlength' => 560,
    '#default_value' => $redirect->rid || $redirect->redirect ? redirect_url($redirect->redirect, $redirect->redirect_options, TRUE) : '',
    '#required' => TRUE,
    '#description' => t('Enter an internal Backdrop path, URL alias, or complete external URL (like http://example.com/) to which the user will be sent. Use %front to redirect to the home page.', array('%front' => '<front>')),
    '#element_validate' => array('redirect_element_validate_redirect'),
    '#autocomplete_path' => 'path-autocomplete',
  );
  $form['redirect_options'] = array(
    '#type' => 'value',
    '#value' => $redirect->redirect_options,
    '#tree' => TRUE,
  );

  // This will be a hidden value unless locale module is enabled.
  $form['langcode'] = array(
    '#type' => 'value',
    '#value' => $redirect->langcode,
  );

  $all_statuses = config_get('redirect.settings', 'additional_statuses');
  $options = redirect_status_code_options(NULL, $all_statuses);
  if (!array_key_exists($redirect->status_code, $options)) {
    $options = redirect_status_code_options(NULL, TRUE);
  }
  $form['status_code'] = array(
    '#type' => $all_statuses ? 'select' : 'radios',
    '#title' => t('Redirect type'),
    '#description' => t('<a href="https://wikipedia.org/wiki/List_of_HTTP_status_codes#3xx_Redirection">More information about status codes</a>.'),
    '#default_value' => $redirect->status_code,
    '#options' => $options,
  );

  $form['override'] = array(
    '#prefix' => '<div class="messages warning">',
    '#suffix' => '</div>',
    '#access' => FALSE,
    '#weight' => -100,
  );
  $form['override']['confirm'] = array(
    '#type' => 'checkbox',
    '#title' => t('I understand the above warnings and would like to proceed with saving this URL redirect'),
    '#default_value' => FALSE,
    '#parents' => array('override'),
  );

  if (!empty($form_state['storage']['override_messages']) && !form_get_errors()) {
    $form['override']['#access'] = TRUE;
    if (count($form_state['storage']['override_messages']) > 1) {
      $message_output = theme('item_list', array('items' => $form_state['storage']['override_messages']));
    }
    else {
      $message_output = implode('', $form_state['storage']['override_messages']);
    }
    $form['override']['messages'] = array(
      '#markup' => $message_output,
      '#weight' => -1,
    );
    // Reset the messages.
    $form_state['storage']['override_messages'] = array();
  }

  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save redirect'),
  );
  $form['actions']['delete'] = array(
    '#type' => 'submit',
    '#value' => t('Delete'),
    '#submit' => array('redirect_edit_form_delete_submit'),
    '#limit_validation_errors' => array(array('actions')),
    '#access' => !$redirect->isNew(),
  );
  $form['actions']['cancel'] = array(
    '#type' => 'link',
    '#title' => t('Cancel'),
    '#href' => isset($_GET['destination']) ? $_GET['destination'] : 'admin/config/urls/redirect',
  );

  return $form;
}