forked from sonata-project/SonataAdminBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored creation of sidebar menu by using menu builder
- Loading branch information
Showing
13 changed files
with
453 additions
and
344 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?php | ||
/* | ||
* This file is part of the Sonata package. | ||
* | ||
* (c) Thomas Rabaix <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
*/ | ||
|
||
namespace Sonata\AdminBundle\Menu; | ||
|
||
use Knp\Menu\FactoryInterface; | ||
use Knp\Menu\ItemInterface; | ||
use Knp\Menu\Provider\MenuProviderInterface; | ||
use Sonata\AdminBundle\Admin\Pool; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* Sonata menu builder. | ||
* | ||
* @author Martin Hasoň <[email protected]> | ||
*/ | ||
class MenuBuilder | ||
{ | ||
private $pool; | ||
private $factory; | ||
private $provider; | ||
private $request; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param Pool $pool | ||
* @param FactoryInterface $factory | ||
* @param MenuProviderInterface $provider | ||
*/ | ||
public function __construct(Pool $pool, FactoryInterface $factory, MenuProviderInterface $provider) | ||
{ | ||
$this->pool = $pool; | ||
$this->factory = $factory; | ||
$this->provider = $provider; | ||
} | ||
|
||
/** | ||
* Builds sidebar menu. | ||
* | ||
* @return ItemInterface | ||
*/ | ||
public function createSidebarMenu() | ||
{ | ||
$menu = $this->factory->createItem('root', array( | ||
'extras' => array( | ||
'request' => $this->request, | ||
), | ||
)); | ||
|
||
foreach ($this->pool->getAdminGroups() as $name => $group) { | ||
$attributes = array( | ||
'icon' => $group['icon'], | ||
'label_catalogue' => $group['label_catalogue'], | ||
); | ||
|
||
$extras = array( | ||
'roles' => $group['roles'], | ||
); | ||
|
||
// Check if the menu group is built by a menu provider | ||
if (isset($group['provider'])) { | ||
$subMenu = $this->provider->get($group['provider']); | ||
|
||
$menu | ||
->addChild($subMenu) | ||
->setExtras(array_merge($subMenu->getExtras(), $extras)) | ||
->setAttributes(array_merge($subMenu->getAttributes(), $attributes)) | ||
; | ||
|
||
continue; | ||
} | ||
|
||
// The menu group is built by config | ||
$menu->addChild($name, array( | ||
'label' => $group['label'], | ||
'attributes' => $attributes, | ||
'extras' => $extras, | ||
)); | ||
|
||
foreach ($group['items'] as $item) { | ||
if (isset($item['admin']) && !empty($item['admin'])) { | ||
$admin = $this->pool->getInstance($item['admin']); | ||
|
||
// skip menu item if no `list` url is available or user doesn't have the LIST access rights | ||
if (!$admin->hasRoute('list') || !$admin->isGranted('LIST')) { | ||
continue; | ||
} | ||
|
||
$label = $admin->getLabel(); | ||
$options = $admin->generateMenuUrl('list'); | ||
$options['extras'] = array( | ||
'translation_domain' =>$admin->getTranslationDomain(), | ||
'admin' => $admin, | ||
); | ||
} else { | ||
$label = $item['label']; | ||
$options = array( | ||
'route' => $item['route'], | ||
'routeParameters' => $item['route_params'], | ||
'extras' => array( | ||
'translation_domain' => $group['label_catalogue'], | ||
) | ||
); | ||
} | ||
|
||
$menu[$name]->addChild($label, $options); | ||
} | ||
|
||
if (0 === count($menu[$name]->getChildren())) { | ||
$menu->removeChild($name); | ||
} | ||
} | ||
|
||
return $menu; | ||
} | ||
|
||
/** | ||
* Sets the request the service | ||
* | ||
* @param Request $request | ||
*/ | ||
public function setRequest(Request $request = null) | ||
{ | ||
$this->request = $request; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
<service id="sonata.admin.menu_builder" class="Sonata\AdminBundle\Menu\MenuBuilder"> | ||
<argument type="service" id="sonata.admin.pool" /> | ||
<argument type="service" id="knp_menu.factory" /> | ||
<argument type="service" id="knp_menu.menu_provider" /> | ||
<call method="setRequest"><argument type="service" id="request" on-invalid="null" strict="false" /></call> | ||
</service> | ||
|
||
<service id="sonata.admin.sidebar_menu" class="Knp\Menu\MenuItem"> | ||
<tag name="knp_menu.menu" alias="sonata_admin_sidebar" /> | ||
</service> | ||
</services> | ||
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.