function Sort::expandItem

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

Expands a sort item in case a shortcut was used.

Parameters

array $sort_item: The raw sort item.

Return value

array The expanded sort item.

1 call to Sort::expandItem()
Sort::createFromQueryParameter in core/modules/jsonapi/src/Query/Sort.php
Creates a Sort object from a query parameter.

File

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

Class

Sort
Gathers information about the sort parameter.

Namespace

Drupal\jsonapi\Query

Code

protected static function expandItem(array $sort_item) {
    $cacheability = (new CacheableMetadata())->addCacheContexts([
        'url.query_args:sort',
    ]);
    $defaults = [
        static::DIRECTION_KEY => 'ASC',
        static::LANGUAGE_KEY => NULL,
    ];
    if (!isset($sort_item[static::PATH_KEY])) {
        throw new CacheableBadRequestHttpException($cacheability, 'You need to provide a field name for the sort parameter.');
    }
    $expected_keys = [
        static::PATH_KEY,
        static::DIRECTION_KEY,
        static::LANGUAGE_KEY,
    ];
    $expanded = array_merge($defaults, $sort_item);
    // Verify correct sort keys.
    if (count(array_diff($expected_keys, array_keys($expanded))) > 0) {
        throw new CacheableBadRequestHttpException($cacheability, 'You have provided an invalid set of sort keys.');
    }
    return $expanded;
}

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