Search code examples
phpsymfonybackendeasyadmin

Disable or hide EasyAdmin crud search bar


I have developped an online learning website with EasyAdmin as backend. Everything works fine, but I'd like to hide or disable the search bar on top of the crud pages. I have not overridden any templates, just created crud based on my entities with fields and a custom query builder to only index content created by the logged in user. Can't seem to find any info on how to do it online or in the doc. Is it possible to add an option to hide or disable the default search bar? Thank you!


Solution

  • https://symfony.com/doc/3.x/EasyAdminBundle/crud.html#search-order-and-pagination-options

    In your Dashboard:

    # App\Controller\Admin\DashboardController
    
    public function configureCrud(): Crud
    {
        $crud = Crud::new();
        return $crud
            // ...
            ->setSearchFields(null); // hide the search bar on all admin pages
    }
    

    That's what you asked for. But you can go further:

    -> Hide the search bar only on a specific CrudController:

    # App\Controller\Admin\PostCrudController
    
    public function configureCrud(Crud $crud): Crud
    {
        return $crud
            // ...
            ->setSearchFields(null); // hide the search bar on admin pages related to the Post entity
    }
    

    -> Add conditions for granular control:

    # App\Controller\Admin\PostCrudController
    
    public function configureCrud(Crud $crud): Crud
    {
        if (
            $_GET['crudAction'] === Action::EDIT // if this is the 'edit' page
            || !in_array('ROLE_ADMIN', $this->getUser()->getRoles()) // or if the user is not an admin
        ) {
            $crud->setSearchFields(null); // then hide the search bar
        }
        return $crud;
    }