Bonjour,

Je cherche à insérer la date de création dans mon crud et cela fonctionne bien avec mon entity User mais pas Article si vous pourriez me mettre sur la voie, je vous en serais très reconnaissant.

Je cherche aussi à nettoyer le html dans le crud mais je n'y arrive pas dans le twig je fais un filtre | raw mais là j'essaie avec: TextEditorField::new('content', \ENT_COMPAT | \ENT_HTML5)->setLabel('content'), mais rien n'y fait.

Voici mes fonctions en question:

<?php

namespace App\EventSubscriber;

use App\Entity\Article;
use App\Entity\Test;
use App\Entity\User;
use DateTimeImmutable;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Security;

Class EasyAdminSubscriber implements EventSubscriberInterface
{

    public static function getSubscribedEvents()
    {
        return [
            BeforeEntityPersistedEvent::class => ['setArticleDate'],
            BeforeEntityPersistedEvent::class => ['setUSerDate'],
        ];
    }
    public function setArticleDate(BeforeEntityPersistedEvent $event)
    {
        $entity = $event->getEntityInstance();
        if(!($entity instanceof Article)){
            return;
        }
        $now = new DateTimeImmutable('now');
        $entity->setCreatedAt($now);
    }
    public function setUserDate(BeforeEntityPersistedEvent $event)
    {
        $entity = $event->getEntityInstance();
        if(!($entity instanceof User)){
            return;
        }
        $now = new DateTimeImmutable('now');

        $entity->setCreatedAt($now);
    }
}

et mon crud:

<?php

namespace App\Controller\Admin;

use App\Entity\Article;
use DateTimeImmutable;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextEditorField;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Config\Filters;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Field\ArrayField;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ImageField;
use EasyCorp\Bundle\EasyAdminBundle\Field\SlugField;
use FOS\CKEditorBundle\Form\Type\CKEditorType;

class ArticleCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return Article::class;
    }

    public function configureCrud(Crud $crud): Crud
    {
        return $crud
        ->setPageTitle(Crud::PAGE_INDEX, 'Liste des article')
        ->setPageTitle(Crud::PAGE_EDIT, 'Modifier un article')
        ->setPageTitle(Crud::PAGE_NEW, 'Créer un article')
        ->setPageTitle(Crud::PAGE_DETAIL, 'Consulter un article')

        ->setDefaultSort(['createdAt' => 'DESC']);
    }

    public function configureActions(Actions $actions): Actions{
        return $actions
        ->add(Crud::PAGE_INDEX, Action::DETAIL)
        ->update(Crud::PAGE_INDEX,Action::NEW,function(Action $action){
            return $action->setIcon('fas fa-newspaper pe-1')->setLabel('Ajouter un article');
        })
        ->update(Crud::PAGE_INDEX,Action::EDIT,function(Action $action){
            return $action->setIcon('fas fa-edit text-warning')->setLabel('Editer')->addCssClass('text-dark');
        })
        ->update(Crud::PAGE_INDEX,Action::DELETE,function(Action $action){
            return $action->setIcon('fas fa-trash text-danger')->setLabel('Supprimer')->addCssClass('text-dark');
        })
        ->update(Crud::PAGE_INDEX,Action::DETAIL,function(Action $action){
            return $action->setIcon('fas fa-eye text-info')->setLabel('Consulter')->addCssClass('text-dark');
        });
    }

    public function configureFields(string $pagetitle): iterable
    {

        return [
            IdField::new('id')->hideOnForm(),
            AssociationField::new('category')->setLabel('Catégorie(s)')->setTemplatePath('Admin/field/category.html.twig')->hideOnIndex()->setRequired(true),
            DateTimeField::new('createdAt')->hideOnForm()->setLabel('Créé le')->setRequired(true),
            DateTimeField::new('updatedAt')->setLabel('Modifié le')->hideOnForm()->setRequired(true),
            AssociationField::new('author')->setLabel('Auteur'),
            TextField::new('title')->setLabel('Titre'),
            SlugField::new('slug')->setTargetFieldName('title')->setLabel('Slug')->hideOnIndex(),
            TextEditorField::new('content', \ENT_COMPAT | \ENT_HTML5)->setLabel('content'),
            TextField::new('tags')->hideOnIndex()->setRequired(true),
            BooleanField::new('published')->setLabel('Publié'),
            // BooleanField::new('portcategory')->setLabel('Indexé'),
            ImageField::new('image')->setBasePath('assets/uploads/articles/')
                                    ->setUpLoadDir('public/assets/uploads/articles/')->setLabel('Image')
                                    ->setUploadedFileNamePattern('[randomhash].[extension]')
                                    ->setRequired(false)
        ];
    }

    public function configureFilters(Filters $filters): Filters
    {
        return $filters
        ->add('title')
        ->add('content')
        ->add('published');
        // ->add('portcategory');
    }

}

Merci de votre aide

Aucune réponse