function Sort::expandFieldString

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

Expands a simple string sort into a more expressive sort that we can use.

Parameters

string $fields: The comma separated list of fields to expand into an array.

Return value

array The expanded sort.

1 call to Sort::expandFieldString()
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 122

Class

Sort
Gathers information about the sort parameter.

Namespace

Drupal\jsonapi\Query

Code

protected static function expandFieldString($fields) {
    return array_map(function ($field) {
        $sort = [];
        if ($field[0] == '-') {
            $sort[static::DIRECTION_KEY] = 'DESC';
            $sort[static::PATH_KEY] = substr($field, 1);
        }
        else {
            $sort[static::DIRECTION_KEY] = 'ASC';
            $sort[static::PATH_KEY] = $field;
        }
        return $sort;
    }, explode(',', $fields));
}

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