'EasyAdmin: Error on change formTypeOptions property field in twig
I've an User entity and EasyAdmin (EA) UserCrudController for manage to them. The User entity has active boolean field. And I want that field will be disabled for current user in admin interface. I got a working solution:
{% extends '@EasyAdmin/crud/index.html.twig' %}
{% block table_body %}
...
{% for field in entity.fields %}
{# disable active field for current uset #}
{% if is_granted('IS_AUTHENTICATED_FULLY') %}
{% if app.user.id == entity.instance.id and field.property == 'active' %}
{% set templatePath = 'admin/crud/field/_boolean_disabled.html.twig' %}
{% else %}
{% set templatePath = field.templatePath %}
{% endif %}
{% endif %}
<td data-label="{{ field.label|e('html_attr') }}" class="{{ field.property == sort_field_name ? 'sorted' }} text-{{ field.textAlign }} {{ field.cssClass }}" dir="{{ ea.i18n.textDirection }}">
{{ include(templatePath, { field: field, entity: entity }, with_context = false) }}
</td>
{% endfor %}
...
with override EA boolean template.
But I wouldn't want to override EA boolean template and only complete field.formTypeOptions by element 'disabled': 'true'
{% for field in entity.fields %}
{# disable active field for current uset #}
{% if is_granted('IS_AUTHENTICATED_FULLY') %}
{% if app.user.id == entity.instance.id and field.property == 'active' %}
{% set field.formTypeOptions = field.formTypeOptions|merge({'disabled': 'true'}) %}
{% endif %}
{% endif %}
<td data-label="{{ field.label|e('html_attr') }}" class="{{ field.property == sort_field_name ? 'sorted' }} text-{{ field.textAlign }} {{ field.cssClass }}" dir="{{ ea.i18n.textDirection }}">
{{ include(field.templatePath, { field: field, entity: entity }, with_context = false) }}
</td>
{% endfor %}
But for this path I get an error: "Uncaught PHP Exception Twig\Error\SyntaxError: "Unexpected token "punctuation" of value "." ("end of statement block" expected)." at /home/vagrant/code/blog.local/templates/admin/crud/user/index.html.twig line 27"
line 27: {% set field.formTypeOptions = field.formTypeOptions|merge({'disabled': 'true'}) %}
When I do:
{% set x = field.formTypeOptions|merge({'disabled': 'true'}) %}
{{ dump(x) }}
array:7 [▼
"required" => false
"row_attr" => array:1 [▶]
"attr" => array:1 [▶]
"label" => "Active"
"label_translation_parameters" => []
"label_attr" => array:1 [▶]
"disabled" => "true"
]
I get the required array, but I get the same error when I try to assign a new value
{% set field.formTypeOptions = field.formTypeOptions|merge({'disabled': 'true'}) %}
What am I doing wrong? Thanks
Solution 1:[1]
I think its because the merge function does not like punctuations. Try to set the value to a variable before:
Change:
{% set field.formTypeOptions = field.formTypeOptions|merge({'disabled': 'true'}) %}
To:
{% set options = field.formTypeOptions %}
{% set field.formTypeOptions = options|merge({'disabled': 'true'}) %}
Solution 2:[2]
You could already do that in UserCrudController and avoid that logic in Twig:
public function configureFields(string $pageName): iterable
{
// ...
// check user/roles
$isInputDisabled = true;
if($this->isGranted('ROLE_ADMIN')){
$isInputDisabled = false;
}
// ...
$active = BooleanField::new('active', 'Active')
->setFormTypeOption('disabled', $isInputDisabled);
// ...
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Quatsch |
Solution 2 |