vendor/ibexa/content-forms/src/lib/Content/View/Filter/ContentEditViewFilter.php line 72

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\ContentForms\Content\View\Filter;
  8. use Ibexa\ContentForms\Data\Content\ContentUpdateData;
  9. use Ibexa\ContentForms\Data\Mapper\ContentUpdateMapper;
  10. use Ibexa\ContentForms\Form\Type\Content\ContentEditType;
  11. use Ibexa\Contracts\ContentForms\Event\AutosaveEnabled;
  12. use Ibexa\Contracts\Core\Repository\ContentService;
  13. use Ibexa\Contracts\Core\Repository\ContentTypeService;
  14. use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
  15. use Ibexa\Contracts\Core\Repository\LocationService;
  16. use Ibexa\Contracts\Core\Repository\Values\Content\Content;
  17. use Ibexa\Contracts\Core\Repository\Values\Content\Location;
  18. use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
  19. use Ibexa\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface;
  20. use Ibexa\Core\MVC\Symfony\View\Event\FilterViewBuilderParametersEvent;
  21. use Ibexa\Core\MVC\Symfony\View\ViewEvents;
  22. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  23. use Symfony\Component\Form\FormFactoryInterface;
  24. use Symfony\Component\Form\FormInterface;
  25. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  26. class ContentEditViewFilter implements EventSubscriberInterface
  27. {
  28. private ContentService $contentService;
  29. private ContentTypeService $contentTypeService;
  30. private FormFactoryInterface $formFactory;
  31. private UserLanguagePreferenceProviderInterface $languagePreferenceProvider;
  32. private LocationService $locationService;
  33. private EventDispatcherInterface $eventDispatcher;
  34. public function __construct(
  35. ContentService $contentService,
  36. LocationService $locationService,
  37. ContentTypeService $contentTypeService,
  38. FormFactoryInterface $formFactory,
  39. UserLanguagePreferenceProviderInterface $languagePreferenceProvider,
  40. EventDispatcherInterface $eventDispatcher
  41. ) {
  42. $this->contentService = $contentService;
  43. $this->contentTypeService = $contentTypeService;
  44. $this->formFactory = $formFactory;
  45. $this->languagePreferenceProvider = $languagePreferenceProvider;
  46. $this->locationService = $locationService;
  47. $this->eventDispatcher = $eventDispatcher;
  48. }
  49. public static function getSubscribedEvents()
  50. {
  51. return [ViewEvents::FILTER_BUILDER_PARAMETERS => 'handleContentEditForm'];
  52. }
  53. /**
  54. * @param \Ibexa\Core\MVC\Symfony\View\Event\FilterViewBuilderParametersEvent $event
  55. *
  56. * @throws \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  57. * @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
  58. * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
  59. */
  60. public function handleContentEditForm(FilterViewBuilderParametersEvent $event)
  61. {
  62. if ('ibexa_content_edit:editVersionDraftAction' !== $event->getParameters()->get('_controller')) {
  63. return;
  64. }
  65. $request = $event->getRequest();
  66. $languageCode = $request->attributes->get('language');
  67. $contentId = $request->attributes->getInt('contentId');
  68. $contentDraft = $this->contentService->loadContent(
  69. $contentId,
  70. [$languageCode], // @todo: rename to languageCode in 3.0
  71. $request->attributes->getInt('versionNo')
  72. );
  73. $currentContent = $this->contentService->loadContent($contentId);
  74. $currentFields = $currentContent->getFields();
  75. $contentType = $this->contentTypeService->loadContentType(
  76. $contentDraft->contentInfo->contentTypeId,
  77. $this->languagePreferenceProvider->getPreferredLanguages()
  78. );
  79. try {
  80. $location = $this->locationService->loadLocation(
  81. (int)$event->getParameters()->get(
  82. 'locationId',
  83. $contentDraft->contentInfo->mainLocationId
  84. )
  85. );
  86. } catch (NotFoundException $e) {
  87. }
  88. $contentUpdate = $this->resolveContentEditData(
  89. $contentDraft,
  90. $languageCode,
  91. $contentType,
  92. $currentFields
  93. );
  94. $autosaveEnabled = $this->eventDispatcher->dispatch(new AutosaveEnabled($contentDraft->getVersionInfo()))->isAutosaveEnabled();
  95. $form = $this->resolveContentEditForm(
  96. $contentUpdate,
  97. $languageCode,
  98. $contentDraft,
  99. $location ?? null,
  100. $autosaveEnabled
  101. );
  102. $event->getParameters()->add([
  103. 'form' => $form->handleRequest($request),
  104. 'validate' => (bool)$request->get('validate', false),
  105. ]);
  106. }
  107. /**
  108. * @param array<\Ibexa\Contracts\Core\Repository\Values\Content\Field> $currentFields
  109. */
  110. private function resolveContentEditData(
  111. Content $content,
  112. string $languageCode,
  113. ContentType $contentType,
  114. array $currentFields
  115. ): ContentUpdateData {
  116. $contentUpdateMapper = new ContentUpdateMapper();
  117. return $contentUpdateMapper->mapToFormData($content, [
  118. 'languageCode' => $languageCode,
  119. 'contentType' => $contentType,
  120. 'currentFields' => $currentFields,
  121. ]);
  122. }
  123. /**
  124. * @param \Ibexa\ContentForms\Data\Content\ContentUpdateData $contentUpdate
  125. * @param string $languageCode
  126. * @param \Ibexa\Contracts\Core\Repository\Values\Content\Content $content
  127. *
  128. * @return \Symfony\Component\Form\FormInterface
  129. */
  130. private function resolveContentEditForm(
  131. ContentUpdateData $contentUpdate,
  132. string $languageCode,
  133. Content $content,
  134. ?Location $location = null,
  135. bool $autosaveEnabled = true
  136. ): FormInterface {
  137. return $this->formFactory->create(
  138. ContentEditType::class,
  139. $contentUpdate,
  140. [
  141. 'location' => $location,
  142. 'languageCode' => $languageCode,
  143. 'mainLanguageCode' => $content->contentInfo->mainLanguageCode,
  144. 'content' => $content,
  145. 'contentUpdateStruct' => $contentUpdate,
  146. 'drafts_enabled' => true,
  147. 'autosave_enabled' => $autosaveEnabled,
  148. ]
  149. );
  150. }
  151. }
  152. class_alias(ContentEditViewFilter::class, 'EzSystems\EzPlatformContentForms\Content\View\Filter\ContentEditViewFilter');