vendor/ibexa/dashboard/src/bundle/EventSubscriber/PageBuilder/BlockQueryParametersSubscriber.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\Bundle\Dashboard\EventSubscriber\PageBuilder;
  8. use Ibexa\FieldTypePage\FieldType\Page\Block\Renderer\BlockRenderEvents;
  9. use Ibexa\FieldTypePage\FieldType\Page\Block\Renderer\Event\PreRenderEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. /**
  14. * Forwards all main request query parameters to block-rendering sub-request.
  15. *
  16. * @internal
  17. */
  18. final class BlockQueryParametersSubscriber implements EventSubscriberInterface
  19. {
  20. private RequestStack $requestStack;
  21. /** @var array<string> */
  22. private array $blockIdentifierList;
  23. /**
  24. * @param array<string> $blockIdentifierList
  25. */
  26. public function __construct(RequestStack $requestStack, array $blockIdentifierList)
  27. {
  28. $this->requestStack = $requestStack;
  29. $this->blockIdentifierList = $blockIdentifierList;
  30. }
  31. public static function getSubscribedEvents(): array
  32. {
  33. return [
  34. BlockRenderEvents::GLOBAL_BLOCK_RENDER_PRE => [
  35. ['onBlockPreRender', -100],
  36. ],
  37. ];
  38. }
  39. public function onBlockPreRender(PreRenderEvent $event): void
  40. {
  41. if (!in_array($event->getBlockValue()->getType(), $this->blockIdentifierList, true)) {
  42. return;
  43. }
  44. // forward query parameters (needed for e.g.: pagination) from the main request to block-rendering sub-request
  45. $mainRequest = $this->requestStack->getMainRequest();
  46. $currentRequest = $this->requestStack->getCurrentRequest();
  47. if (null !== $mainRequest && null !== $currentRequest && $mainRequest !== $currentRequest) {
  48. $this->forwardQueryParameters($mainRequest, $currentRequest);
  49. }
  50. }
  51. private function forwardQueryParameters(Request $fromRequest, Request $toRequest): void
  52. {
  53. $toRequest->query->add($fromRequest->query->all());
  54. }
  55. }