Skip to content

Extending settings

User settings

You can add new preferences to the User settings menu in the Back Office.

To do so, create a setting class implementing two interfaces: ValueDefinitionInterface and FormMapperInterface.

In this example the class is located in AppBundle/Setting/Unit.php and enables the user to select their preference for metric or imperial unit systems.

 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
declare(strict_types=1);

namespace AppBundle\Setting;

use EzSystems\EzPlatformUser\UserSetting\FormMapperInterface;
use EzSystems\EzPlatformUser\UserSetting\ValueDefinitionInterface;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use EzSystems\EzPlatformAdminUi\UserSetting as AdminUiUserSettings;

class Unit implements ValueDefinitionInterface, FormMapperInterface
{
    public const METRIC_OPTION = 'metric';
    public const IMPERIAL_OPTION = 'imperial';

    public function getName(): string
    {
        return 'Unit';
    }

    public function getDescription(): string
    {
        return 'Choose between metric and imperial unit systems';
    }

    public function getDisplayValue(string $storageValue): string
    {
        switch($storageValue) {
            case self::METRIC_OPTION:
                return 'Metric';
            case self::IMPERIAL_OPTION:
                return 'Imperial';
            default:
                throw new InvalidArgumentException(
                    '$storageValue',
                    sprintf('There is no \'%s\' option', $storageValue)
                );
        }
    }

    public function getDefaultValue(): string
    {
        return 'metric';
    }

    public function mapFieldForm(FormBuilderInterface $formBuilder, AdminUiUserSettings\ValueDefinitionInterface $value): FormBuilderInterface
    {
        $choices = [
            'Metric' => self::METRIC_OPTION,
            'Imperial' => self::IMPERIAL_OPTION,
        ];

        return $formBuilder->create(
            'value',
            ChoiceType::class,
            [
                'multiple' => false,
                'required' => true,
                'label' => $this->getDescription(),
                'choices' => $choices,
            ]
        );
    }
}

Register the class as a service:

1
2
3
4
AppBundle\Setting\Unit:
    tags:
        - { name: ezplatform.admin_ui.user_setting.value, identifier: unit, priority: 50 }
        - { name: ezplatform.admin_ui.user_setting.form_mapper, identifier: unit }

You can order the settings in the User menu by setting their priority.

The value of the setting is accessible with ez_user_settings['unit'].

User settings edit templates

You can also define a template to be used when editing the given setting:

1
2
3
4
5
6
7
8
9
ezpublish:
    system:
        admin_group:
            user_settings_update_view:
                full:
                    unit:
                        template: AppBundle:user:settings/update_unit.html.twig
                        match:
                            Identifier: [ unit ]

The template must extend the @ezdesign/user/settings/update.html.twig template:

1
2
3
4
5
6
7
8
9
{% extends '@ezdesign/user/settings/update.html.twig' %}

{% block form %}
    {{ parent() }}
    <div class="alert ez-alert--info mt-4" role="alert">
        Imperial units are: yard, mile, pound, etc.</br>
        Metric units are: meter, kilometer, kilogram, etc.
    </div>
{% endblock %}