Skip to content
Snippets Groups Projects
Commit c9acfd8b authored by Jonathan Sacksick's avatar Jonathan Sacksick Committed by Matt Glaman
Browse files

Issue #3122594 by jsacksick: Support "special" Search API sort fields

parent eabe0e93
No related branches found
No related tags found
No related merge requests found
......@@ -167,17 +167,8 @@ final class IndexResource extends EntityResourceBase implements ContainerInjecti
$sort_params = $request->query->get('sort');
$sort = Sort::createFromQueryParameter($sort_params);
$fields = array_keys($query->getIndex()->getFields());
foreach($sort->fields() as $field) {
$direction = $field[Sort::DIRECTION_KEY] ?? 'ASC';
$path = $field[Sort::PATH_KEY];
if (!in_array($path, $fields)) {
throw new CacheableBadRequestHttpException($cacheability, sprintf('The %s field is not sortable.', $path));
}
$query->sort($path, $direction);
foreach ($sort->fields() as $field) {
$query->sort($field['path'], $field['direction']);
}
}
......
......@@ -341,13 +341,29 @@ final class IndexResourceTest extends BrowserTestBase {
[5, 4, 3, 2, 1],
[],
];
yield [
[
'sort' => 'search_api_id',
],
5,
[1, 2, 3, 4, 5],
[],
];
yield [
[
'sort' => '-search_api_id',
],
5,
[5, 4, 3, 2, 1],
[],
];
yield [
[
'sort' => [
'sort-id' => [
'path' => 'id',
'direction' => 'ASC',
]
],
],
],
5,
......@@ -360,7 +376,7 @@ final class IndexResourceTest extends BrowserTestBase {
'sort-id' => [
'path' => 'id',
'direction' => 'DESC',
]
],
],
],
5,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment