You are here

public static function UrlHelper::filterQueryParameters in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Component/Utility/UrlHelper.php \Drupal\Component\Utility\UrlHelper::filterQueryParameters()

Filters a URL query parameter array to remove unwanted elements.

Parameters

array $query: An array to be processed.

array $exclude: (optional) A list of $query array keys to remove. Use "parent[child]" to exclude nested items.

string $parent: Internal use only. Used to build the $query array key for nested items.

Return value

An array containing query parameters.

7 calls to UrlHelper::filterQueryParameters()
drupal_current_script_url in core/includes/install.inc
Returns the URL of the current script, with modified query parameters.
PagerParameters::getQueryParameters in core/lib/Drupal/Core/Pager/PagerParameters.php
Gets all request URL query parameters that are unrelated to paging.
RedirectDestination::get in core/lib/Drupal/Core/Routing/RedirectDestination.php
Gets the destination as a path.
TableSort::getQueryParameters in core/lib/Drupal/Core/Utility/TableSort.php
Composes a URL query parameter array for table sorting links.
UrlHelperTest::testFilterQueryParameters in core/tests/Drupal/Tests/Component/Utility/UrlHelperTest.php
Tests query filtering.

... See full list

File

core/lib/Drupal/Component/Utility/UrlHelper.php, line 79

Class

UrlHelper
Helper class URL based methods.

Namespace

Drupal\Component\Utility

Code

public static function filterQueryParameters(array $query, array $exclude = [], $parent = '') {

  // If $exclude is empty, there is nothing to filter.
  if (empty($exclude)) {
    return $query;
  }
  elseif (!$parent) {
    $exclude = array_flip($exclude);
  }
  $params = [];
  foreach ($query as $key => $value) {
    $string_key = $parent ? $parent . '[' . $key . ']' : $key;
    if (isset($exclude[$string_key])) {
      continue;
    }
    if (is_array($value)) {
      $params[$key] = static::filterQueryParameters($value, $exclude, $string_key);
    }
    else {
      $params[$key] = $value;
    }
  }
  return $params;
}