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');
    }
1

2 Answers 2

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 %}
1
  • 1
    that's the best and easy answer
    – Cassiano
    Mar 22, 2016 at 13:57
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.

2
  • 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: <th>{% block name %}{{ admin.trans(field_description.label) }}{% endblock %}</th> <td>{% block field %}{{ value|raw }}{% endblock %}</td>
    – Mauro
    May 21, 2012 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, 2012 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.