vendor/ibexa/order-management/src/bundle/EventSubscriber/MainMenuSubscriber.php line 38

Open in your IDE?
  1. <?php
  2. /**
  3. * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4. * @license For full copyright and license information view LICENSE file distributed with this source code.
  5. */
  6. declare(strict_types=1);
  7. namespace Ibexa\Bundle\OrderManagement\EventSubscriber;
  8. use Ibexa\AdminUi\Menu\Event\ConfigureMenuEvent;
  9. use Ibexa\Contracts\OrderManagement\Policy\Order\View;
  10. use Ibexa\Contracts\ProductCatalog\PermissionResolverInterface;
  11. use JMS\TranslationBundle\Model\Message;
  12. use JMS\TranslationBundle\Translation\TranslationContainerInterface;
  13. use Knp\Menu\ItemInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. final class MainMenuSubscriber implements EventSubscriberInterface, TranslationContainerInterface
  16. {
  17. public const ITEM_COMMERCE = 'main__commerce';
  18. public const ITEM_COMMERCE_ORDERS = 'main__commerce__orders';
  19. private PermissionResolverInterface $permissionResolver;
  20. public function __construct(PermissionResolverInterface $permissionResolver)
  21. {
  22. $this->permissionResolver = $permissionResolver;
  23. }
  24. public static function getSubscribedEvents(): array
  25. {
  26. return [
  27. ConfigureMenuEvent::MAIN_MENU => ['onConfigureMainMenu', 15],
  28. ];
  29. }
  30. public function onConfigureMainMenu(ConfigureMenuEvent $event): void
  31. {
  32. $menu = $event->getMenu();
  33. $this->addCommerceMenu($menu);
  34. $rootElement = $menu->getChild(self::ITEM_COMMERCE);
  35. if ($rootElement === null || !$this->permissionResolver->canUser(new View())) {
  36. return;
  37. }
  38. $rootElement->addChild(
  39. self::ITEM_COMMERCE_ORDERS,
  40. [
  41. 'route' => 'ibexa.order_management.list',
  42. 'extras' => [
  43. 'orderNumber' => 10,
  44. 'routes' => [
  45. [
  46. 'pattern' => '~^ibexa\.order_management\.~',
  47. ],
  48. ],
  49. ],
  50. ]
  51. );
  52. }
  53. public static function getTranslationMessages(): array
  54. {
  55. return [
  56. (new Message(self::ITEM_COMMERCE, 'ibexa_menu'))->setDesc('Commerce'),
  57. (new Message(self::ITEM_COMMERCE_ORDERS, 'ibexa_menu'))->setDesc('Orders'),
  58. ];
  59. }
  60. private function addCommerceMenu(ItemInterface $menu): void
  61. {
  62. $menu->addChild(
  63. self::ITEM_COMMERCE,
  64. [
  65. 'attributes' => [
  66. 'data-tooltip-placement' => 'right',
  67. 'data-tooltip-extra-class' => 'ibexa-tooltip--navigation',
  68. ],
  69. 'extras' => [
  70. 'icon' => 'cart-full',
  71. 'orderNumber' => 100,
  72. ],
  73. ]
  74. );
  75. }
  76. }