我发现了一种可能性:覆盖文本模板的 base_list_field.html.twig 模板:定义nl2br
块时添加过滤器:field
list_text_field.html.twig
<td class="sonata-ba-list-field sonata-ba-list-field-{{ field_description.type }}" objectId="{{ admin.id(object) }}">
{% if
field_description.options.identifier is defined
and field_description.options.route is defined
and admin.isGranted(field_description.options.route.name == 'show' ? 'VIEW' : field_description.options.route.name|upper, object)
and admin.hasRoute(field_description.options.route.name)
%}
<a href="{{ admin.generateObjectUrl(field_description.options.route.name, object, field_description.options.route.parameters) }}">
{%- block field %}{{ value|nl2br }}{% endblock -%}
</a>
{% else %}
{{ block('field') }}
{% endif %}
</td>
在你的config.yml
:
sonata_doctrine_orm_admin:
templates:
types:
list:
text: YourBundle:Admin:list_text_field.html.twig
configureListFields
$listMapper->add('test', 'text', array('label' => 'Test'))