function Sort::createFromQueryParameter

Same name in other branches
  1. 9 core/modules/jsonapi/src/Query/Sort.php \Drupal\jsonapi\Query\Sort::createFromQueryParameter()
  2. 8.9.x core/modules/jsonapi/src/Query/Sort.php \Drupal\jsonapi\Query\Sort::createFromQueryParameter()
  3. 10 core/modules/jsonapi/src/Query/Sort.php \Drupal\jsonapi\Query\Sort::createFromQueryParameter()

Creates a Sort object from a query parameter.

Parameters

mixed $parameter: The `sort` query parameter from the Symfony request object.

Return value

self A Sort object with defaults.

3 calls to Sort::createFromQueryParameter()
EntityResource::getJsonApiParams in core/modules/jsonapi/src/Controller/EntityResource.php
Extracts JSON:API query parameters from the request.
SortTest::testCreateFromQueryParameter in core/modules/jsonapi/tests/src/Unit/Query/SortTest.php
@covers ::createFromQueryParameter @dataProvider parameterProvider
SortTest::testCreateFromQueryParameterFail in core/modules/jsonapi/tests/src/Unit/Query/SortTest.php
@covers ::createFromQueryParameter @dataProvider badParameterProvider

File

core/modules/jsonapi/src/Query/Sort.php, line 93

Class

Sort
Gathers information about the sort parameter.

Namespace

Drupal\jsonapi\Query

Code

public static function createFromQueryParameter($parameter) {
    if (empty($parameter)) {
        $cacheability = (new CacheableMetadata())->addCacheContexts([
            'url.query_args:sort',
        ]);
        throw new CacheableBadRequestHttpException($cacheability, 'You need to provide a value for the sort parameter.');
    }
    // Expand a JSON:API compliant sort into a more expressive sort parameter.
    if (is_string($parameter)) {
        $parameter = static::expandFieldString($parameter);
    }
    // Expand any defaults into the sort array.
    $expanded = [];
    foreach ($parameter as $sort_index => $sort_item) {
        $expanded[$sort_index] = static::expandItem($sort_item);
    }
    return new static($expanded);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.