vendor/ibexa/fieldtype-page/src/lib/Event/Subscriber/CollectRelationsSubscriber.php line 47

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\BlockRelationEvents;
  10. use Ibexa\FieldTypePage\Event\CollectBlockRelationsEvent;
  11. use Ibexa\FieldTypePage\FieldType\Page\Block\Definition\BlockDefinitionFactoryInterface;
  12. use Ibexa\FieldTypePage\FieldType\Page\Block\Relation\BlockAttributeTypeRelationExtractorRegistry;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class CollectRelationsSubscriber implements EventSubscriberInterface
  15. {
  16.     /** @var \Ibexa\FieldTypePage\FieldType\Page\Block\Relation\BlockAttributeTypeRelationExtractorRegistry */
  17.     private $extractorRegistry;
  18.     /** @var \Ibexa\FieldTypePage\FieldType\Page\Block\Definition\BlockDefinitionFactoryInterface */
  19.     private $blockDefinitionFactory;
  20.     public function __construct(
  21.         BlockAttributeTypeRelationExtractorRegistry $extractorRegistry,
  22.         BlockDefinitionFactoryInterface $blockDefinitionFactory
  23.     ) {
  24.         $this->extractorRegistry $extractorRegistry;
  25.         $this->blockDefinitionFactory $blockDefinitionFactory;
  26.     }
  27.     /**
  28.      * {@inheritdoc}
  29.      */
  30.     public static function getSubscribedEvents()
  31.     {
  32.         return [
  33.             BlockRelationEvents::COLLECT_BLOCK_RELATIONS => ['onCollectBlockRelations'20],
  34.         ];
  35.     }
  36.     /**
  37.      * @param \Ibexa\FieldTypePage\Event\CollectBlockRelationsEvent $event
  38.      */
  39.     public function onCollectBlockRelations(CollectBlockRelationsEvent $event): void
  40.     {
  41.         $fieldValue $event->getFieldValue();
  42.         $blockValue $event->getBlockValue();
  43.         $relations $event->getRelations();
  44.         $page $fieldValue->getPage();
  45.         $newRelations = [];
  46.         foreach ($blockValue->getAttributes() as $attribute) {
  47.             try {
  48.                 $blockDefinition $this->blockDefinitionFactory->getBlockDefinition($blockValue->getType());
  49.             } catch (\Exception $e) {
  50.                 continue;
  51.             }
  52.             $attributeDefinition $this->getAttributeDefinitionFromBlock($blockDefinition$attribute->getName());
  53.             if (null === $attributeDefinition) {
  54.                 continue;
  55.             }
  56.             $extractors $this->extractorRegistry->getApplicableExtractors(
  57.                 $page,
  58.                 $blockValue,
  59.                 $attributeDefinition,
  60.                 $attribute
  61.             );
  62.             foreach ($extractors as $extractor) {
  63.                 $newRelations[] = $extractor->extract($page$blockValue$attributeDefinition$attribute);
  64.             }
  65.         }
  66.         if (\count($newRelations) === 0) {
  67.             return;
  68.         }
  69.         $event->setRelations(
  70.             array_merge($relationsarray_merge(...$newRelations))
  71.         );
  72.     }
  73.     /**
  74.      * @param \Ibexa\Contracts\FieldTypePage\FieldType\Page\Block\Definition\BlockDefinition $blockDefinition
  75.      * @param string $name
  76.      *
  77.      * @return \Ibexa\Contracts\FieldTypePage\FieldType\Page\Block\Definition\BlockAttributeDefinition|null
  78.      */
  79.     private function getAttributeDefinitionFromBlock(BlockDefinition $blockDefinitionstring $name)
  80.     {
  81.         foreach ($blockDefinition->getAttributes() as $attributeDefinition) {
  82.             if ($attributeDefinition->getIdentifier() === $name) {
  83.                 return $attributeDefinition;
  84.             }
  85.         }
  86.         return null;
  87.     }
  88. }
  89. class_alias(CollectRelationsSubscriber::class, 'EzSystems\EzPlatformPageFieldType\Event\Subscriber\CollectRelationsSubscriber');