【问题标题】:EasyAdmin: Error on change formTypeOptions property field in twigEasyAdmin:更改树枝中的 formTypeOptions 属性字段时出错
【发布时间】:2022-01-01 19:05:21
【问题描述】:

我有一个 User 实体和 EasyAdmin (EA) UserCrudController 来管理他们。 User 实体具有 active 布尔字段。我希望在管理界面中为当前用户禁用该字段。 我有一个可行的解决方案:

{% 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 %}
...

使用覆盖 EA 布尔模板。

但我不想覆盖 EA 布尔模板,只通过元素 'disabled': 'true'

完成 field.formTypeOptions
{% 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 %}

但是对于这条路径我得到一个错误:“Uncaught PHP Exception Twig\Error\SyntaxError:”Unexpected token “punctuation” of value “。” (“语句块结束”预期)。”在 /home/vagrant/code/blog.local/templates/admin/crud/user/index.html.twig 第 27 行”

第 27 行:{% set field.formTypeOptions = field.formTypeOptions|merge({'disabled': 'true'}) %}

当我这样做时:

{% 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"
]

我得到了所需的数组,但是当我尝试分配一个新值时,我得到了同样的错误

{% set field.formTypeOptions = field.formTypeOptions|merge({'disabled': 'true'}) %}

我做错了什么?谢谢

【问题讨论】:

  • 我只是想让您知道我很高兴看到正确处理缩写。第一次使用长格式,后面括号中的缩写。然后从那里开始使用缩写。我希望每个人都这样做。赞一个!

标签: symfony twig easyadmin


【解决方案1】:

我认为这是因为合并功能不喜欢标点符号。尝试将值设置为之前的变量:

变化:

{% set field.formTypeOptions = field.formTypeOptions|merge({'disabled': 'true'}) %}

收件人:

   {% set options = field.formTypeOptions %}
   {% set field.formTypeOptions = options|merge({'disabled': 'true'}) %}

【讨论】:

  • 感谢 Quatsch!我以这种方式进行了测试,但在网上遇到了同样的错误: {% set field.formTypeOptions = options|merge({'disabled': 'true'}) %}
猜你喜欢
  • 1970-01-01
  • 2011-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-07
  • 1970-01-01
  • 2018-06-22
相关资源
最近更新 更多