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.         $contentDraft $this->contentService->loadContent(
  73.             $request->attributes->getInt('contentId'),
  74.             [$languageCode], // @todo: rename to languageCode in 3.0
  75.             $request->attributes->getInt('versionNo')
  76.         );
  77.         $contentType $this->contentTypeService->loadContentType(
  78.             $contentDraft->contentInfo->contentTypeId,
  79.             $this->languagePreferenceProvider->getPreferredLanguages()
  80.         );
  81.         try {
  82.             $location $this->locationService->loadLocation(
  83.                 (int)$event->getParameters()->get(
  84.                     'locationId',
  85.                     $contentDraft->contentInfo->mainLocationId
  86.                 )
  87.             );
  88.         } catch (NotFoundException $e) {
  89.         }
  90.         $contentUpdate $this->resolveContentEditData($contentDraft$languageCode$contentType);
  91.         $form $this->resolveContentEditForm(
  92.             $contentUpdate,
  93.             $languageCode,
  94.             $contentDraft,
  95.             $location ?? null
  96.         );
  97.         $event->getParameters()->add([
  98.             'form' => $form->handleRequest($request),
  99.             'validate' => (bool)$request->get('validate'false),
  100.         ]);
  101.     }
  102.     /**
  103.      * @param \Ibexa\Contracts\Core\Repository\Values\Content\Content $content
  104.      * @param string $languageCode
  105.      * @param \Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType $contentType
  106.      *
  107.      * @return \Ibexa\ContentForms\Data\Content\ContentUpdateData
  108.      */
  109.     private function resolveContentEditData(
  110.         Content $content,
  111.         string $languageCode,
  112.         ContentType $contentType
  113.     ): ContentUpdateData {
  114.         $contentUpdateMapper = new ContentUpdateMapper();
  115.         return $contentUpdateMapper->mapToFormData($content, [
  116.             'languageCode' => $languageCode,
  117.             'contentType' => $contentType,
  118.         ]);
  119.     }
  120.     /**
  121.      * @param \Ibexa\ContentForms\Data\Content\ContentUpdateData $contentUpdate
  122.      * @param string $languageCode
  123.      * @param \Ibexa\Contracts\Core\Repository\Values\Content\Content $content
  124.      *
  125.      * @return \Symfony\Component\Form\FormInterface
  126.      */
  127.     private function resolveContentEditForm(
  128.         ContentUpdateData $contentUpdate,
  129.         string $languageCode,
  130.         Content $content,
  131.         ?Location $location null
  132.     ): FormInterface {
  133.         return $this->formFactory->create(
  134.             ContentEditType::class,
  135.             $contentUpdate,
  136.             [
  137.                 'location' => $location,
  138.                 'languageCode' => $languageCode,
  139.                 'mainLanguageCode' => $content->contentInfo->mainLanguageCode,
  140.                 'content' => $content,
  141.                 'contentUpdateStruct' => $contentUpdate,
  142.                 'drafts_enabled' => true,
  143.             ]
  144.         );
  145.     }
  146. }
  147. class_alias(ContentEditViewFilter::class, 'EzSystems\EzPlatformContentForms\Content\View\Filter\ContentEditViewFilter');