Skip to content

Add user setting

Create new User setting

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 src/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
<?php
declare(strict_types=1);

namespace App\Setting;

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

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, 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 setting as a service:

1
2
3
4
5
services:
    App\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'].

Create template for editing settings

You can override a template used when editing the new setting:

1
2
3
4
5
6
7
8
9
ezplatform:
    system:
        admin_group:
            user_settings_update_view:
                full:
                    unit:
                        template: User/Setting/update_unit.html.twig
                        match:
                            Identifier: [ unit ]

The templates/User/Setting/update_unit.html.twig template must extend the @ezdesign/account/settings/update.html.twig template:

1
2
3
4
5
6
7
8
9
{% extends '@ezdesign/account/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 %}