3
votes

Using SonataAdminBundle with Symfony2, I'm looking for a solution to access some Admin classes with a specific route.

For example, I have a ContractAdmin class with boolean fields such as "Enabled". What I would like is to add in the left KnpMenu of sonata admin, some links pointing to the same Admin class but with a custom route (other than the default "list" route), for example:

  • Contracts
    • All Contracts
    • Contracts enabled (Listing only enabled contract)
    • Contracts not yet enabled (Listing only not enabled contract)

This would avoid me to use filters.

So, how could I create and put these links to the menu which target the corresponding admin class controller with a custom route?

Thank you ;)

1
I have found a way to link the custom CRUD Controller to a custom route in the menu. Declaring a route in the configureRoutes method into the admin class, then adding the corresponding action into the corresponding CRUD Controller. In this action, I'm using the same code as in the "listAction". So now the question is : How to modify the query used to generate the list view from that controller action ?icedocemile

1 Answers

1
votes

I've solved it declaring a custom CRUDController for this admin class and adding the actions needed calling listAction method :

class ContractAdminController extends Controller {

public function contractsEnabledAction() {
    return $this->listAction();
}

I've declared this custom route into the Admin class :

protected function configureRoutes(RouteCollection $collection) {
    parent::configureRoutes($collection);
    $collection->add('contracts_enabled', 'contractsEnabled/');
}

Then, overriding the createQuery method in the admin class, I'm using the request "_route" attribute like that :

public function createQuery($context = 'list') {
    $query = parent::createQuery($context);

    switch ($this->getRequest()->get("_route")) {
        case "admin_acme_contract_contracts_enabled" :
            $query->andWhere(
                    $query->expr()->eq($query->getRootAliases()[0] . '.enabled', ':param')
            );
            $query->setParameter('param', true);
            break;
    }
    return $query;
}