From d059304dc5fa02a8c857f9c6f89b397097607f29 Mon Sep 17 00:00:00 2001 From: Maciej Kobus Date: Fri, 9 Mar 2018 12:38:11 +0100 Subject: [PATCH] EZP-28849: Allow repository forms to use View Manager for handling draft edit controller and template dispatching (#215) --- bundle/Controller/ContentEditController.php | 102 ++----- .../Compiler/ViewBuilderRegistryPass.php | 36 +++ .../Configuration/Parser/ContentEditView.php | 16 ++ bundle/EzSystemsRepositoryFormsBundle.php | 4 + .../config/ezpublish_default_settings.yml | 2 + bundle/Resources/config/routing.yml | 4 +- bundle/Resources/config/services.yml | 4 +- bundle/Resources/config/views.yml | 35 +++ .../View/Builder/ContentEditViewBuilder.php | 262 ++++++++++++++++++ lib/Content/View/ContentEditView.php | 88 +++++- .../View/Filter/ContentEditViewFilter.php | 127 +++++++++ lib/Content/View/Provider/Configured.php | 67 +++++ 12 files changed, 662 insertions(+), 85 deletions(-) create mode 100644 bundle/DependencyInjection/Compiler/ViewBuilderRegistryPass.php create mode 100644 bundle/DependencyInjection/Configuration/Parser/ContentEditView.php create mode 100644 bundle/Resources/config/views.yml create mode 100644 lib/Content/View/Builder/ContentEditViewBuilder.php create mode 100644 lib/Content/View/Filter/ContentEditViewFilter.php create mode 100644 lib/Content/View/Provider/Configured.php diff --git a/bundle/Controller/ContentEditController.php b/bundle/Controller/ContentEditController.php index 62bad2a74..323848f32 100644 --- a/bundle/Controller/ContentEditController.php +++ b/bundle/Controller/ContentEditController.php @@ -12,19 +12,15 @@ use eZ\Publish\API\Repository\ContentTypeService; use eZ\Publish\API\Repository\LanguageService; use eZ\Publish\API\Repository\LocationService; -use eZ\Publish\API\Repository\Values\Content\VersionInfo; -use eZ\Publish\Core\Base\Exceptions\BadStateException; use EzSystems\RepositoryForms\Content\View\ContentCreateDraftView; use EzSystems\RepositoryForms\Content\View\ContentCreateView; use EzSystems\RepositoryForms\Content\View\ContentEditView; use EzSystems\RepositoryForms\Data\Content\CreateContentDraftData; use EzSystems\RepositoryForms\Data\Mapper\ContentCreateMapper; -use EzSystems\RepositoryForms\Data\Mapper\ContentUpdateMapper; use EzSystems\RepositoryForms\Form\ActionDispatcher\ActionDispatcherInterface; use EzSystems\RepositoryForms\Form\Type\Content\ContentDraftCreateType; use EzSystems\RepositoryForms\Form\Type\Content\ContentEditType; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Response; class ContentEditController extends Controller { @@ -43,13 +39,6 @@ class ContentEditController extends Controller /** @var ActionDispatcherInterface */ private $contentActionDispatcher; - /** - * @var string - * - * @deprecated Deprecated since 1.10 and will be removed in 2.0. See setPagelayout(). - */ - private $pagelayout; - public function __construct( ContentTypeService $contentTypeService, ContentService $contentService, @@ -70,9 +59,9 @@ public function __construct( * @param int $contentTypeIdentifier ContentType id to create * @param string $language Language code to create the content in (eng-GB, ger-DE, ...)) * @param int $parentLocationId Location the content should be a child of - * @param Request $request + * @param \Symfony\Component\HttpFoundation\Request $request * - * @return \EzSystems\RepositoryForms\Content\View\ContentCreateView|Response + * @return \EzSystems\RepositoryForms\Content\View\ContentCreateView|\Symfony\Component\HttpFoundation\Response * * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentType * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException @@ -116,9 +105,9 @@ public function createWithoutDraftAction($contentTypeIdentifier, $language, $par * @param int $fromVersionNo * @param string $fromLanguage * @param string $toLanguage - * @param Request $request + * @param \Symfony\Component\HttpFoundation\Request $request * - * @return \EzSystems\RepositoryForms\Content\View\ContentCreateDraftView|Response + * @return \EzSystems\RepositoryForms\Content\View\ContentCreateDraftView|\Symfony\Component\HttpFoundation\Response * * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException @@ -168,84 +157,41 @@ public function createContentDraftAction( ]); } + /** + * @param \EzSystems\RepositoryForms\Content\View\ContentEditView $view + * + * @return \EzSystems\RepositoryForms\Content\View\ContentEditView|\Symfony\Component\HttpFoundation\Response + * + * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException + */ + public function editVersionDraftAction(ContentEditView $view) + { + return $view; + } + /** * Shows a content draft editing form. * + * @deprecated In 2.1 and will be removed in 3.0. Please use `editVersionDraftAction()` instead. + * * @param int $contentId ContentType id to create * @param int $versionNo Version number the version should be created from. Defaults to the currently published one. - * @param Request $request * @param string $language Language code to create the version in (eng-GB, ger-DE, ...)) * @param int|null $locationId * - * @return ContentEditView|Response - * @throws BadStateException If the version isn't editable, or if there is no editable version. + * @return \EzSystems\RepositoryForms\Content\View\ContentEditView|\Symfony\Component\HttpFoundation\Response */ public function editContentDraftAction( $contentId, $versionNo = null, - Request $request, $language = null, $locationId = null ) { - $draft = $this->contentService->loadContent($contentId, [$language], $versionNo); - if ($draft->getVersionInfo()->status !== VersionInfo::STATUS_DRAFT) { - throw new BadStateException('Version status', 'status is not draft'); - } - - $referrerLocation = $this->locationService->loadLocation($locationId ?? $draft->contentInfo->mainLocationId); - $language = $language ?: $draft->getVersionInfo()->getContentInfo()->mainLanguageCode; - $language = $this->languageService->loadLanguage($language); - $contentType = $this->contentTypeService->loadContentType($draft->contentInfo->contentTypeId); - - $contentUpdate = (new ContentUpdateMapper())->mapToFormData( - $draft, - [ - 'languageCode' => $language->languageCode, - 'contentType' => $this->contentTypeService->loadContentType($draft->contentInfo->contentTypeId), - ] - ); - $form = $this->createForm( - ContentEditType::class, - $contentUpdate, - [ - 'languageCode' => $language->languageCode, - 'mainLanguageCode' => $draft->contentInfo->mainLanguageCode, - 'drafts_enabled' => true, - ] - ); - $form->handleRequest($request); - - if ($form->isValid() && null !== $form->getClickedButton()) { - $this->contentActionDispatcher->dispatchFormAction( - $form, - $contentUpdate, - $form->getClickedButton()->getName(), - ['referrerLocation' => $referrerLocation] - ); - if ($response = $this->contentActionDispatcher->getResponse()) { - return $response; - } - } - - return new ContentEditView(null, [ - 'form' => $form->createView(), - 'language' => $language, - 'content' => $draft, - 'contentType' => $contentType, - 'location' => $referrerLocation, + return $this->forward('ez_content_edit:editVersionDraftAction', [ + 'contentId' => $contentId, + 'versionNo' => $versionNo, + 'languageCode' => $language, + 'locationId' => $locationId, ]); } - - /** - * @param string $pagelayout - * @return ContentEditController - * - * @deprecated Deprecated since 1.10 and will be removed in 2.0. Pagelayout is injected via ViewTemplatesListener. - */ - public function setPagelayout($pagelayout) - { - $this->pagelayout = $pagelayout; - - return $this; - } } diff --git a/bundle/DependencyInjection/Compiler/ViewBuilderRegistryPass.php b/bundle/DependencyInjection/Compiler/ViewBuilderRegistryPass.php new file mode 100644 index 000000000..b0ef50a41 --- /dev/null +++ b/bundle/DependencyInjection/Compiler/ViewBuilderRegistryPass.php @@ -0,0 +1,36 @@ +hasDefinition(self::VIEW_BUILDER_REGISTRY) || !$container->hasDefinition(ContentEditViewBuilder::class)) { + return; + } + + $registry = $container->findDefinition(self::VIEW_BUILDER_REGISTRY); + + $viewBuilders = [ + $container->getDefinition(self::VIEW_BUILDER_CONTENT_EDIT), + ]; + + $registry->addMethodCall('addToRegistry', [$viewBuilders]); + } +} diff --git a/bundle/DependencyInjection/Configuration/Parser/ContentEditView.php b/bundle/DependencyInjection/Configuration/Parser/ContentEditView.php new file mode 100644 index 000000000..b5fb26fbd --- /dev/null +++ b/bundle/DependencyInjection/Configuration/Parser/ContentEditView.php @@ -0,0 +1,16 @@ +addCompilerPass(new FieldTypeFormMapperDispatcherPass()); $container->addCompilerPass(new LimitationFormMapperPass()); $container->addCompilerPass(new LimitationValueMapperPass()); + $container->addCompilerPass(new ViewBuilderRegistryPass()); $eZExtension = $container->getExtension('ezpublish'); $eZExtension->addPolicyProvider(new UserRegisterPolicyProvider()); @@ -34,6 +37,7 @@ public function build(ContainerBuilder $container) $eZExtension->addConfigParser(new ContentEdit()); $eZExtension->addConfigParser(new UserEdit()); $eZExtension->addConfigParser(new LimitationValueTemplates()); + $eZExtension->addConfigParser(new ContentEditView()); $eZExtension->addDefaultSettings(__DIR__ . '/Resources/config', ['ezpublish_default_settings.yml']); } } diff --git a/bundle/Resources/config/ezpublish_default_settings.yml b/bundle/Resources/config/ezpublish_default_settings.yml index 6c4d0559c..e504054b7 100644 --- a/bundle/Resources/config/ezpublish_default_settings.yml +++ b/bundle/Resources/config/ezpublish_default_settings.yml @@ -9,3 +9,5 @@ parameters: ezsettings.default.user_edit.templates.create: "EzSystemsRepositoryFormsBundle:Content:content_edit.html.twig" ezsettings.default.limitation_value_templates: - { template: 'EzSystemsRepositoryFormsBundle::limitation_values.html.twig', priority: 0 } + + ezsettings.default.content_edit_view: {} diff --git a/bundle/Resources/config/routing.yml b/bundle/Resources/config/routing.yml index b80a7fd97..8d07b960c 100644 --- a/bundle/Resources/config/routing.yml +++ b/bundle/Resources/config/routing.yml @@ -8,8 +8,8 @@ ez_content_create_no_draft: ez_content_draft_edit: path: /content/edit/draft/{contentId}/{versionNo}/{language}/{locationId} defaults: - _controller: ez_content_edit:editContentDraftAction - language: ~ + _controller: ez_content_edit:editVersionDraftAction + language: ~ # @todo rename to languageCode in 3.0 locationId: ~ options: expose: true diff --git a/bundle/Resources/config/services.yml b/bundle/Resources/config/services.yml index 86d187338..f4821b24f 100644 --- a/bundle/Resources/config/services.yml +++ b/bundle/Resources/config/services.yml @@ -4,6 +4,7 @@ imports: - {resource: role.yml} - {resource: fieldtypes.yml} - {resource: form_types.yml} + - {resource: views.yml} parameters: ezrepoforms.field_type_form_mapper.dispatcher.class: EzSystems\RepositoryForms\FieldType\FieldTypeFormMapperDispatcher @@ -259,8 +260,6 @@ services: - "@ezpublish.api.service.language" - "@ezrepoforms.action_dispatcher.content" parent: ezpublish.controller.base - calls: - - [setPageLayout, [$pagelayout$]] ezrepoforms.controller.user: class: "%ezrepoforms.controller.user.class%" @@ -313,7 +312,6 @@ services: - [setViewTemplate, ['EzSystems\RepositoryForms\User\View\UserUpdateView', "$user_edit.templates.update$"]] - [setViewTemplate, ['EzSystems\RepositoryForms\User\View\UserRegisterFormView', "$user_registration.templates.form$"]] - [setViewTemplate, ['EzSystems\RepositoryForms\User\View\UserRegisterConfirmView', "$user_registration.templates.confirmation$"]] - - [setViewTemplate, ['EzSystems\RepositoryForms\Content\View\ContentEditView', "$content_edit.templates.edit$"]] - [setViewTemplate, ['EzSystems\RepositoryForms\Content\View\ContentCreateView', "$content_edit.templates.create$"]] - [setViewTemplate, ['EzSystems\RepositoryForms\Content\View\ContentCreateDraftView', "$content_edit.templates.create_draft$"]] - [setPagelayout, ["$pagelayout$"]] diff --git a/bundle/Resources/config/views.yml b/bundle/Resources/config/views.yml new file mode 100644 index 000000000..19213b20d --- /dev/null +++ b/bundle/Resources/config/views.yml @@ -0,0 +1,35 @@ +services: + _defaults: + public: false + autowire: true + autoconfigure: true + + EzSystems\RepositoryForms\Content\View\Builder\ContentEditViewBuilder: + arguments: + - '@ezpublish.api.repository' + - '@ezpublish.view.configurator' + - '@ezpublish.view.view_parameters.injector.dispatcher' + - '$content_edit.templates.edit$' + - '@ezrepoforms.action_dispatcher.content' + + EzSystems\RepositoryForms\Content\View\Provider\Configured: + arguments: + - '@ezplatform.repository_forms.content_edit_view.matcher_factory' + tags: + - { name: ezpublish.view_provider, type: EzSystems\RepositoryForms\Content\View\ContentEditView, priority: 10 } + + ezplatform.repository_forms.content_edit_view.matcher_factory: + class: '%ezpublish.view.matcher_factory.class%' + arguments: + - '@ezpublish.api.repository' + - 'eZ\Publish\Core\MVC\Symfony\Matcher\ContentBased' + calls: + - [setContainer, ['@service_container']] + - [setMatchConfig, ['$content_edit_view$']] + + EzSystems\RepositoryForms\Content\View\Filter\ContentEditViewFilter: + arguments: + - '@ezpublish.api.service.content' + - '@ezpublish.api.service.content_type' + tags: + - { name: kernel.event_subscriber } diff --git a/lib/Content/View/Builder/ContentEditViewBuilder.php b/lib/Content/View/Builder/ContentEditViewBuilder.php new file mode 100644 index 000000000..cf848b245 --- /dev/null +++ b/lib/Content/View/Builder/ContentEditViewBuilder.php @@ -0,0 +1,262 @@ +repository = $repository; + $this->viewConfigurator = $viewConfigurator; + $this->viewParametersInjector = $viewParametersInjector; + $this->defaultTemplate = $defaultTemplate; + $this->contentActionDispatcher = $contentActionDispatcher; + } + + public function matches($argument) + { + return 'ez_content_edit:editVersionDraftAction' === $argument; + } + + /** + * @param array $parameters + * + * @return \eZ\Publish\Core\MVC\Symfony\View\ContentView|\eZ\Publish\Core\MVC\Symfony\View\View + * + * @throws \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException + * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException + * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException + * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException + */ + public function buildView(array $parameters) + { + // @todo improve default templates injection + $view = new ContentEditView($this->defaultTemplate); + + $language = $this->resolveLanguage($parameters); + $location = $this->resolveLocation($parameters); + $content = $this->resolveContent($parameters, $location, $language); + $contentType = $this->loadContentType((int) $content->contentInfo->contentTypeId); + $form = $parameters['form']; + + if (!$content->getVersionInfo()->isDraft()) { + throw new InvalidArgumentException('Version', 'status is not draft'); + } + + if (null === $location) { // assume main location if no location was provided + $location = $this->loadLocation((int) $content->contentInfo->mainLocationId); + } + + if ($location->contentId !== $content->id) { + throw new InvalidArgumentException('Location', 'Provided location does not belong to selected content'); + } + + if ($form->isValid() && null !== $form->getClickedButton()) { + $this->contentActionDispatcher->dispatchFormAction( + $form, + $form->getData(), + $form->getClickedButton()->getName(), + ['referrerLocation' => $location] + ); + + if ($response = $this->contentActionDispatcher->getResponse()) { + $view->setResponse($response); + } + } + + $view->setContent($content); + $view->setLanguage($language); + $view->setLocation($location); + $view->setForm($parameters['form']); + + $view->addParameters([ + 'content' => $content, + 'location' => $location, + 'language' => $language, + 'contentType' => $contentType, + 'form' => $form->createView(), + ]); + + $this->viewParametersInjector->injectViewParameters($view, $parameters); + $this->viewConfigurator->configure($view); + + return $view; + } + + /** + * Loads Content with id $contentId. + * + * @param int $contentId + * @param array $languages + * @param int|null $versionNo + * + * @return \eZ\Publish\API\Repository\Values\Content\Content + * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException + * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException + */ + private function loadContent(int $contentId, array $languages = [], int $versionNo = null): Content + { + return $this->repository->getContentService()->loadContent($contentId, $languages, $versionNo); + } + + /** + * Loads a visible Location. + * + * @param int $locationId + * + * @return \eZ\Publish\API\Repository\Values\Content\Location + * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException + */ + private function loadLocation(int $locationId): Location + { + return $this->repository->getLocationService()->loadLocation($locationId); + } + + /** + * Loads Language with code $languageCode. + * + * @param string $languageCode + * + * @return \eZ\Publish\API\Repository\Values\Content\Language + * + * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException + */ + private function loadLanguage(string $languageCode): Language + { + return $this->repository->getContentLanguageService()->loadLanguage($languageCode); + } + + /** + * Loads ContentType with id $contentTypeId. + * + * @param int $contentTypeId + * + * @return \eZ\Publish\API\Repository\Values\ContentType\ContentType + * + * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException + */ + private function loadContentType(int $contentTypeId): ContentType + { + return $this->repository->getContentTypeService()->loadContentType($contentTypeId); + } + + /** + * @param array $parameters + * + * @return \eZ\Publish\API\Repository\Values\Content\Language + * + * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException + */ + private function resolveLanguage(array $parameters): Language + { + if (isset($parameters['languageCode'])) { + return $this->loadLanguage($parameters['languageCode']); + } + + if (isset($parameters['language'])) { + if (is_string($parameters['language'])) { + // @todo BC: route parameter should be called languageCode but it won't happen until 3.0 + return $this->loadLanguage($parameters['language']); + } + + return $parameters['language']; + } + + throw new InvalidArgumentException('Language', + 'No language information provided. Are you missing language or languageCode parameters'); + } + + /** + * @param array $parameters + * @param \eZ\Publish\API\Repository\Values\Content\Location|null $location + * @param \eZ\Publish\API\Repository\Values\Content\Language $language + * + * @return \eZ\Publish\API\Repository\Values\Content\Content + * + * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException + */ + private function resolveContent(array $parameters, ?Location $location, Language $language): Content + { + if (isset($parameters['content'])) { + return $parameters['content']; + } + + if (isset($parameters['contentId'])) { + $contentId = $parameters['contentId']; + } elseif (null !== $location) { + $contentId = $location->contentId; + } else { + throw new InvalidArgumentException( + 'Content', + 'No content could be loaded from parameters' + ); + } + + return $this->loadContent( + (int) $contentId, + null !== $language ? [$language->languageCode] : [], + (int) $parameters['versionNo'] ?: null + ); + } + + /** + * @param array $parameters + * + * @return \eZ\Publish\API\Repository\Values\Content\Location|null + */ + private function resolveLocation(array $parameters): ?Location + { + if (isset($parameters['locationId'])) { + return $this->loadLocation((int) $parameters['locationId']); + } + + if (isset($parameters['location'])) { + return $parameters['location']; + } + + return null; + } +} diff --git a/lib/Content/View/ContentEditView.php b/lib/Content/View/ContentEditView.php index ac9948e2b..99de2f539 100644 --- a/lib/Content/View/ContentEditView.php +++ b/lib/Content/View/ContentEditView.php @@ -3,11 +3,95 @@ * @copyright Copyright (C) eZ Systems AS. All rights reserved. * @license For full copyright and license information view LICENSE file distributed with this source code. */ +declare(strict_types=1); + namespace EzSystems\RepositoryForms\Content\View; +use eZ\Publish\API\Repository\Values\Content\Content; +use eZ\Publish\API\Repository\Values\Content\Language; +use eZ\Publish\API\Repository\Values\Content\Location; use eZ\Publish\Core\MVC\Symfony\View\BaseView; -use eZ\Publish\Core\MVC\Symfony\View\View; +use eZ\Publish\Core\MVC\Symfony\View\ContentValueView; +use eZ\Publish\Core\MVC\Symfony\View\LocationValueView; +use Symfony\Component\Form\FormInterface; -class ContentEditView extends BaseView implements View +class ContentEditView extends BaseView implements ContentValueView, LocationValueView { + /** @var \eZ\Publish\API\Repository\Values\Content\Content */ + private $content; + + /** @var \eZ\Publish\API\Repository\Values\Content\Location */ + private $location; + + /** @var \eZ\Publish\API\Repository\Values\Content\Language */ + private $language; + + /** @var \Symfony\Component\Form\FormInterface */ + private $form; + + /** + * @param \eZ\Publish\API\Repository\Values\Content\Content $content + */ + public function setContent(Content $content) + { + $this->content = $content; + } + + /** + * Returns the Content. + * + * @return \eZ\Publish\API\Repository\Values\Content\Content + */ + public function getContent(): Content + { + return $this->content; + } + + /** + * @param \eZ\Publish\API\Repository\Values\Content\Location $location + */ + public function setLocation(Location $location) + { + $this->location = $location; + } + + /** + * @return \eZ\Publish\API\Repository\Values\Content\Location + */ + public function getLocation(): Location + { + return $this->location; + } + + /** + * @return \eZ\Publish\API\Repository\Values\Content\Language + */ + public function getLanguage(): Language + { + return $this->language; + } + + /** + * @param \eZ\Publish\API\Repository\Values\Content\Language $language + */ + public function setLanguage(Language $language) + { + $this->language = $language; + } + + /** + * @return \Symfony\Component\Form\FormInterface + */ + public function getForm(): FormInterface + { + return $this->form; + } + + /** + * @param \Symfony\Component\Form\FormInterface $form + */ + public function setForm(FormInterface $form) + { + $this->form = $form; + } } diff --git a/lib/Content/View/Filter/ContentEditViewFilter.php b/lib/Content/View/Filter/ContentEditViewFilter.php new file mode 100644 index 000000000..288a93bbe --- /dev/null +++ b/lib/Content/View/Filter/ContentEditViewFilter.php @@ -0,0 +1,127 @@ +contentService = $contentService; + $this->contentTypeService = $contentTypeService; + $this->formFactory = $formFactory; + } + + public static function getSubscribedEvents() + { + return [ViewEvents::FILTER_BUILDER_PARAMETERS => 'handleContentEditForm']; + } + + /** + * @param \eZ\Publish\Core\MVC\Symfony\View\Event\FilterViewBuilderParametersEvent $event + * + * @throws \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException + * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException + * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException + */ + public function handleContentEditForm(FilterViewBuilderParametersEvent $event) + { + if ('ez_content_edit:editVersionDraftAction' !== $event->getParameters()->get('_controller')) { + return; + } + + $request = $event->getRequest(); + $languageCode = $request->attributes->get('language'); + $contentDraft = $this->contentService->loadContent( + $request->attributes->get('contentId'), + [$languageCode], // @todo: rename to languageCode in 3.0 + $request->attributes->get('versionNo') + ); + $contentType = $this->contentTypeService->loadContentType($contentDraft->contentInfo->contentTypeId); + + $contentUpdate = $this->resolveContentEditData($contentDraft, $languageCode, $contentType); + $form = $this->resolveContentEditForm($contentUpdate, $languageCode, $contentDraft); + + $event->getParameters()->add(['form' => $form->handleRequest($request)]); + } + + /** + * @param \eZ\Publish\API\Repository\Values\Content\Content $content + * @param string $languageCode + * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType + * + * @return \EzSystems\RepositoryForms\Data\Content\ContentUpdateData + */ + private function resolveContentEditData( + Content $content, + string $languageCode, + ContentType $contentType + ): ContentUpdateData { + $contentUpdateMapper = new ContentUpdateMapper(); + + return $contentUpdateMapper->mapToFormData($content, [ + 'languageCode' => $languageCode, + 'contentType' => $contentType, + ]); + } + + /** + * @param \EzSystems\RepositoryForms\Data\Content\ContentUpdateData $contentUpdate + * @param string $languageCode + * @param \eZ\Publish\API\Repository\Values\Content\Content $content + * + * @return \Symfony\Component\Form\FormInterface + * + * @throws \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException + */ + private function resolveContentEditForm( + ContentUpdateData $contentUpdate, + string $languageCode, + Content $content + ): FormInterface { + return $this->formFactory->create( + ContentEditType::class, + $contentUpdate, + [ + 'languageCode' => $languageCode, + 'mainLanguageCode' => $content->contentInfo->mainLanguageCode, + 'drafts_enabled' => true, + ] + ); + } +} diff --git a/lib/Content/View/Provider/Configured.php b/lib/Content/View/Provider/Configured.php new file mode 100644 index 000000000..b0c6d4c02 --- /dev/null +++ b/lib/Content/View/Provider/Configured.php @@ -0,0 +1,67 @@ +matcherFactory = $matcherFactory; + } + + public function getView(View $view) + { + if (($configHash = $this->matcherFactory->match($view)) === null) { + return null; + } + + return $this->buildContentEditView($configHash); + } + + /** + * Builds a ContentEditView object from $viewConfig. + * + * @param array $viewConfig + * + * @return \EzSystems\RepositoryForms\Content\View\ContentEditView + */ + protected function buildContentEditView(array $viewConfig): ContentEditView + { + $view = new ContentEditView(); + $view->setConfigHash($viewConfig); + if (isset($viewConfig['template'])) { + $view->setTemplateIdentifier($viewConfig['template']); + } + if (isset($viewConfig['controller'])) { + $view->setControllerReference(new ControllerReference($viewConfig['controller'])); + } + if (isset($viewConfig['params']) && is_array($viewConfig['params'])) { + $view->addParameters($viewConfig['params']); + } + + return $view; + } +}