You are here

function query_parameters_to_url_url_outbound_alter in Query Parameters To URL 7

Implements hook_url_outbound_alter().

2 calls to query_parameters_to_url_url_outbound_alter()
query_parameters_to_url_admin_examples in ./query_parameters_to_url.admin.inc
Example URLs page callback.
query_parameters_to_url_init in ./query_parameters_to_url.module
Implements hook_init().

File

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

Code

function query_parameters_to_url_url_outbound_alter(&$path, &$options, $original_path) {
  if (variable_get(QUERY_PARAMETERS_TO_URL_ENABLED, TRUE)) {

    // Check if this is a path we should rewrite.
    $rewrite = query_parameters_to_url_path_should_be_rewritten($path, $options, $original_path);
    if ($rewrite) {

      // If asked to skip the check, by hook_init(), skip it.
      $skip_global_redirect_state = isset($options['skip_global_redirect']) && $options['skip_global_redirect'] == TRUE;

      // Don't encode the query parameters if called from globalredirect_init().
      // The hook_outbound_url_alter() will be called twice from inside
      // globalredirect_init().
      if (!$skip_global_redirect_state && query_parameters_to_url_is_called_from_global_redirect_init()) {
        return;
      }

      // Check if link has any query parameters set.
      if (isset($options['query']) && !empty($options['query'])) {
        $query_parameters_components = '';

        // The new path parts array, containing the the query parameters, will
        // be delimited with a special character, so we know where the real URL
        // components start, and where the encoded query parameters start.
        $url_and_query_delimiter = query_parameters_to_url_url_query_delimiter();

        // Make sure to replace <front> with front page url.
        if ($path == '<front>') {
          $path = variable_get('site_frontpage', 'node');
        }

        // Make sure to trim the rightmost slashes, so double slashes don't
        // occur in the path.
        $new_path_no_delimiter = rtrim($path, '/');
        $new_path = $new_path_no_delimiter . '/' . $url_and_query_delimiter;
        if (query_parameters_to_url_rewrite_hooks_enabled()) {

          // Allow altering the final outbound url, before encoding.
          $context = array(
            'direction' => 'outbound',
            'options' => &$options,
            'original_path' => $original_path,
            'new_path_without_parameters' => &$new_path,
          );
          drupal_alter('query_parameters_to_url_rewrite', $path, $context);
        }
        foreach ($options['query'] as $key => $values) {

          // Encode the query parameter values to clean url component.
          $encoded_query_parameter_values = query_parameters_to_url_encode_query_parameter_values($values);

          // Add the encoded query parameter values to the end of the path, only
          // if it actually has any values.
          $is_blank = empty($encoded_query_parameter_values) && !is_numeric($encoded_query_parameter_values);
          if (!$is_blank) {
            $query_parameters_components .= '/' . $key . '/' . $encoded_query_parameter_values;
          }

          // Unset the query parameter that was encoded in the URL, so it's not
          // included in the generated link.
          unset($options['query'][$key]);
        }

        // If there were any query parameters to encode, replace the original
        // path with a new path that contains the encoded query parameters.
        if (!empty($query_parameters_components)) {
          $path = $new_path . $query_parameters_components;
        }
      }
    }
  }
}