SonataAdminBundle custom rendering of text fields in list

荒凉一梦 提交于 2019-12-17 18:11:43

问题


I'm using symfony2 and SonataAdminBundle. I have a simple Entity called Post in which I have content field that is basically html text (from a ckeditor for the record). I need to display in the Post list the content field as raw html, without escaping it. Hacking base_list_field template like this

{% block field %}{{ value|raw }}{% endblock %}

works, but it's clearly not the proper way. Any hints?

edit: SOLVED! I've defined a custom html type in the config.yml for sonata_doctrine_orm_admin:

sonata_doctrine_orm_admin:
    templates:
      types:
        list:
          html: MyBundle:Default:list_html.html.twig

And created the custom list_html.html.twig template in which i do not escape HTML:

{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}

{% block field%}
    {{value|raw}}
{% endblock %}

Now in the PostAdmin I can define the behaviour of the field in the configureListFields method:

$listMapper
    ->add('content', 'html')

回答1:


The solution:

I've defined a custom html type in the config.yml for sonata_doctrine_orm_admin:

sonata_doctrine_orm_admin:
    templates:
      types:
        list:
          html: MyBundle:Default:list_html.html.twig

And created the custom list_html.html.twig template in which i do not escape HTML:

{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}

{% block field%}
    {{value|raw}}
{% endblock %}

Now in the PostAdmin I can define the behaviour of the field in the configureListFields method:

$listMapper
    ->add('content', 'html')



回答2:


I know it's an old post that has an accepted answer, but now you can also use the safe option to tell Symfony not to sanitize the output.

$mapper->add('content', null, [
            'safe' => true,
        ]);


来源:https://stackoverflow.com/questions/8729439/sonataadminbundle-custom-rendering-of-text-fields-in-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!