Skip to content

Customize search sorting

You can customize the Sort by menu in the Back Office search result page, for example, by adding new sort criteria.

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.

The following example class implements SortingDefinitionProviderInterface::getSortingDefinitions, and adds two definitions to sort by section name. A sorting definition contains an identifier, a menu label, a list of content search Sort Clauses, which could be either default or custom, and a priority value to position them in the menu. It also implements TranslationContainerInterface::getTranslationMessages to provide two default English translations in the ibexa_search namespace.

Create the src/Search/SortingDefinition/Provider/SectionNameSortingDefinitionProvider.php file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php declare(strict_types=1);

namespace App\Search\SortingDefinition\Provider;

use Ibexa\Contracts\Core\Repository\Values\Content\Query;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\SortClause;
use Ibexa\Contracts\Search\SortingDefinition\SortingDefinition;
use Ibexa\Contracts\Search\SortingDefinition\SortingDefinitionProviderInterface;
use JMS\TranslationBundle\Model\Message;
use JMS\TranslationBundle\Translation\TranslationContainerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

final class SectionNameSortingDefinitionProvider implements SortingDefinitionProviderInterface, TranslationContainerInterface
{
    private TranslatorInterface $translator;

    public function __construct(TranslatorInterface $translator)
    {
        $this->translator = $translator;
    }

    public function getSortingDefinitions(): array
    {
        return [
            new SortingDefinition(
                'section_asc',
                $this->translator->trans('sort_definition.section_name_asc.label'),
                [
                    new SortClause\SectionName(Query::SORT_ASC),
                ],
                333
            ),
            new SortingDefinition(
                'section_desc',
                $this->translator->trans('sort_definition.section_name_desc.label'),
                [
                    new SortClause\SectionName(Query::SORT_DESC),
                ],
                369
            ),
        ];
    }

    public static function getTranslationMessages(): array
    {
        return [
            (new Message('sort_definition.section_name_asc.label'))->setDesc('Sort by section A-Z'),
            (new Message('sort_definition.section_name_desc.label'))->setDesc('Sort by section Z-A'),
        ];
    }
}

Then add a service definition to config/services.yaml:

1
2
3
4
5
services:
    #…
    App\Search\SortingDefinition\Provider\SectionNameSortingDefinitionProvider:
        tags:
            - name: ibexa.search.sorting_definition.provider

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. You could also create it manually, as translations/messages.en.yaml file with the following contents:

1
2
sort_definition.section_name_asc.label: 'Sort by section A-Z'
sort_definition.section_name_desc.label: 'Sort by section Z-A'