- Documentation >
- Administration >
- Back Office >
- Add user setting
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 | <?php declare(strict_types=1);
namespace App\Setting;
use Ibexa\Contracts\User\UserSetting\FormMapperInterface;
use Ibexa\Contracts\User\UserSetting\ValueDefinitionInterface;
use Ibexa\Core\Base\Exceptions\InvalidArgumentException;
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:
| services:
App\Setting\Unit:
tags:
- { name: ibexa.user.setting.value, identifier: unit, group: my_group, priority: 50 }
- { name: ibexa.user.setting.mapper.form, identifier: unit }
|
You can order the settings in the User menu by setting their priority
.
group
indicates the group that the setting is placed in.
It can be one of the built-in groups, or a custom one.
To create a custom setting group, create an App\Setting\Group\MyGroup.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 | <?php declare(strict_types=1);
namespace App\Setting\Group;
use Ibexa\User\UserSetting\Group\AbstractGroup;
final class MyGroup extends AbstractGroup
{
public function __construct(
array $values = []
) {
parent::__construct($values);
}
public function getName(): string
{
return 'My Group';
}
public function getDescription(): string
{
return 'My custom setting group';
}
}
|
Register the setting group as a service:
| App\Setting\Group\MyGroup:
tags:
- { name: ibexa.user.setting.group, identifier: my_group, priority: 30 }
|
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
under the ibexa.system.<scope>.user_settings_update_view
configuration key:
| ibexa:
system:
admin_group:
user_settings_update_view:
full:
unit:
template: '@ibexadesign/user/setting/update_unit.html.twig'
match:
Identifier: [ unit ]
|
The templates/themes/admin/user/setting/update_unit.html.twig
template must extend the @ibexadesign/account/settings/update.html.twig
template:
| {% extends '@ibexadesign/account/settings/update.html.twig' %}
{% block form %}
{{ parent() }}
<div class="alert ibexa-alert--info mt-4" role="alert">
Imperial units are: yard, mile, pound, etc.</br>
Metric units are: meter, kilometer, kilogram, etc.
</div>
{% endblock %}
|