Search code examples
symfonyeasyadmin

Put value on null if an other value is null on createEntity or updateEntity in EasyAdmin


I'm working with Symfony and EasyAdmin.

Here is my render :

enter image description here

And here is the Indication code :

<?php

namespace App\Controller\Admin;

use App\Entity\Indication;
use Doctrine\ORM\EntityManagerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Field\ColorField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;

class IndicationCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return Indication::class;
    }

    public function configureFields(string $pageName): iterable
    {
        return [
            AssociationField::new('deficiencies', 'Déficience')
                ->formatValue(function ($value, $entity) {
                    return implode(", ",$entity->getDeficiencies()->toArray());
                })
                ->setTemplatePath('admin/associationTemplate.html.twig')
                ->setRequired(true),

            AssociationField::new('underActivities', 'Sous-activité')
                ->formatValue(function ($value, $entity) {
                    return implode(", ",$entity->getUnderActivities()->toArray());
                })
                ->setTemplatePath('admin/associationTemplate.html.twig')
                ->setRequired(true),

            ChoiceField::new('indicationPreAdjusting', 'Indice pré-aménagement')
                ->setChoices([
                    'Vert' => 'Green',
                    'Orange' => 'Orange',
                    'Rouge' => 'Red',
                    ])
                ->onlyOnForms(),
            ColorField::new('indicationPreAdjusting', 'Indice pré-aménagement')->hideOnForm(),

            AssociationField::new('adjustings', 'Aménagement')
                ->formatValue(function ($value, $entity) {
                    return implode(", ",$entity->getAdjustings()->toArray());
                })
                ->setTemplatePath('admin/associationTemplate.html.twig'),

            ChoiceField::new('indicationPostAdjusting', 'Indice post-aménagement')
                ->setChoices([
                    'Vert' => 'Green',
                    'Orange' => 'Orange',
                    'Rouge' => 'Red',
                ])
                ->onlyOnForms(),
            ColorField::new('indicationPostAdjusting', 'Indice post-aménagement')->hideOnForm(),
        ];
    }

    public function configureCrud(Crud $crud): Crud
    {
        return $crud
            ->setSearchFields(['deficiencies.name', 'underActivities.name'])
            ->setEntityLabelInSingular('Indication')
            ->setEntityLabelInPlural('Indications');
    }
}

I want if "adjustings" ("Aménagement", the penultimate column) is null, put the "indicationPostAdjusting" on null. Because now, I can put an indicationPostAdjusting even if there is no adjusting(s).

I know there is some solutions with EasyAdmin like createEntity, persistEntity and updateEntity, but I don't know how to use it in my case, do you have any ideas ?

Noé

-- EDIT --

I'm creating an EasyAdminSubscriber like :

<?php

namespace App\EventSubscriber;

use App\Entity\Indication;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class EasyAdminSubscriber implements EventSubscriberInterface
{

    public static function getSubscribedEvents()
    {
        return [
            BeforeEntityPersistedEvent::class => ['setIndicationPostAdjustingOnNullIfNoAdjustings'],
        ];
    }

    public function setIndicationPostAdjustingOnNullIfNoAdjustings(BeforeEntityPersistedEvent $event)
    {
        $entity = $event->getEntityInstance();

        if (!($entity instanceof Indication)) {
            return;
        }

        dump($entity->getAdjustings());
    }
}

If Adjustings is empty, I got this :

enter image description here

Else :

enter image description here

My new question is : How can I check if my array "elements" is empty or not ?

How can I make it pls ?


Solution

  • Not sure to understand what you are trying to achieve but sound like something Event can do, like AfterEntityUpdatedEvent, in easyadmin. It's a function which will trigger each time you update a specific entity.