Skip to content

Commit 16c6468

Browse files
adriendupuisadamwojsjulitafalconduszadabrtmnocon
committed
IBX-6878: Customize search sorting (#2226)
--------- Co-authored-by: Adrien Dupuis <[email protected]> Co-authored-by: Adam Wójs <[email protected]> Co-authored-by: julitafalcondusza <[email protected]> Co-authored-by: Tomasz Dąbrowski <[email protected]> Co-authored-by: Marek Nocoń <[email protected]> (cherry picked from commit 9d37154)
1 parent 9fae153 commit 16c6468

File tree

6 files changed

+94
-2
lines changed

6 files changed

+94
-2
lines changed

code_samples/back_office/search/config/append_to_services.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,7 @@ services:
2626
$groupIdentifier: 'global-search-autocomplete-product'
2727
tags:
2828
- { name: ibexa.admin_ui.component, group: global-search-autocomplete-templates }
29+
30+
App\Search\SortingDefinition\Provider\SectionNameSortingDefinitionProvider:
31+
tags:
32+
- name: ibexa.search.sorting_definition.provider
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace App\Search\SortingDefinition\Provider;
4+
5+
use Ibexa\Contracts\Core\Repository\Values\Content\Query;
6+
use Ibexa\Contracts\Core\Repository\Values\Content\Query\SortClause;
7+
use Ibexa\Contracts\Search\SortingDefinition\SortingDefinition;
8+
use Ibexa\Contracts\Search\SortingDefinition\SortingDefinitionProviderInterface;
9+
use JMS\TranslationBundle\Model\Message;
10+
use JMS\TranslationBundle\Translation\TranslationContainerInterface;
11+
use Symfony\Contracts\Translation\TranslatorInterface;
12+
13+
final class SectionNameSortingDefinitionProvider implements SortingDefinitionProviderInterface, TranslationContainerInterface
14+
{
15+
private TranslatorInterface $translator;
16+
17+
public function __construct(TranslatorInterface $translator)
18+
{
19+
$this->translator = $translator;
20+
}
21+
22+
public function getSortingDefinitions(): array
23+
{
24+
return [
25+
new SortingDefinition(
26+
'section_asc',
27+
$this->translator->trans('sort_definition.section_name_asc.label'),
28+
[
29+
new SortClause\SectionName(Query::SORT_ASC),
30+
],
31+
333
32+
),
33+
new SortingDefinition(
34+
'section_desc',
35+
$this->translator->trans('sort_definition.section_name_desc.label'),
36+
[
37+
new SortClause\SectionName(Query::SORT_DESC),
38+
],
39+
369
40+
),
41+
];
42+
}
43+
44+
public static function getTranslationMessages(): array
45+
{
46+
return [
47+
(new Message('sort_definition.section_name_asc.label'))->setDesc('Sort by section A-Z'),
48+
(new Message('sort_definition.section_name_desc.label'))->setDesc('Sort by section Z-A'),
49+
];
50+
}
51+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
sort_definition.section_name_asc.label: 'Sort by section A-Z'
2+
sort_definition.section_name_desc.label: 'Sort by section Z-A'
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
description: Add a "sort by" method to the Back Office search result page.
3+
---
4+
5+
# Customize search sorting
6+
7+
You can customize the **Sort by** menu in the Back Office search result page, for example, by adding new sort criteria.
8+
9+
To do it, you must create a service that implements the `Ibexa\Contracts\Search\SortingDefinition\SortingDefinitionProviderInterface` and tag it with `ibexa.search.sorting_definition.provider`.
10+
11+
The following example class implements `SortingDefinitionProviderInterface::getSortingDefinitions`, and adds two definitions to sort by section name.
12+
A sorting definition contains an identifier, a menu label, a list of content search Sort Clauses, which could be either [default](sort_clause_reference.md#sort-clauses) or [custom](create_custom_sort_clause.md), and a priority value to position them in the menu.
13+
It also implements `TranslationContainerInterface::getTranslationMessages` to provide two default English translations in the `ibexa_search` namespace.
14+
15+
Create the `src/Search/SortingDefinition/Provider/SectionNameSortingDefinitionProvider.php` file:
16+
17+
``` php hl_lines="22"
18+
[[= include_file('code_samples/back_office/search/src/Search/SortingDefinition/Provider/SectionNameSortingDefinitionProvider.php') =]]
19+
```
20+
21+
Then add a service definition to `config/services.yaml`:
22+
23+
``` yaml hl_lines="5"
24+
services:
25+
#
26+
[[= include_file('code_samples/back_office/search/config/append_to_services.yaml', 29, 32) =]]
27+
```
28+
29+
You can extract a translation file with the `translation:extract` command, for example, `php bin/console translation:extract en --dir=src --output-dir=translations` to obtain the `translations/ibexa_search.en.xlf` file.
30+
You could also create it manually, as `translations/messages.en.yaml` file with the following contents:
31+
32+
``` yaml
33+
[[= include_file('code_samples/back_office/search/translations/messages.en.yaml') =]]
34+
```

docs/administration/back_office/customize_search_suggestion.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ To be present, this wrapping node template must be added to the `global-search-a
139139
``` yaml
140140
services:
141141
#…
142-
[[= include_file('code_samples/back_office/search/config/append_to_services.yaml', 22, 28) =]]
142+
[[= include_file('code_samples/back_office/search/config/append_to_services.yaml', 21, 28) =]]
143143
```
144144

145145
The template for the product suggestion item follows, named `templates/themes/admin/ui/global_search_autocomplete_product_item.html.twig`:

mkdocs.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ nav:
146146
- Sub-items list: administration/back_office/subitems_list.md
147147
- Notifications: administration/back_office/notifications.md
148148
- Customize search suggestion: administration/back_office/customize_search_suggestion.md
149+
- Customize search sorting: administration/back_office/customize_search_sorting.md
149150
- Content management:
150151
- Content management: content_management/content_management.md
151152
- Content management guide: content_management/content_management_guide.md
@@ -290,7 +291,7 @@ nav:
290291
- Storefront Twig functions: templating/twig_function_reference/storefront_twig_functions.md
291292
- URL Twig functions: templating/twig_function_reference/url_twig_functions.md
292293
- User Twig functions: templating/twig_function_reference/user_twig_functions.md
293-
- Other Twig filters: templating/twig_function_reference/other_twig_filters.md
294+
- Other Twig filters: templating/twig_function_reference/other_twig_filters.md
294295
- URLs and routes:
295296
- URLs and routes: templating/urls_and_routes/urls_and_routes.md
296297
- Custom breadcrumbs: templating/urls_and_routes/custom_breadcrumbs.md

0 commit comments

Comments
 (0)