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

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