Reputation: 41
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:
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 ;)
Upvotes: 3
Views: 328
Reputation: 41
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;
}
Upvotes: 1