简体   繁体   中英

Sonata Admin Class : add KnpMenu links pointing Admin class with custom route

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 ;)

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM