SonataAdminBundle custom rendering of text fields in list SonataAdminBundle custom rendering of text fields in list symfony symfony

SonataAdminBundle custom rendering of text fields in list


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')


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,        ]);