You are here

public static function UrlHelper::buildQuery in Zircon Profile 8

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

Parses an array into a valid, rawurlencoded query string.

rawurlencode() is RFC3986 compliant, and as a consequence RFC3987 compliant. The latter defines the required format of "URLs" in HTML5. urlencode() is almost the same as rawurlencode(), except that it encodes spaces as "+" instead of "%20". This makes its result non compliant to RFC3986 and as a consequence non compliant to RFC3987 and as a consequence not valid as a "URL" in HTML5.

@todo Remove this function once PHP 5.4 is required as we can use just http_build_query() directly.

Parameters

array $query: The query parameter array to be processed, e.g. \Drupal::request()->query->all().

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

Return value

string A rawurlencoded string which can be used as or appended to the URL query string.

Related topics

20 calls to UrlHelper::buildQuery()
CustomPageExceptionHtmlSubscriberTest::testHandleWithGetRequest in core/tests/Drupal/Tests/Core/EventSubscriber/CustomPageExceptionHtmlSubscriberTest.php
Tests onHandleException with a GET request.
CustomPageExceptionHtmlSubscriberTest::testHandleWithPostRequest in core/tests/Drupal/Tests/Core/EventSubscriber/CustomPageExceptionHtmlSubscriberTest.php
Tests onHandleException with a POST request.
drupal_current_script_url in core/includes/install.inc
Returns the URL of the current script, with modified query parameters.
FieldPluginBase::renderAsLink in core/modules/views/src/Plugin/views/field/FieldPluginBase.php
Render this field as a link, with the info from a fieldset set by the user.
file_create_url in core/includes/file.inc
Creates a web-accessible URL for a stream to an external or local file.

... See full list

File

core/lib/Drupal/Component/Utility/UrlHelper.php, line 50
Contains \Drupal\Component\Utility\UrlHelper.

Class

UrlHelper
Helper class URL based methods.

Namespace

Drupal\Component\Utility

Code

public static function buildQuery(array $query, $parent = '') {
  $params = array();
  foreach ($query as $key => $value) {
    $key = $parent ? $parent . '[' . rawurlencode($key) . ']' : rawurlencode($key);

    // Recurse into children.
    if (is_array($value)) {
      $params[] = static::buildQuery($value, $key);
    }
    elseif (!isset($value)) {
      $params[] = $key;
    }
    else {

      // For better readability of paths in query strings, we decode slashes.
      $params[] = $key . '=' . str_replace('%2F', '/', rawurlencode($value));
    }
  }
  return implode('&', $params);
}