Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. UrlHelper.php

function UrlHelper::buildQuery

Parses an array into a valid query string encoded with rawurlencode().

Function 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.

Parameters

array $query: The query parameter array to be processed; for instance, \Drupal::request()->query->all().

string $parent: (optional) Internal use only. Used to build the $query array key for nested items. Defaults to an empty string.

Return value

string A string encoded with rawurlencode() which can be used as or appended to the URL query string.

Related topics

PHP wrapper functions
Functions that are wrappers or custom implementations of PHP functions.
23 calls to UrlHelper::buildQuery()
BigPipeStrategy::generateBigPipePlaceholderId in core/modules/big_pipe/src/Render/Placeholder/BigPipeStrategy.php
Generates a BigPipe placeholder ID.
Cookie::addCheckToUrl in core/modules/user/src/Authentication/Provider/Cookie.php
Adds a query parameter to check successful log in redirect URL.
CssCollectionOptimizerLazy::optimize in core/lib/Drupal/Core/Asset/CssCollectionOptimizerLazy.php
Optimizes a collection of assets.
drupal_current_script_url in core/includes/install.inc
Returns the URL of the current script, with modified query parameters.
Endpoint::buildResourceUrl in core/modules/media/src/OEmbed/Endpoint.php
Builds and returns the endpoint URL.

... See full list

File

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

Class

UrlHelper
Helper class URL based methods.

Namespace

Drupal\Component\Utility

Code

public static function buildQuery(array $query, $parent = '') {
    $params = [];
    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);
}
RSS feed
Powered by Drupal