Sonata Admin Bundle configureShowFields 上的原始过滤器过滤器、原始、Admin、Sonata

2023-09-07 00:44:06 作者:杀我别用感情刀

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

解决方案

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