vendor/ibexa/site-context/src/bundle/EventSubscriber/SystemPreviewSubscriber.php line 31

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\SiteContext\EventSubscriber;
  8. use Ibexa\Contracts\SiteContext\Event\ResolveLocationPreviewUrlEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  11. final class SystemPreviewSubscriber implements EventSubscriberInterface
  12. {
  13. private UrlGeneratorInterface $urlGenerator;
  14. public function __construct(UrlGeneratorInterface $urlGenerator)
  15. {
  16. $this->urlGenerator = $urlGenerator;
  17. }
  18. public static function getSubscribedEvents(): array
  19. {
  20. return [
  21. ResolveLocationPreviewUrlEvent::class => ['onResolveLocationPreviewUrl', -100],
  22. ];
  23. }
  24. public function onResolveLocationPreviewUrl(ResolveLocationPreviewUrlEvent $event): void
  25. {
  26. if ($event->getPreviewUrl() !== null) {
  27. return;
  28. }
  29. $content = $event->getLocation()->getContent();
  30. $context = $event->getContext();
  31. $previewUrl = $this->urlGenerator->generate(
  32. 'ibexa.version.preview',
  33. [
  34. 'contentId' => $content->id,
  35. 'versionNo' => $content->getVersionInfo()->versionNo,
  36. 'language' => $context['language'],
  37. 'siteAccessName' => $context['siteaccess'] ?? null,
  38. ],
  39. UrlGeneratorInterface::ABSOLUTE_URL
  40. );
  41. $event->setPreviewUrl($previewUrl);
  42. }
  43. }