Skip to content

Commit

Permalink
EZP-28849: Allow repository forms to use View Manager for handling dr…
Browse files Browse the repository at this point in the history
…aft edit controller and template dispatching (ezsystems#215)
  • Loading branch information
webhdx authored and Łukasz Serwatka committed Mar 9, 2018
1 parent b79b0b8 commit d059304
Show file tree
Hide file tree
Showing 12 changed files with 662 additions and 85 deletions.
102 changes: 24 additions & 78 deletions bundle/Controller/ContentEditController.php
Expand Up @@ -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
{
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
36 changes: 36 additions & 0 deletions bundle/DependencyInjection/Compiler/ViewBuilderRegistryPass.php
@@ -0,0 +1,36 @@
<?php
/**
* This file is part of the eZ RepositoryForms package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\RepositoryFormsBundle\DependencyInjection\Compiler;

use EzSystems\RepositoryForms\Content\View\Builder\ContentEditViewBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Compiler pass to add View Builders to ViewBuilderRegistry.
*/
class ViewBuilderRegistryPass implements CompilerPassInterface
{
const VIEW_BUILDER_REGISTRY = 'ezpublish.view_builder.registry';
const VIEW_BUILDER_CONTENT_EDIT = ContentEditViewBuilder::class;

public function process(ContainerBuilder $container)
{
if (!$container->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]);
}
}
@@ -0,0 +1,16 @@
<?php
/**
* This file is part of the eZ RepositoryForms package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\RepositoryFormsBundle\DependencyInjection\Configuration\Parser;

use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\Parser\View;

class ContentEditView extends View
{
const NODE_KEY = 'content_edit_view';
const INFO = 'Template selection settings when displaying a content edit form';
}
4 changes: 4 additions & 0 deletions bundle/EzSystemsRepositoryFormsBundle.php
Expand Up @@ -12,7 +12,9 @@
use EzSystems\RepositoryFormsBundle\DependencyInjection\Compiler\FieldTypeFormMapperDispatcherPass;
use EzSystems\RepositoryFormsBundle\DependencyInjection\Compiler\LimitationFormMapperPass;
use EzSystems\RepositoryFormsBundle\DependencyInjection\Compiler\LimitationValueMapperPass;
use EzSystems\RepositoryFormsBundle\DependencyInjection\Compiler\ViewBuilderRegistryPass;
use EzSystems\RepositoryFormsBundle\DependencyInjection\Configuration\Parser\ContentEdit;
use EzSystems\RepositoryFormsBundle\DependencyInjection\Configuration\Parser\ContentEditView;
use EzSystems\RepositoryFormsBundle\DependencyInjection\Configuration\Parser\LimitationValueTemplates;
use EzSystems\RepositoryFormsBundle\DependencyInjection\Configuration\Parser\UserEdit;
use EzSystems\RepositoryFormsBundle\DependencyInjection\Configuration\Parser\UserRegistration;
Expand All @@ -27,13 +29,15 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new FieldTypeFormMapperDispatcherPass());
$container->addCompilerPass(new LimitationFormMapperPass());
$container->addCompilerPass(new LimitationValueMapperPass());
$container->addCompilerPass(new ViewBuilderRegistryPass());

$eZExtension = $container->getExtension('ezpublish');
$eZExtension->addPolicyProvider(new UserRegisterPolicyProvider());
$eZExtension->addConfigParser(new UserRegistration());
$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']);
}
}
2 changes: 2 additions & 0 deletions bundle/Resources/config/ezpublish_default_settings.yml
Expand Up @@ -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: {}
4 changes: 2 additions & 2 deletions bundle/Resources/config/routing.yml
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions bundle/Resources/config/services.yml
Expand Up @@ -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
Expand Down Expand Up @@ -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%"
Expand Down Expand Up @@ -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$"]]
Expand Down
35 changes: 35 additions & 0 deletions 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 }

0 comments on commit d059304

Please sign in to comment.