2

Installed easyadminbundle with symfony 4, configured for an entity name Delivery and it has a field associated to another entity name WeeklyMenu:

easy_amin.yaml:

Delivery:
...
      form:
        fields:
          - { property: 'delivered'}
          - { property: 'weeklyMenu', type: 'choice', type_options: { choices: null }}

I need a dynamically filtered results of weeklyMenu entity here, so I can get a list of the next days menus and so on. It's set to null now but have to get a filtered result here.

I've read about overriding the AdminController which I stucked with it. I believe that I have to override easyadmin's query builder that listing an associated entity's result.

2 Answers 2

4

i've figured out, here is the solution if someone looking for:

namespace App\Controller;

use Doctrine\ORM\EntityRepository;
use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\FormBuilder;

class AdminController extends EasyAdminController {

  public function createDeliveryEntityFormBuilder($entity, $view) {
    $formBuilder = parent::createEntityFormBuilder($entity, $view);
    $fields = $formBuilder->all();
    /**
     * @var  $fieldId string
     * @var  $field FormBuilder
     */
    foreach ($fields as $fieldId => $field) {
      if ($fieldId == 'weeklyMenu') {
        $options = [
            'attr'     => ['size' => 1,],
            'required' => true,
            'multiple' => false,
            'expanded' => false,
            'class'    => 'App\Entity\WeeklyMenu',
        ];
        $options['query_builder'] = function (EntityRepository $er) {
          $qb = $er->createQueryBuilder('e');

          return $qb->where($qb->expr()->gt('e.date', ':today'))
                    ->setParameter('today', new \DateTime("today"))
                    ->andWhere($qb->expr()->eq('e.delivery', ':true'))
                    ->setParameter('true', 1)
                    ->orderBy('e.date', 'DESC');
        };
        $formBuilder->add($fieldId, EntityType::class, $options);
      }
    }

    return $formBuilder;
  }
}

so the easyAdmin check if a formbuilder exists with the entity's name i.e. create<ENTITYNAME>FormBuilder(); and you can override here with your own logic.

Sign up to request clarification or add additional context in comments.

Comments

2

Another approach to this would be to create new FormTypeConfigurator and overwrite choices and/or labels. And tag it as:

App\Form\Type\Configurator\UserTypeConfigurator:
    tags: ['easyadmin.form.type.configurator']

and the configurator looks like this:

<?php
declare(strict_types = 1);

namespace App\Form\Type\Configurator;

use App\Entity\User;
use EasyCorp\Bundle\EasyAdminBundle\Form\Type\Configurator\TypeConfiguratorInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormConfigInterface;

final class UserTypeConfigurator implements TypeConfiguratorInterface
{
    /**
     * {@inheritdoc}
     */
    public function configure($name, array $options, array $metadata, FormConfigInterface $parentConfig)
    {
        if ($parentConfig->getData() instanceof User) {
            $options['choices'] = User::getUserStatusAvailableChoices();
        }

        return $options;
    }

    /**
     * {@inheritdoc}
     */
    public function supports($type, array $options, array $metadata)
    {
        return in_array($type, ['choice', ChoiceType::class], true);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.