Mauro
Mauro

Reputation: 1487

Raw filter on Sonata Admin Bundle configureShowFields

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

Upvotes: 1

Views: 5286

Answers (2)

William Vbl
William Vbl

Reputation: 503

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 %}

Upvotes: 15

benlumley
benlumley

Reputation: 11382

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.

Upvotes: 0

Related Questions