/**
  * {@inheritdoc}
  */
 public function validateBlock(ErrorElement $errorElement, BlockInterface $block)
 {
     if (($name = $block->getSetting('menu_name')) && $name !== '' && !$this->menuProvider->has($name)) {
         // If we specified a menu_name, check that it exists
         $errorElement->with('menu_name')->addViolation('sonata.block.menu.not_existing', array('name' => $name))->end();
     }
 }
 /**
  * Retrieves item in the menu, eventually using the menu provider.
  *
  * @param ItemInterface|string $menu
  * @param array $pathName
  * @param array $options
  *
  * @return ItemInterface
  *
  * @throws \LogicException
  * @throws \InvalidArgumentException when the path is invalid
  */
 public function getMenu($menu, array $pathName = array(), array $options = array())
 {
     if (!$menu instanceof ItemInterface) {
         $menu = $this->provider->get((string) $menu, array_merge($options, array('check_access' => false)));
     }
     foreach ($pathName as $child) {
         $menu = $menu->getChild($child);
         if ($menu === null) {
             throw new \InvalidArgumentException(sprintf('The menu has no child named "%s"', $child));
         }
     }
     return $menu;
 }
 /**
  * Builds sidebar menu.
  *
  * @return ItemInterface
  */
 public function createSidebarMenu()
 {
     $menu = $this->factory->createItem('root');
     foreach ($this->pool->getAdminGroups() as $name => $group) {
         $extras = array('icon' => $group['icon'], 'label_catalogue' => $group['label_catalogue'], 'roles' => $group['roles']);
         $menuProvider = isset($group['provider']) ? $group['provider'] : 'sonata_group_menu';
         $subMenu = $this->provider->get($menuProvider, array('name' => $name, 'group' => $group));
         $subMenu = $menu->addChild($subMenu);
         $subMenu->setExtras(array_merge($subMenu->getExtras(), $extras));
     }
     $event = new ConfigureMenuEvent($this->factory, $menu);
     $this->eventDispatcher->dispatch(ConfigureMenuEvent::SIDEBAR, $event);
     return $event->getMenu();
 }
 /**
  * @param array $adminGroupsOnTopOption
  *
  * @dataProvider getAdminGroupsWithOnTopOption
  */
 public function testGetMenuProviderOnTopOptions(array $adminGroupsOnTopOption)
 {
     $this->pool->expects($this->once())->method('getInstance')->with($this->equalTo('sonata_admin_foo_service'))->will($this->returnValue($this->getAdminMock(true, false)));
     $menu = $this->provider->get('providerFoo', array('name' => 'foo', 'group' => $adminGroupsOnTopOption));
     $this->assertInstanceOf('Knp\\Menu\\ItemInterface', $menu);
     $this->assertCount(0, $menu->getChildren());
 }
Example #5
0
 /**
  * Retrieves item in the menu, eventually using the menu provider.
  *
  * @param ItemInterface|string $menu
  * @param array                $path
  * @param array                $options
  *
  * @return ItemInterface
  *
  * @throws \InvalidArgumentException when the path is invalid
  */
 public function getMenu($menu, array $path = array(), array $options = array())
 {
     if (!$menu instanceof ItemInterface) {
         $menu = $this->provider->get((string) $menu, $options);
     }
     foreach ($path as $child) {
         $menu = $menu->getChild($child);
         if (null === $menu) {
             throw new \InvalidArgumentException(sprintf('The menu has no child named "%s"', $child));
         }
     }
     return $menu;
 }
 /**
  * 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();
         $extras = array('icon' => $group['icon'], 'label_catalogue' => $group['label_catalogue'], '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);
         }
     }
     $event = new ConfigureMenuEvent($this->factory, $menu);
     $this->eventDispatcher->dispatch(ConfigureMenuEvent::SIDEBAR, $event);
     return $event->getMenu();
 }
Example #7
0
 /**
  * Renders a menu in a specific location.
  *
  * @param string $location The location on the page to render the menu
  * @param array  $options
  *
  * @return string
  */
 public function renderMenu($location, array $options = [])
 {
     /** @var \SplPriorityQueue $menu */
     $menu = $this->provider->get($location);
     return $this->renderer->build($menu, $options);
 }
Example #8
0
 /**
  * Renders a menu in a specific location.
  *
  * @param string $location The location on the page to render the menu
  * @param array  $options
  *
  * @return string
  */
 public function renderMenu($location, array $options = array())
 {
     $menu = $this->provider->get($location);
     return $this->renderer->build($menu, $options);
 }