vendor/ibexa/fieldtype-page/src/lib/Event/Subscriber/BlockRenderSubscriber.php line 32

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\LandingPage\Model\Zone;
  9. use Ibexa\FieldTypePage\FieldType\Page\Block\Renderer\BlockRenderEvents;
  10. use Ibexa\FieldTypePage\FieldType\Page\Block\Renderer\Event\PreRenderEvent;
  11. use Ibexa\FieldTypePage\FieldType\Page\Block\Renderer\Twig\TwigRenderRequest;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class BlockRenderSubscriber implements EventSubscriberInterface
  14. {
  15. /**
  16. * {@inheritdoc}
  17. */
  18. public static function getSubscribedEvents()
  19. {
  20. return [
  21. BlockRenderEvents::GLOBAL_BLOCK_RENDER_PRE => ['onBlockPreRender', 0],
  22. ];
  23. }
  24. /**
  25. * @param \Ibexa\FieldTypePage\FieldType\Page\Block\Renderer\Event\PreRenderEvent $event
  26. */
  27. public function onBlockPreRender(PreRenderEvent $event): void
  28. {
  29. $blockContext = $event->getBlockContext();
  30. $renderRequest = $event->getRenderRequest();
  31. if (!$renderRequest instanceof TwigRenderRequest) {
  32. return;
  33. }
  34. $parameters = array_merge(
  35. $renderRequest->getParameters(),
  36. [
  37. 'block_context' => $blockContext,
  38. 'zone' => $this->getZone($event),
  39. ]
  40. );
  41. $renderRequest->setParameters($parameters);
  42. }
  43. /**
  44. * @param \Ibexa\FieldTypePage\FieldType\Page\Block\Renderer\Event\PreRenderEvent $event
  45. *
  46. * @return \Ibexa\Contracts\FieldTypePage\FieldType\LandingPage\Model\Zone|null
  47. */
  48. private function getZone(PreRenderEvent $event): ?Zone
  49. {
  50. $blockValue = $event->getBlockValue();
  51. $blockContext = $event->getBlockContext();
  52. $page = $blockContext->getPage();
  53. foreach ($page->getZones() as $zone) {
  54. foreach ($zone->getBlocks() as $block) {
  55. if ($block->getId() === $blockValue->getId()) {
  56. return $zone;
  57. }
  58. }
  59. }
  60. return null;
  61. }
  62. }
  63. class_alias(BlockRenderSubscriber::class, 'EzSystems\EzPlatformPageFieldType\Event\Subscriber\BlockRenderSubscriber');