1

I'm doing a project with Symfony2 and Sonata Admin Bundle. How I can apply the filter raw of twig (to display formated text) in action configureShowFields?

I would not override Sonata templates...

The code of my configureShowFields:

protected function configureShowFields(ShowMapper $showMapper)
    {
        $showMapper
            ->add('active')
            ->add('title')
            ->add('subtitle') // I need this field with twig RAW filter
            ->add('description') //I need this field with twig RAW filter
            ->add('url')
            ->add('date')
            ->add('tags')
            ->add('file');
    }
j0k
  • 22,600
  • 28
  • 79
  • 90
Mauro
  • 1,447
  • 1
  • 26
  • 46
  • See the similar question [SonataAdminBundle custom rendering of text fields in list](http://stackoverflow.com/q/8729439/2257664) for a simple solution. – A.L Oct 25 '14 at 23:00

2 Answers2

15

You can use the "safe" sonata field option as follow:

protected function configureShowFields(ShowMapper $showMapper)
{
    $showMapper
        ->add('subtitle', null, array('safe' => true))
    ;
}

It will add the "raw" twig filter to your entity field.

From the base_show_field.html.twig:

{% block field %}
    {% if field_description.options.safe %}
       {{ value|raw }}
    {% else %}
       {{ value|nl2br }}
    {% endif %}
{% endblock %}
William Vbl
  • 503
  • 3
  • 8
0

You need to make a custom template.

Under:

sonata_doctrine_orm_admin:
  templates:
    types:
      list:
        array:      SonataAdminBundle:CRUD:list_array.html.twig
        *** other existing declarations ***
        raw:        MyBundle:CRUD:raw.html.twig

Then make the template that the declaration maps to, and give 'raw' as the second argument to add field. It'll then call your new template to render that field.

benlumley
  • 11,370
  • 2
  • 40
  • 39
  • I've done: - Create a folder in app / Resources / SonataAdminBundle / views / CRUD - Create the file in the folder: base_show_field.html.twig In this file put: {% block name %}{{ admin.trans(field_description.label) }}{% endblock %} {% block field %}{{ value|raw }}{% endblock %} – Mauro May 21 '12 at 14:13
  • 1
    Think this means you are now escaping /everything/ - which I'm guessing is what you want. For everyone else - my suggestion above allows you to do it to some fields only. – benlumley May 23 '12 at 11:56