EasyAdmin 3: How to display entities based on dql for CRUD index

1.5k Views Asked by At

I have an Entity named Page that can be a callToAction (boolean) and I would like to display the Page Entity with callToAction == false on one subMenu and the Page Entity with callToAction == true on another subMenu. I have a CRUD for the Page Entity. So the Dashboard would be something like that:

MenuItem::subMenu('Page', 'far fa-file-alt')->setSubItems([
                MenuItem::linkToCrud('Page', 'fa fa-alt', Page::class),
                MenuItem::linkToCrud('Call To Action', 'fa fa-file-alt', Page::class),
            ])

But I don't know where to put the dql to display the entities I want (callToAction true or false) and I don't even know if it's possible, but I know it was with Easy Admin 2, that's why I wonder. I also would like that on the NEW Action, when you're on the Page with callToAction == true, when you create the new Entity Page from here, that the callToAction is set to true immediatly and the User doesn't even see the field. Still don't know if it's possible. Thanks :)

EDIT: So i've found that I can use createIndexQueryBuilder() to display on the index exactly the entities, and it works well but I don't know how to call two different createIndexQueryBuilder depending of the subMenu we display. I tried doing a custom action and using createQueryBuilder but I don't have the params searchDto, etc:

public function configureActions(Actions $actions): Actions
    {
        $indexIsCallToAction = Action::new('indexIsCallToAction', 'Index Call To Action', 'fa fa-send')
            ->linkToCrudAction('indexIsCallToAction');
        $actions->add(CRUD::PAGE_INDEX, $indexIsCallToAction);
        return $actions;
        //return parent::configureActions($actions); // TODO: Change the autogenerated stub
    }

    public function indexIsCallToAction(AdminContext $context,SearchDto $searchDto, EntityDto $entityDto, FieldCollection $fields, FilterCollection $filters){
        $response = $this->get(EntityRepository::class)->createQueryBuilder($searchDto, $entityDto, $fields, $filters);

        return $response;
    }

So it doesn't work.

1

There are 1 best solutions below

0
On

As a dashboard controller is an ordinary controller you can do something like this:

public function __construct(PageRepository $pageRepo)
{
    $this->pageRepo = $pageRepo;
}

public function configureMenuItems(): iterable
{
    $submenuItems = [];
    if (null !== $pages = $this->pageRepo->findBy(["callToAction" => true ])) {
        foreach ($pages as $page) {
            $submenuItems[] = MenuItem::linkToCrud('Call To Action', 'fa fa-file-alt', Page::class);
        }
    }
    yield MenuItem::subMenu('Page Submenu with callToAction', 'far fa-file-alt')->setSubItems($submenuItems);

    $submenuItems = [];
    if (null !== $pages = $this->pageRepo->findBy(["callToAction" => false ])) {
        foreach ($pages as $page) {
            $submenuItems[] = MenuItem::linkToCrud('Page', 'fa fa-alt', Page::class);
        }
    }
    yield MenuItem::subMenu('Other Page Submenu', 'far fa-file-alt')->setSubItems($submenuItems);
}