vendor/ibexa/fieldtype-page/src/lib/Event/Subscriber/EmbedAttributeSubscriber.php line 40

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\FieldTypePage\Event\Subscriber;
  8. use Ibexa\Contracts\FieldTypePage\FieldType\Page\Block\Definition\BlockDefinition;
  9. use Ibexa\FieldTypePage\Event\PageEvents;
  10. use Ibexa\FieldTypePage\Event\PageFromPersistenceEvent;
  11. use Ibexa\FieldTypePage\FieldType\Page\Block\Definition\BlockDefinitionFactoryInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. final class EmbedAttributeSubscriber implements EventSubscriberInterface
  14. {
  15.     /** @var \Ibexa\FieldTypePage\FieldType\Page\Block\Definition\BlockDefinitionFactoryInterface */
  16.     private $blockDefinitionFactory;
  17.     /** @var string[] */
  18.     private $attributeTypes;
  19.     public function __construct(
  20.         BlockDefinitionFactoryInterface $blockDefinitionFactory,
  21.         array $attributeTypes
  22.     ) {
  23.         $this->blockDefinitionFactory $blockDefinitionFactory;
  24.         $this->attributeTypes $attributeTypes;
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             PageEvents::PERSISTENCE_FROM => 'onPageLoadFromPersistence',
  30.         ];
  31.     }
  32.     public function onPageLoadFromPersistence(PageFromPersistenceEvent $event): void
  33.     {
  34.         $page $event->getValue()->getPage();
  35.         $visitedBlockDefinitions = [];
  36.         foreach ($page->getBlockIterator() as $block) {
  37.             $type $block->getType();
  38.             if (!$this->isBlockTypeValid($type$visitedBlockDefinitions)) {
  39.                 continue;
  40.             }
  41.             $blockDefinition $this->getBlockDefinition($type$visitedBlockDefinitions);
  42.             $visitedBlockDefinitions[$type] = $blockDefinition;
  43.             foreach ($blockDefinition->getAttributes() as $attributeDefinition) {
  44.                 if (!in_array($attributeDefinition->getType(), $this->attributeTypes)) {
  45.                     continue;
  46.                 }
  47.                 $attribute $block->getAttribute($attributeDefinition->getIdentifier());
  48.                 if ($attribute !== null && is_string($attribute->getValue())) {
  49.                     $attribute->setValue((int)$attribute->getValue());
  50.                 }
  51.             }
  52.         }
  53.     }
  54.     private function isBlockTypeValid(string $type, array $visitedBlockDefinitions): bool
  55.     {
  56.         return isset($visitedBlockDefinitions[$type]) || $this->blockDefinitionFactory->hasBlockDefinition($type);
  57.     }
  58.     private function getBlockDefinition(string $type, array $visitedBlockDefinitions): BlockDefinition
  59.     {
  60.         return $visitedBlockDefinitions[$type] ?? $this->blockDefinitionFactory->getBlockDefinition($type);
  61.     }
  62. }
  63. class_alias(EmbedAttributeSubscriber::class, 'EzSystems\EzPlatformPageFieldType\Event\Subscriber\EmbedAttributeSubscriber');