vendor/ibexa/page-builder/src/bundle/Menu/Event/Subscriber/AddSwitchLayoutToolEventSubscriber.php line 48

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\PageBuilder\Menu\Event\Subscriber;
  8. use Ibexa\AdminUi\Menu\Event\ConfigureMenuEvent;
  9. use Ibexa\Bundle\PageBuilder\Menu\Event\PageBuilderConfigureMenuEventName;
  10. use Ibexa\Contracts\Core\Repository\Values\Content\Content;
  11. use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
  12. use Ibexa\FieldTypePage\ApplicationConfig\Providers\LayoutDefinitions;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class AddSwitchLayoutToolEventSubscriber implements EventSubscriberInterface
  15. {
  16. public const ITEM__SWITCH_LAYOUT = 'page_builder__infobar__tools__switch_layout';
  17. /** @var \Ibexa\FieldTypePage\ApplicationConfig\Providers\LayoutDefinitions */
  18. private $layoutDefinitionsProvider;
  19. public function __construct(LayoutDefinitions $layoutDefinitionsProvider)
  20. {
  21. $this->layoutDefinitionsProvider = $layoutDefinitionsProvider;
  22. }
  23. /**
  24. * Returns an array of event names this subscriber wants to listen to.
  25. *
  26. * @return array The event names to listen to
  27. */
  28. public static function getSubscribedEvents()
  29. {
  30. return [
  31. PageBuilderConfigureMenuEventName::PAGE_BUILDER_INFOBAR_PREVIEW_MODE_TOOLS => 'addSwitchLayoutMenuItem',
  32. PageBuilderConfigureMenuEventName::PAGE_BUILDER_INFOBAR_EDIT_MODE_TOOLS => 'addSwitchLayoutMenuItem',
  33. PageBuilderConfigureMenuEventName::PAGE_BUILDER_INFOBAR_CREATE_MODE_TOOLS => 'addSwitchLayoutMenuItem',
  34. ];
  35. }
  36. /**
  37. * @param \Ibexa\AdminUi\Menu\Event\ConfigureMenuEvent $event
  38. */
  39. // TODO: now it is rendered with React component, should it be?
  40. public function addSwitchLayoutMenuItem(ConfigureMenuEvent $event): void
  41. {
  42. $menu = $event->getMenu();
  43. $filterBy = null;
  44. if (isset($event->getOptions()['content'])) {
  45. $content = $event->getOptions()['content'];
  46. if ($content instanceof Content) {
  47. $filterBy = $content->getContentType();
  48. }
  49. }
  50. if (!$this->isMoreThanOneVisibleLayout($filterBy)) {
  51. return;
  52. }
  53. $menu->addChild(
  54. $event->getFactory()->createItem(
  55. self::ITEM__SWITCH_LAYOUT,
  56. [
  57. 'label' => false,
  58. 'extras' => [
  59. 'empty_list_item' => true,
  60. 'list_item_attributes' => [
  61. 'class' => 'ibexa-pb-action-bar__tools-item--layout-switcher',
  62. ],
  63. ],
  64. ]
  65. )
  66. );
  67. }
  68. private function isMoreThanOneVisibleLayout(?ContentType $filterBy = null): bool
  69. {
  70. $visibleLayouts = array_filter(
  71. $this->layoutDefinitionsProvider->getConfig($filterBy),
  72. static function (array $layoutDefinition) {
  73. return $layoutDefinition['visible'];
  74. }
  75. );
  76. return count($visibleLayouts) > 1;
  77. }
  78. }
  79. class_alias(AddSwitchLayoutToolEventSubscriber::class, 'EzSystems\EzPlatformPageBuilderBundle\Menu\Event\Subscriber\AddSwitchLayoutToolEventSubscriber');