Khandaker Toihidul Islam
Software Engineer
Backend : PHP | Mysql | Symfony | CakePhp | Wordpress | Drupal
Frontend : Node js | Vue js | React Js | Javascript | HTML | CSS | Bootstrap
Tools , Server : Nginx | Apache | Git | Jenkins | Jira | Asana | Silenium | Spira

Customized dropdown in Easy Admin

  1. At first need to config config/packages/easy_admin.yaml. The controller and query builder must be in the same Class.
      Student:
        controller: App\Controller\CustomEasyAdmin
        class: App\Entity\Student 
        form:
          fields:
            - { property: 'name' }
            - { property: 'grade', type: 'entity', type_options: { class: 'App\Entity\Grade', required: true, multiple: false, query_builder: ['App\Controller\CustomEasyAdmin', 'crateGradeSelectBox'] } }
          

2. Now we will create the controller and method

<?php 
namespace App\Controller;
use App\Repository\GradeRepository;
use Doctrine\ORM\QueryBuilder;
use EasyCorp\Bundle\EasyAdminBundle\Form\Type\EasyAdminFormType;
use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController;
use Symfony\Component\HttpFoundation\Request;

class CustomEasyAdmin extends EasyAdminController
{
    private static $user;

    protected function initialize(Request $request){
      self::$user = $this->getUser();
      parent::initialize($request);
    }

    public function crateGradeSelectBox(GradeRepository $r): QueryBuilder
    {
        $userId = self::$user->getId();
        return $r->createQueryBuilder('g')
           ->innerJoin('g.school', 's')
           ->innerJoin('s.userId', 'u')
           ->where('u.id = :user')
           ->setParameter('user', $userId)
           ->orderBy('g.id', 'DESC');
     }

}

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *