1 system.admin.inc system_urls_settings($form, &$form_state)

Form builder; Configure clean URL settings.

Related topics

File

core/modules/system/system.admin.inc, line 2212
Admin page callbacks for the System module.

Code

function system_urls_settings($form, &$form_state) {
  $config = config('system.core');
  $available = FALSE;
  $conflict = FALSE;

  // If the request URI is a clean URL, clean URLs must be available.
  // Otherwise, run a test.
  if (strpos(request_uri(), '?q=') === FALSE && strpos(request_uri(), '&q=') === FALSE) {
    $available = TRUE;
  }
  else {
    $request = backdrop_http_request($GLOBALS['base_url'] . '/admin/config/urls/settings/check');
    // If the request returns HTTP 200, clean URLs are available.
    if (isset($request->code) && $request->code == 200) {
      $available = TRUE;
      // If the user started the clean URL test, provide explicit feedback.
      if (isset($form_state['input']['clean_url_test_execute'])) {
        backdrop_set_message(t('The clean URL test passed.'));
      }
    }
    else {
      if ($request->error) {
        watchdog('system', 'HTTP request for Clean URLs failed with error: @error', array('@error' => $request->error));
      }

      // If the test failed while clean URLs are enabled, make sure clean URLs
      // can be disabled.
      if ($config->get('clean_url')) {
        $conflict = TRUE;
        // Warn the user of a conflicting situation, unless after processing
        // a submitted form.
        if (!isset($form_state['input']['op'])) {
          backdrop_set_message(t('Clean URLs are enabled, but the clean URL test failed. Uncheck the box below to disable clean URLs.'), 'warning');
        }
      }
      // If the user started the clean URL test, provide explicit feedback.
      elseif (isset($form_state['input']['clean_url_test_execute'])) {
        backdrop_set_message(t('The clean URL test failed.'), 'warning');
      }
    }
  }

  $form['#config'] = 'system.core';
  // Show the enable/disable form if clean URLs are available, or if the user
  // must be able to resolve a conflicting setting.
  if ($available || $conflict) {
    $form['clean_url'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable clean URLs'),
      '#default_value' => $config->get('clean_url'),
      '#description' => t('Use URLs like <code>example.com/user</code> instead of <code>example.com/?q=user</code>.'),
    );
    if ($conflict) {
      // $form_state['redirect'] needs to be set to the non-clean URL,
      // otherwise the setting is not saved.
      $form_state['redirect'] = url('', array('query' => array('q' => '/admin/config/url/settings')));
    }
  }
  // Show the clean URLs test form.
  else {
    backdrop_add_js(backdrop_get_path('module', 'system') . '/js/system.admin.js');

    // Explain why the user is seeing this page and what to expect after
    // clicking the 'Run the clean URL test' button.
    backdrop_set_message(t('Clean URLs cannot be enabled.'), 'warning');
    backdrop_set_message(t('When clean URLs can be enabled, you will be able to use URLs like <code>example.com/user</code> instead of <code>example.com/?q=user</code>.'), 'info');
    backdrop_set_message(t('If you are directed to this page or to a "Page not found (404)" error after testing for clean URLs, see the <a href="@url">online handbook</a>.', array('@url' => url('https://backdropcms.org/user-guide/clean-urls'))), 'info');

    $form_state['redirect'] = url('admin/config/urls/settings');
    $form['actions'] = array('#type' => 'actions');
    $form['actions']['clean_url_test'] = array(
      '#type' => 'submit',
      '#value' => t('Run the clean URL test'),
      '#submit' => array(),
    );
    $form['clean_url_test_execute'] = array(
      '#type' => 'hidden',
      '#value' => 1,
    );
  }

  $form['canonical_secure'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use HTTPS for canonical URLs'),
    '#default_value' => $config->get('canonical_secure'),
    '#description' => t('This option makes Backdrop use HTTPS protocol for generated canonical URLs. Please note: to get it working in mixed-mode (both secure and insecure) sessions, the variable <code>https</code> should be set to <code>TRUE</code> in your file <code>settings.php</code>'),
  );

  return system_settings_form($form);
}