vendor/ibexa/taxonomy/src/lib/Event/Subscriber/ContentFormEventsSubscriber.php line 45

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\Taxonomy\Event\Subscriber;
  8. use Ibexa\ContentForms\Data\Content\ContentUpdateData;
  9. use Ibexa\ContentForms\Event\ContentFormEvents;
  10. use Ibexa\ContentForms\Event\FormActionEvent;
  11. use Ibexa\Taxonomy\Extractor\TaxonomyEntryExtractorInterface;
  12. use Ibexa\Taxonomy\Service\TaxonomyConfiguration;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\RedirectResponse;
  15. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  16. final class ContentFormEventsSubscriber implements EventSubscriberInterface
  17. {
  18. private TaxonomyConfiguration $taxonomyConfiguration;
  19. private TaxonomyEntryExtractorInterface $taxonomyEntryExtractor;
  20. private UrlGeneratorInterface $urlGenerator;
  21. public function __construct(
  22. TaxonomyConfiguration $taxonomyConfiguration,
  23. TaxonomyEntryExtractorInterface $taxonomyEntryExtractor,
  24. UrlGeneratorInterface $urlGenerator
  25. ) {
  26. $this->taxonomyConfiguration = $taxonomyConfiguration;
  27. $this->taxonomyEntryExtractor = $taxonomyEntryExtractor;
  28. $this->urlGenerator = $urlGenerator;
  29. }
  30. public static function getSubscribedEvents(): array
  31. {
  32. return [
  33. ContentFormEvents::CONTENT_CANCEL => ['onContentFormCancel', -10],
  34. ];
  35. }
  36. public function onContentFormCancel(FormActionEvent $event): void
  37. {
  38. $data = $event->getData();
  39. if (!$data instanceof ContentUpdateData || !$event->hasPayload('content')) {
  40. return;
  41. }
  42. if (null !== $event->getOption('referrerLocation')) {
  43. return;
  44. }
  45. if (!$this->taxonomyConfiguration->isContentTypeAssociatedWithTaxonomy($data->contentDraft->getContentType())) {
  46. return;
  47. }
  48. $contentInfo = $this->taxonomyEntryExtractor->extractEntryParentFromContentUpdateData($data);
  49. if (null === $contentInfo) {
  50. return;
  51. }
  52. $url = $this->urlGenerator->generate(
  53. 'ibexa.content.view',
  54. [
  55. 'contentId' => $contentInfo->id,
  56. 'locationId' => $contentInfo->mainLocationId,
  57. ],
  58. UrlGeneratorInterface::ABSOLUTE_URL
  59. );
  60. $event->setResponse(new RedirectResponse($url));
  61. }
  62. }