[英]Unable to set custom data in show action field in symfony sonata admin
I have a show page and I want to add a custom value.我有一个显示页面,我想添加一个自定义值。
I have tried doing what I did in other actions which is to add an array to the third parameter with the data key like so:我尝试做我在其他操作中所做的事情,即使用 data 键将数组添加到第三个参数,如下所示:
protected function configureShowFields(ShowMapper $showMapper)
{
$showMapper
->add('name')
->add('dateEnd')
->add('example', null,
array('data' => 'example value')
)
;
}
In the configureListFields action, this works.在 configureListFields 操作中,这是有效的。 I have injected custom values with the data attribute.
我已经使用 data 属性注入了自定义值。
But still I am not able to access key example in the show.html.twig
file.但是我仍然无法访问
show.html.twig
文件中的关键示例。 It gives me this error它给了我这个错误
Variable "example" does not exist.
变量“示例”不存在。
What should I do to access this custom variable in the twig file ?我应该怎么做才能访问树枝文件中的这个自定义变量?
Try尝试
{{ elements.elements.example.options.data }}
in your twig template在你的树枝模板中
I used this solution.我使用了这个解决方案。 In the
configureShowFields()
method of an Admin class:在 Admin 类的
configureShowFields()
方法中:
$showMapper
->with('Tab Name')
->add(
'any_name',
null,
[
'template' => 'Admin/Custom/any_name_show_template.html.twig',
'customData' => $this->someRepository->getSomeEntityBy($field),
'anotherCustomData' => $this->someService->getSomeDataBy($value),
]
)
;
In the custom template, you can access custom data by field_description.options.<customFieldName>
, so for provided example data accessors would be {{ field_description.options.customData }}
and {{ field_description.options.anotherCustomData }}
在自定义模板中,您可以通过
field_description.options.<customFieldName>
访问自定义数据,因此对于提供的示例数据访问器将是{{ field_description.options.customData }}
和{{ field_description.options.anotherCustomData }}
For the shorter field name in the Twig template, you can do like this:对于 Twig 模板中较短的字段名称,您可以这样做:
{% set customData = field_description.options.customData %}
and access the custom data like {{ customData }}
并访问自定义数据,如
{{ customData }}
Hope this helps and saves time.希望这会有所帮助并节省时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.