vendor/ibexa/taxonomy/src/lib/Event/Subscriber/ConfigureMainMenuSubscriber.php line 46

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\Taxonomy\Event\Subscriber;
  8. use Ibexa\AdminUi\Menu\Event\ConfigureMenuEvent;
  9. use Ibexa\AdminUi\Menu\MainMenuBuilder;
  10. use Ibexa\Contracts\Core\Repository\ContentService;
  11. use Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException;
  12. use Ibexa\Taxonomy\Exception\TaxonomyNotFoundException;
  13. use Ibexa\Taxonomy\Service\TaxonomyConfiguration;
  14. use Ibexa\Taxonomy\Tree\TaxonomyTreeServiceInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class ConfigureMainMenuSubscriber implements EventSubscriberInterface
  17. {
  18. private TaxonomyConfiguration $taxonomyConfiguration;
  19. private TaxonomyTreeServiceInterface $taxonomyTreeService;
  20. private ContentService $contentService;
  21. public function __construct(
  22. TaxonomyConfiguration $taxonomyConfiguration,
  23. TaxonomyTreeServiceInterface $taxonomyTreeService,
  24. ContentService $contentService
  25. ) {
  26. $this->taxonomyConfiguration = $taxonomyConfiguration;
  27. $this->taxonomyTreeService = $taxonomyTreeService;
  28. $this->contentService = $contentService;
  29. }
  30. /**
  31. * @return array<string, string>
  32. */
  33. public static function getSubscribedEvents(): array
  34. {
  35. return [ConfigureMenuEvent::MAIN_MENU => 'onMainMenuConfigure'];
  36. }
  37. public function onMainMenuConfigure(ConfigureMenuEvent $event): void
  38. {
  39. $mainMenu = $event->getMenu();
  40. $contentMenu = $mainMenu->getChild(MainMenuBuilder::ITEM_CONTENT);
  41. if (null === $contentMenu) {
  42. return;
  43. }
  44. foreach ($this->taxonomyConfiguration->getTaxonomies() as $taxonomy) {
  45. $register = $this->taxonomyConfiguration->getConfigForTaxonomy(
  46. $taxonomy,
  47. TaxonomyConfiguration::CONFIG_REGISTER_MAIN_MENU
  48. );
  49. if ($register === false) {
  50. continue;
  51. }
  52. try {
  53. $treeRoot = $this->taxonomyTreeService->loadTreeRoot($taxonomy);
  54. $treeRootNode = reset($treeRoot);
  55. $rootContent = $this->contentService->loadContentInfo($treeRootNode['contentId']);
  56. } catch (UnauthorizedException | TaxonomyNotFoundException $exception) {
  57. continue;
  58. }
  59. $contentMenu->addChild(
  60. sprintf('taxonomy.%s', $taxonomy),
  61. [
  62. 'route' => 'ibexa.content.view',
  63. 'routeParameters' => [
  64. 'contentId' => $rootContent->id,
  65. 'locationId' => $rootContent->mainLocationId,
  66. ],
  67. 'extras' => [
  68. 'translation_domain' => 'ibexa_taxonomy',
  69. 'orderNumber' => 65,
  70. 'taxonomy' => $taxonomy,
  71. ],
  72. ]
  73. );
  74. }
  75. }
  76. }