You are here

function query_parameters_to_url_init in Query Parameters To URL 7

Implements hook_init().

There are situations when we can't alter a link, like a submitted Views exposed filter, which sets the query parameters via a form, with the form action set to GET. In those cases we will do a redirect to the Clean URL, only the specified paths.

File

./query_parameters_to_url.module, line 595
Query Arguments To URL module.

Code

function query_parameters_to_url_init() {
  if (variable_get(QUERY_PARAMETERS_TO_URL_ENABLED, TRUE)) {

    // Check whether the path was altered in inbound alter function, which means
    // we shouldn't rewrite the URL again.
    $path_altered =& drupal_static('query_parameters_to_url_url_inbound_alter', FALSE);
    if (!$path_altered) {

      // Get current path and query parameters, and prepare to send them to the
      // outbound alter hook, so it tries and rewrites a path with query
      // parameters into a new clean URL with encoded query parameters.
      $original_path = current_path();

      // Hack support for global redirect module, to make sure that the front
      // page has an empty string for the URL.
      if (drupal_is_front_page() && module_exists('globalredirect') && ($settings = _globalredirect_get_settings()) && $settings['frontpage_redirect']) {
        $original_path = '';
      }
      $original_query_parameters = drupal_get_query_parameters();
      $path = $original_path;
      $options = array(
        'query' => $original_query_parameters,
      );

      // Skip the global redirect check, because we know this is not called
      // from globalredirect_init().
      $options['skip_global_redirect'] = TRUE;

      // Try and rewrite the path.
      query_parameters_to_url_url_outbound_alter($path, $options, $path);

      // Make sure to remove any left slashes, if the path was empty, because
      // it was the front page path.
      $path = ltrim($path, '/');

      // If the path indeed was changed, we redirect to the new path.
      if ($path != $original_path) {
        $options['absolute'] = TRUE;
        $url = url($path, $options);
        $url = query_parameters_to_url_replace_url_encoded_delimiters($url);
        $exit_early = query_parameters_to_url_protect_redirect_loop();
        if ($exit_early) {
          return;
        }

        // Inspired by redirect module, tell the browser where to redirect
        // and why.
        drupal_add_http_header('Location', $url);
        $status_code = query_parameters_to_url_redirect_status_code();
        $status_message = query_parameters_to_url_status_code_options($status_code);
        drupal_add_http_header('Status', $status_message);

        // Output redirect, and cache it if necessary.
        query_parameters_to_url_output_and_cache_redirect($url);
      }
    }
  }
}