无法在 symfony sonata admin 的显示操作字段中设置自定义数据
Unable to set custom data in show action field in symfony sonata admin
我有一个展示页面,我想添加一个自定义值。
我已经尝试做我在其他操作中所做的,即添加一个数组到
第三个参数带有数据键,如下所示:
protected function configureShowFields(ShowMapper $showMapper)
{
$showMapper
->add('name')
->add('dateEnd')
->add('example', null,
array('data' => 'example value')
)
;
}
在 configureListFields 操作中,这有效。我已经使用数据属性注入了自定义值。
但我仍然无法访问 show.html.twig
文件中的关键示例。
它给了我这个错误
Variable "example" does not exist.
我应该如何访问 twig 文件中的这个自定义变量?
尝试
{{ elements.elements.example.options.data }}
在你的树枝模板中
我使用了这个解决方案。在 Admin class 的 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),
]
)
;
在自定义模板中,您可以通过 field_description.options.<customFieldName>
访问自定义数据,因此对于提供的示例数据访问器将是 {{ field_description.options.customData }}
和 {{ field_description.options.anotherCustomData }}
对于Twig模板中较短的字段名,你可以这样做:
{% set customData = field_description.options.customData %}
并访问自定义数据,例如 {{ customData }}
希望这有助于节省时间。
我有一个展示页面,我想添加一个自定义值。
我已经尝试做我在其他操作中所做的,即添加一个数组到 第三个参数带有数据键,如下所示:
protected function configureShowFields(ShowMapper $showMapper)
{
$showMapper
->add('name')
->add('dateEnd')
->add('example', null,
array('data' => 'example value')
)
;
}
在 configureListFields 操作中,这有效。我已经使用数据属性注入了自定义值。
但我仍然无法访问 show.html.twig
文件中的关键示例。
它给了我这个错误
Variable "example" does not exist.
我应该如何访问 twig 文件中的这个自定义变量?
尝试
{{ elements.elements.example.options.data }}
在你的树枝模板中
我使用了这个解决方案。在 Admin class 的 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),
]
)
;
在自定义模板中,您可以通过 field_description.options.<customFieldName>
访问自定义数据,因此对于提供的示例数据访问器将是 {{ field_description.options.customData }}
和 {{ field_description.options.anotherCustomData }}
对于Twig模板中较短的字段名,你可以这样做:
{% set customData = field_description.options.customData %}
并访问自定义数据,例如 {{ customData }}
希望这有助于节省时间。