Search code examples
text-editorsymfony5easyadmin

View TextEditorField as html in detail modal in easyadmin


I use EasyAdmin to manage the back office of my site created with Symfony 5.

One of my entities has a TextEditorField type field that I use with CKEditorType for forms. In the forms no problem I can do my formatting normally, however in the display board, the "View content" modal displays the raw text and not converted to HTML.

I would like to keep the modal system to see more, is it possible to see the HTML text in this modal? Here is the field declared in my crud

TextEditorField::new('contenu', 'Descriptif')->setFormType(CKEditorType::class)->onlyOnForms(),
TextEditorField::new('contenu', 'Descriptif')->hideOnForm(),

see the table see the modal

Thank you for your help

I tried to use the TextareaField type because it can display as html (renderAsHtml) but my content is too long and I need to see it in a pop-up and not in the table.


Solution

  • I finally found the answer to my problem, so I'm sharing here before closing.

    I looked in the bundle for the template that displays the details and made a custom template by replacing "nl2br" with "raw".

    Here is the path to the file I created: templates\bundles\EasyAdminBundle\crud\field\text_editor.html.twig

    {# @var ea \EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext #}
    {# @var field \EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto #}
    {# @var entity \EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto #}
    {% if ea.crud.currentAction == 'detail' %}
        {{ field.formattedValue|raw }}
    {% else %}
        {% set html_id = 'ea-text-editor-' ~ field.uniqueId %}
        <a href="#" data-bs-toggle="modal" data-bs-target="#{{ html_id }}">
            <i class="far fa-file-alt"></i> {{ 'field.text_editor.view_content'|trans([], domain = 'EasyAdminBundle') }}
        </a>
    
        <div class="modal fade" id="{{ html_id }}" tabindex="-1" role="dialog" aria-hidden="true">
            <div class="modal-dialog modal-lg" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">{{ field.label }}</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="{{ 'action.close'|trans([], domain = 'EasyAdminBundle') }}">
                        </button>
                    </div>
                    <div class="modal-body">
                        {{ field.formattedValue|raw }}
                    </div>
                </div>
            </div>
        </div>
    {% endif %}
    

    Have a good day !