SonataAdminBundle custom rendering of text fields in list
Asked Answered
R

2

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.

Routinize answered 4/1, 2012 at 15:34 Comment(1)
You should add your own answer and accept it (after a small delay, I think its two days for self-accept) instead of editing your answer with the solution.Chrysoprase
O
26

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')
Overactive answered 12/11, 2012 at 6:29 Comment(1)
This didn't seam to do anything for me. No error, nothing. Maybe I missed something, but could not get it to work.Idioglossia
E
0

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,
        ]);
Elissaelita answered 26/12, 2018 at 10:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.