vendor/ezsystems/repository-forms/lib/Content/View/Filter/ContentCreateViewFilter.php line 69

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) eZ Systems 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 EzSystems\RepositoryForms\Content\View\Filter;
  8. use eZ\Publish\API\Repository\ContentTypeService;
  9. use eZ\Publish\API\Repository\LocationService;
  10. use eZ\Publish\API\Repository\Values\Content\Location;
  11. use eZ\Publish\API\Repository\Values\ContentType\ContentType;
  12. use eZ\Publish\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface;
  13. use eZ\Publish\Core\MVC\Symfony\View\Event\FilterViewBuilderParametersEvent;
  14. use eZ\Publish\Core\MVC\Symfony\View\ViewEvents;
  15. use EzSystems\RepositoryForms\Data\Content\ContentCreateData;
  16. use EzSystems\RepositoryForms\Data\Mapper\ContentCreateMapper;
  17. use EzSystems\RepositoryForms\Form\Type\Content\ContentEditType;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. use Symfony\Component\Form\FormFactoryInterface;
  20. use Symfony\Component\Form\FormInterface;
  21. class ContentCreateViewFilter implements EventSubscriberInterface
  22. {
  23.     /** @var \eZ\Publish\API\Repository\LocationService */
  24.     private $locationService;
  25.     /** @var \eZ\Publish\API\Repository\ContentTypeService */
  26.     private $contentTypeService;
  27.     /** @var \Symfony\Component\Form\FormFactoryInterface */
  28.     private $formFactory;
  29.     /** @var \eZ\Publish\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface */
  30.     private $languagePreferenceProvider;
  31.     /**
  32.      * @param \eZ\Publish\API\Repository\LocationService $locationService
  33.      * @param \eZ\Publish\API\Repository\ContentTypeService $contentTypeService
  34.      * @param \Symfony\Component\Form\FormFactoryInterface $formFactory
  35.      * @param \eZ\Publish\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface $languagePreferenceProvider
  36.      */
  37.     public function __construct(
  38.         LocationService $locationService,
  39.         ContentTypeService $contentTypeService,
  40.         FormFactoryInterface $formFactory,
  41.         UserLanguagePreferenceProviderInterface $languagePreferenceProvider
  42.     ) {
  43.         $this->locationService $locationService;
  44.         $this->contentTypeService $contentTypeService;
  45.         $this->formFactory $formFactory;
  46.         $this->languagePreferenceProvider $languagePreferenceProvider;
  47.     }
  48.     public static function getSubscribedEvents()
  49.     {
  50.         return [ViewEvents::FILTER_BUILDER_PARAMETERS => 'handleContentCreateForm'];
  51.     }
  52.     /**
  53.      * @param \eZ\Publish\Core\MVC\Symfony\View\Event\FilterViewBuilderParametersEvent $event
  54.      *
  55.      * @throws \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  56.      * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
  57.      * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
  58.      */
  59.     public function handleContentCreateForm(FilterViewBuilderParametersEvent $event)
  60.     {
  61.         if ('ez_content_edit:createWithoutDraftAction' !== $event->getParameters()->get('_controller')) {
  62.             return;
  63.         }
  64.         $request $event->getRequest();
  65.         $languageCode $request->attributes->get('language');
  66.         $contentType $this->contentTypeService->loadContentTypeByIdentifier(
  67.             $request->attributes->get('contentTypeIdentifier'),
  68.             $this->languagePreferenceProvider->getPreferredLanguages()
  69.         );
  70.         $location $this->locationService->loadLocation($request->attributes->get('parentLocationId'));
  71.         $contentCreateData $this->resolveContentCreateData($contentType$location$languageCode);
  72.         $form $this->resolveContentCreateForm(
  73.             $contentCreateData,
  74.             $languageCode
  75.         );
  76.         $event->getParameters()->add(['form' => $form->handleRequest($request)]);
  77.     }
  78.     /**
  79.      * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType
  80.      * @param \eZ\Publish\API\Repository\Values\Content\Location $location
  81.      * @param string $languageCode
  82.      *
  83.      * @return \EzSystems\RepositoryForms\Data\Content\ContentCreateData
  84.      */
  85.     private function resolveContentCreateData(
  86.         ContentType $contentType,
  87.         Location $location,
  88.         string $languageCode
  89.     ): ContentCreateData {
  90.         $contentCreateMapper = new ContentCreateMapper();
  91.         return $contentCreateMapper->mapToFormData(
  92.             $contentType,
  93.             [
  94.                 'mainLanguageCode' => $languageCode,
  95.                 'parentLocation' => $this->locationService->newLocationCreateStruct($location->id$contentType),
  96.             ]
  97.         );
  98.     }
  99.     /**
  100.      * @param \EzSystems\RepositoryForms\Data\Content\ContentCreateData $contentCreateData
  101.      * @param string $languageCode
  102.      *
  103.      * @return \Symfony\Component\Form\FormInterface
  104.      */
  105.     private function resolveContentCreateForm(
  106.         ContentCreateData $contentCreateData,
  107.         string $languageCode
  108.     ): FormInterface {
  109.         return $this->formFactory->create(ContentEditType::class, $contentCreateData, [
  110.             'languageCode' => $languageCode,
  111.             'mainLanguageCode' => $languageCode,
  112.             'drafts_enabled' => true,
  113.         ]);
  114.     }
  115. }