vendor/ibexa/taxonomy/src/lib/Event/Subscriber/ConfigureContentRightSidebarMenuSubscriber.php line 68

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\ContentCreateRightSidebarBuilder;
  9. use Ibexa\AdminUi\Menu\ContentEditRightSidebarBuilder;
  10. use Ibexa\AdminUi\Menu\Event\ConfigureMenuEvent;
  11. use Ibexa\Taxonomy\Service\TaxonomyConfiguration;
  12. use JMS\TranslationBundle\Model\Message;
  13. use JMS\TranslationBundle\Translation\TranslationContainerInterface;
  14. use Knp\Menu\ItemInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. /**
  17.  * Modifying content create/edit menu items to remove items not related to Taxonomy.
  18.  */
  19. final class ConfigureContentRightSidebarMenuSubscriber implements EventSubscriberInterfaceTranslationContainerInterface
  20. {
  21.     public const ITEM__PUBLISH_AND_EDIT 'taxonomy__sidebar_right__publish_and_edit';
  22.     private const TRANSLATION_PREFIX 'taxonomy_';
  23.     private const ALLOWED_CREATE_MENU_ITEMS = [
  24.         ContentCreateRightSidebarBuilder::ITEM__PUBLISH,
  25.         ContentCreateRightSidebarBuilder::ITEM__CANCEL,
  26.         self::ITEM__PUBLISH_AND_EDIT,
  27.     ];
  28.     private const ALLOWED_EDIT_MENU_ITEMS = [
  29.         ContentEditRightSidebarBuilder::ITEM__PUBLISH,
  30.         ContentEditRightSidebarBuilder::ITEM__CANCEL,
  31.         self::ITEM__PUBLISH_AND_EDIT,
  32.     ];
  33.     private TaxonomyConfiguration $taxonomyConfiguration;
  34.     public function __construct(TaxonomyConfiguration $taxonomyConfiguration)
  35.     {
  36.         $this->taxonomyConfiguration $taxonomyConfiguration;
  37.     }
  38.     public static function getSubscribedEvents(): array
  39.     {
  40.         return [
  41.             ConfigureMenuEvent::CONTENT_EDIT_SIDEBAR_RIGHT => 'onContentEditSidebarRight',
  42.             ConfigureMenuEvent::CONTENT_CREATE_SIDEBAR_RIGHT => 'onContentCreateSidebarRight',
  43.         ];
  44.     }
  45.     public function onContentEditSidebarRight(ConfigureMenuEvent $event): void
  46.     {
  47.         if (
  48.             !isset($event->getOptions()['content_type'])
  49.             || !$this->taxonomyConfiguration->isContentTypeAssociatedWithTaxonomy($event->getOptions()['content_type'])
  50.         ) {
  51.             return;
  52.         }
  53.         $this->modifyMenuItems($event->getMenu(), self::ALLOWED_EDIT_MENU_ITEMS);
  54.     }
  55.     public function onContentCreateSidebarRight(ConfigureMenuEvent $event): void
  56.     {
  57.         if (
  58.             !isset($event->getOptions()['content_type'])
  59.             || !$this->taxonomyConfiguration->isContentTypeAssociatedWithTaxonomy($event->getOptions()['content_type'])
  60.         ) {
  61.             return;
  62.         }
  63.         $this->modifyMenuItems($event->getMenu(), self::ALLOWED_CREATE_MENU_ITEMS);
  64.     }
  65.     /**
  66.      * All menu items not related to taxonomy should be removed.
  67.      * Remaining items should contain changed translation domain and label.
  68.      *
  69.      * @param array<string> $allowedMenuItems
  70.      */
  71.     private function modifyMenuItems(ItemInterface $menu, array $allowedMenuItems): void
  72.     {
  73.         $children $menu->getChildren();
  74.         foreach ($children as $child) {
  75.             if (in_array($child->getName(), $allowedMenuItemstrue)) {
  76.                 $child->setExtra('translation_domain''ibexa_taxonomy_menu');
  77.                 $label $child->getLabel();
  78.                 $child->setLabel(self::TRANSLATION_PREFIX $label);
  79.                 if ($child->getName() === ContentCreateRightSidebarBuilder::ITEM__PUBLISH
  80.                 || $child->getName() === ContentEditRightSidebarBuilder::ITEM__PUBLISH) {
  81.                     $this->addPublishAndEditButton($child);
  82.                 }
  83.                 $this->modifyMenuItems($child$allowedMenuItems);
  84.             } else {
  85.                 $menu->removeChild($child);
  86.             }
  87.         }
  88.     }
  89.     private function addPublishAndEditButton(ItemInterface $parentItem): void
  90.     {
  91.         $parentAttributes $parentItem->getAttributes();
  92.         $publishAndEditAttributes array_replace($parentAttributes, [
  93.             'data-click' => '#ezplatform_content_forms_content_edit_publishAndEdit',
  94.         ]);
  95.         $parentItem->addChild(
  96.             self::ITEM__PUBLISH_AND_EDIT,
  97.             [
  98.                 'attributes' => $publishAndEditAttributes,
  99.                 'extras' => [
  100.                     'orderNumber' => 10,
  101.                 ],
  102.             ]
  103.         );
  104.     }
  105.     public static function getTranslationMessages(): array
  106.     {
  107.         return [
  108.             (new Message(self::TRANSLATION_PREFIX ContentCreateRightSidebarBuilder::ITEM__PUBLISH'ibexa_taxonomy_menu'))->setDesc('Save and close'),
  109.             (new Message(self::TRANSLATION_PREFIX ContentCreateRightSidebarBuilder::ITEM__CANCEL'ibexa_taxonomy_menu'))->setDesc('Discard'),
  110.             (new Message(self::TRANSLATION_PREFIX ContentEditRightSidebarBuilder::ITEM__PUBLISH'ibexa_taxonomy_menu'))->setDesc('Save and close'),
  111.             (new Message(self::TRANSLATION_PREFIX ContentEditRightSidebarBuilder::ITEM__CANCEL'ibexa_taxonomy_menu'))->setDesc('Discard'),
  112.             (new Message(self::TRANSLATION_PREFIX self::ITEM__PUBLISH_AND_EDIT'ibexa_taxonomy_menu'))->setDesc('Save'),
  113.         ];
  114.     }
  115. }