EasyCorp / EasyAdminBundle

EasyAdmin is a fast, beautiful and modern admin generator for Symfony applications.
MIT License
4.05k stars 1.02k forks source link

AssociationField - How to render html in __toString function ? #6159

Open FlorianGuiot opened 7 months ago

FlorianGuiot commented 7 months ago

Hi, I don't know if this is the right place to ask my question, but I'll give it a try.

I hadn't updated EasyAdmin for 1 year. When I updated, a feature disappeared and I don't know how to get it back.

There are articles on my website. Each article is linked to a "Media" object, which is basically an illustration photo. When you create or edit an article, you have to choose a photo. So I've created an auto-complete AssociationField that displays the list of photos.

As I couldn't find any other way of changing the way the media was displayed in the drop-down list, I redefined Media's toString() method to display HTML code with the photo and title. (It's easier to choose an image when you see it)

It worked! But after the update, the HTML code is no longer interpreted.

AssociationField in ArticleCrudController.php :

yield AssociationField::new('Miniature')
            ->autocomplete()
            ->setCrudController(MediaCrudController::class)
            ->setHelp('Pour définir la miniature de l\'article, tu dois d\'abord l\'importer dans l\'onglet Médias > Médiathèque. Puis rechercher son nom ici.')
            ->addCssFiles('css/admin/field.css')
            ->setFormTypeOptions(['label_html' => true])
            ->setFormTypeOption('attr', ['class' => 'miniature_field','placeholder' => 'Rechercher une miniature par nom'])
            ->setTemplatePath('bundles/easy_admin/medias/miniature.html.twig')
        ;

Media's __toString :

public function __toString(): string
    {

        return '<img class="mx-3" width="100" src="img/galeries/'.$this->lien.'" />'. $this->nom . " - " . $this->datePublication->format('d/m/Y H:i:s');

    }

Render: media

Yuubitsume commented 7 months ago

Hello Florian, easyAdmin is very annoying when you want to do specific stuff. I don't know how you can do. I Hope you'll find soon.

dwd-akira commented 7 months ago

It's not a specific stuff, it's a regression (v4.6.6 works) #6122

Troi commented 1 month ago

This is wrong proposal. EA fields needs "renderer" callback to define how it should be rendered differently on different places. Need to have same render for entity in all places is wrong.