GroupMenuProvider.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Menu\Provider;
  11. use Knp\Menu\FactoryInterface;
  12. use Knp\Menu\Provider\MenuProviderInterface;
  13. use Sonata\AdminBundle\Admin\Pool;
  14. /**
  15. * Menu provider based on group options.
  16. *
  17. * @author Alexandru Furculita <alex@furculita.net>
  18. */
  19. class GroupMenuProvider implements MenuProviderInterface
  20. {
  21. /**
  22. * @var FactoryInterface
  23. */
  24. private $menuFactory;
  25. /**
  26. * @var Pool
  27. */
  28. private $pool;
  29. /**
  30. * @param FactoryInterface $menuFactory
  31. * @param Pool $pool
  32. */
  33. public function __construct(FactoryInterface $menuFactory, Pool $pool)
  34. {
  35. $this->menuFactory = $menuFactory;
  36. $this->pool = $pool;
  37. }
  38. /**
  39. * Retrieves the menu based on the group options.
  40. *
  41. * @param string $name
  42. * @param array $options
  43. *
  44. * @return \Knp\Menu\ItemInterface
  45. *
  46. * @throws \InvalidArgumentException if the menu does not exists
  47. */
  48. public function get($name, array $options = array())
  49. {
  50. $group = $options['group'];
  51. $menuItem = $this->menuFactory->createItem(
  52. $options['name'],
  53. array(
  54. 'label' => $group['label'],
  55. )
  56. );
  57. foreach ($group['items'] as $item) {
  58. if (isset($item['admin']) && !empty($item['admin'])) {
  59. $admin = $this->pool->getInstance($item['admin']);
  60. // skip menu item if no `list` url is available or user doesn't have the LIST access rights
  61. if (!$admin->hasRoute('list') || !$admin->isGranted('LIST')) {
  62. continue;
  63. }
  64. $label = $admin->getLabel();
  65. $options = $admin->generateMenuUrl('list', array(), $item['route_absolute']);
  66. $options['extras'] = array(
  67. 'translation_domain' => $admin->getTranslationDomain(),
  68. 'admin' => $admin,
  69. );
  70. } else {
  71. $label = $item['label'];
  72. $options = array(
  73. 'route' => $item['route'],
  74. 'routeParameters' => $item['route_params'],
  75. 'routeAbsolute' => $item['route_absolute'],
  76. 'extras' => array(
  77. 'translation_domain' => $group['label_catalogue'],
  78. ),
  79. );
  80. }
  81. $menuItem->addChild($label, $options);
  82. }
  83. if (false === $menuItem->hasChildren()) {
  84. $menuItem->setDisplay(false);
  85. }
  86. return $menuItem;
  87. }
  88. /**
  89. * Checks whether a menu exists in this provider.
  90. *
  91. * @param string $name
  92. * @param array $options
  93. *
  94. * @return bool
  95. */
  96. public function has($name, array $options = array())
  97. {
  98. return 'sonata_group_menu' === $name;
  99. }
  100. }