Skip to content

Commit

Permalink
EZP-28861: Allow template and controller dispatching based on matcher…
Browse files Browse the repository at this point in the history
… configuration for Content Create (ezsystems#217)
  • Loading branch information
webhdx authored and Łukasz Serwatka committed Mar 12, 2018
1 parent d4091bf commit 801d9ef
Show file tree
Hide file tree
Showing 16 changed files with 600 additions and 59 deletions.
55 changes: 4 additions & 51 deletions bundle/Controller/ContentEditController.php
Expand Up @@ -10,16 +10,12 @@
use eZ\Bundle\EzPublishCoreBundle\Controller;
use eZ\Publish\API\Repository\ContentService;
use eZ\Publish\API\Repository\ContentTypeService;
use eZ\Publish\API\Repository\LanguageService;
use eZ\Publish\API\Repository\LocationService;
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\Form\ActionDispatcher\ActionDispatcherInterface;
use EzSystems\RepositoryForms\Form\Type\Content\ContentDraftCreateType;
use EzSystems\RepositoryForms\Form\Type\Content\ContentEditType;
use Symfony\Component\HttpFoundation\Request;

class ContentEditController extends Controller
Expand All @@ -30,72 +26,29 @@ class ContentEditController extends Controller
/** @var ContentService */
private $contentService;

/** @var LocationService */
private $locationService;

/** @var LanguageService */
private $languageService;

/** @var ActionDispatcherInterface */
private $contentActionDispatcher;

public function __construct(
ContentTypeService $contentTypeService,
ContentService $contentService,
LocationService $locationService,
LanguageService $languageService,
ActionDispatcherInterface $contentActionDispatcher
) {
$this->contentTypeService = $contentTypeService;
$this->locationService = $locationService;
$this->languageService = $languageService;
$this->contentActionDispatcher = $contentActionDispatcher;
$this->contentService = $contentService;
$this->contentActionDispatcher = $contentActionDispatcher;
}

/**
* Displays and processes a content creation form. Showing the form does not create a draft in the repository.
*
* @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 \Symfony\Component\HttpFoundation\Request $request
* @param \EzSystems\RepositoryForms\Content\View\ContentCreateView $view
*
* @return \EzSystems\RepositoryForms\Content\View\ContentCreateView|\Symfony\Component\HttpFoundation\Response
*
* @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentType
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
*/
public function createWithoutDraftAction($contentTypeIdentifier, $language, $parentLocationId, Request $request)
public function createWithoutDraftAction(ContentCreateView $view)
{
$language = $this->languageService->loadLanguage($language);
$parentLocation = $this->locationService->loadLocation($parentLocationId);
$contentType = $this->contentTypeService->loadContentTypeByIdentifier($contentTypeIdentifier);
$data = (new ContentCreateMapper())->mapToFormData($contentType, [
'mainLanguageCode' => $language->languageCode,
'parentLocation' => $this->locationService->newLocationCreateStruct($parentLocationId),
]);
$form = $this->createForm(ContentEditType::class, $data, [
'languageCode' => $language->languageCode,
'mainLanguageCode' => $language->languageCode,
'drafts_enabled' => true,
]);
$form->handleRequest($request);

if ($form->isValid() && null !== $form->getClickedButton()) {
$this->contentActionDispatcher->dispatchFormAction($form, $data, $form->getClickedButton()->getName());
if ($response = $this->contentActionDispatcher->getResponse()) {
return $response;
}
}

return new ContentCreateView(null, [
'form' => $form->createView(),
'language' => $language,
'contentType' => $contentType,
'parentLocation' => $parentLocation,
]);
return $view;
}

/**
Expand Down
Expand Up @@ -7,6 +7,7 @@
*/
namespace EzSystems\RepositoryFormsBundle\DependencyInjection\Compiler;

use EzSystems\RepositoryForms\Content\View\Builder\ContentCreateViewBuilder;
use EzSystems\RepositoryForms\Content\View\Builder\ContentEditViewBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -18,17 +19,23 @@ class ViewBuilderRegistryPass implements CompilerPassInterface
{
const VIEW_BUILDER_REGISTRY = 'ezpublish.view_builder.registry';
const VIEW_BUILDER_CONTENT_EDIT = ContentEditViewBuilder::class;
const VIEW_BUILDER_CONTENT_CREATE = ContentCreateViewBuilder::class;

public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition(self::VIEW_BUILDER_REGISTRY) || !$container->hasDefinition(ContentEditViewBuilder::class)) {
if (
!$container->hasDefinition(self::VIEW_BUILDER_REGISTRY)
|| !$container->hasDefinition(self::VIEW_BUILDER_CONTENT_EDIT)
|| !$container->hasDefinition(self::VIEW_BUILDER_CONTENT_CREATE)
) {
return;
}

$registry = $container->findDefinition(self::VIEW_BUILDER_REGISTRY);

$viewBuilders = [
$container->getDefinition(self::VIEW_BUILDER_CONTENT_EDIT),
$container->getDefinition(self::VIEW_BUILDER_CONTENT_CREATE),
];

$registry->addMethodCall('addToRegistry', [$viewBuilders]);
Expand Down
@@ -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 ContentCreateView extends View
{
const NODE_KEY = 'content_create_view';
const INFO = 'Template selection settings when displaying a content create form';
}
Expand Up @@ -22,7 +22,8 @@ public function addSemanticConfig(NodeBuilder $nodeBuilder)
{
$nodeBuilder
->arrayNode('content_edit')
->info('Content edit configuration')
->info('Content edit configuration.')
->setDeprecated('This key was deprecated in 2.1 and will be removed in 3.0. Please use siteaccess aware configuration.')
->children()
->arrayNode('templates')
->info('Content edit templates.')
Expand Down
2 changes: 2 additions & 0 deletions bundle/EzSystemsRepositoryFormsBundle.php
Expand Up @@ -13,6 +13,7 @@
use EzSystems\RepositoryFormsBundle\DependencyInjection\Compiler\LimitationFormMapperPass;
use EzSystems\RepositoryFormsBundle\DependencyInjection\Compiler\LimitationValueMapperPass;
use EzSystems\RepositoryFormsBundle\DependencyInjection\Compiler\ViewBuilderRegistryPass;
use EzSystems\RepositoryFormsBundle\DependencyInjection\Configuration\Parser\ContentCreateView;
use EzSystems\RepositoryFormsBundle\DependencyInjection\Configuration\Parser\ContentEdit;
use EzSystems\RepositoryFormsBundle\DependencyInjection\Configuration\Parser\ContentEditView;
use EzSystems\RepositoryFormsBundle\DependencyInjection\Configuration\Parser\LimitationValueTemplates;
Expand All @@ -38,6 +39,7 @@ public function build(ContainerBuilder $container)
$eZExtension->addConfigParser(new UserEdit());
$eZExtension->addConfigParser(new LimitationValueTemplates());
$eZExtension->addConfigParser(new ContentEditView());
$eZExtension->addConfigParser(new ContentCreateView());
$eZExtension->addDefaultSettings(__DIR__ . '/Resources/config', ['ezpublish_default_settings.yml']);
}
}
1 change: 1 addition & 0 deletions bundle/Resources/config/ezpublish_default_settings.yml
Expand Up @@ -11,3 +11,4 @@ parameters:
- { template: 'EzSystemsRepositoryFormsBundle::limitation_values.html.twig', priority: 0 }

ezsettings.default.content_edit_view: {}
ezsettings.default.content_create_view: {}
1 change: 1 addition & 0 deletions bundle/Resources/config/routing.yml
Expand Up @@ -4,6 +4,7 @@ ez_content_create_no_draft:
_controller: ez_content_edit:createWithoutDraftAction
options:
expose: true
# @todo rename language to languageCode in 3.0

ez_content_draft_edit:
path: /content/edit/draft/{contentId}/{versionNo}/{language}/{locationId}
Expand Down
3 changes: 0 additions & 3 deletions bundle/Resources/config/services.yml
Expand Up @@ -256,8 +256,6 @@ services:
arguments:
- "@ezpublish.api.service.content_type"
- "@ezpublish.api.service.content"
- "@ezpublish.api.service.location"
- "@ezpublish.api.service.language"
- "@ezrepoforms.action_dispatcher.content"
parent: ezpublish.controller.base

Expand Down Expand Up @@ -312,7 +310,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\ContentCreateView', "$content_edit.templates.create$"]]
- [setViewTemplate, ['EzSystems\RepositoryForms\Content\View\ContentCreateDraftView', "$content_edit.templates.create_draft$"]]
- [setPagelayout, ["$pagelayout$"]]

Expand Down
34 changes: 33 additions & 1 deletion bundle/Resources/config/views.yml
Expand Up @@ -4,6 +4,7 @@ services:
autowire: true
autoconfigure: true

# ContentEditView
EzSystems\RepositoryForms\Content\View\Builder\ContentEditViewBuilder:
arguments:
- '@ezpublish.api.repository'
Expand All @@ -12,7 +13,7 @@ services:
- '$content_edit.templates.edit$'
- '@ezrepoforms.action_dispatcher.content'

EzSystems\RepositoryForms\Content\View\Provider\Configured:
EzSystems\RepositoryForms\Content\View\Provider\ContentEditView\Configured:
arguments:
- '@ezplatform.repository_forms.content_edit_view.matcher_factory'
tags:
Expand All @@ -33,3 +34,34 @@ services:
- '@ezpublish.api.service.content_type'
tags:
- { name: kernel.event_subscriber }

# ContentCreateView
EzSystems\RepositoryForms\Content\View\Builder\ContentCreateViewBuilder:
arguments:
- '@ezpublish.api.repository'
- '@ezpublish.view.configurator'
- '@ezpublish.view.view_parameters.injector.dispatcher'
- '$content_edit.templates.create$'
- '@ezrepoforms.action_dispatcher.content'

EzSystems\RepositoryForms\Content\View\Provider\ContentCreateView\Configured:
arguments:
- '@ezplatform.repository_forms.content_create_view.matcher_factory'
tags:
- {name: ezpublish.view_provider, type: 'EzSystems\RepositoryForms\Content\View\ContentCreateView', priority: 10}

ezplatform.repository_forms.content_create_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_create_view$']]

EzSystems\RepositoryForms\Content\View\Filter\ContentCreateViewFilter:
arguments:
- '@ezpublish.api.service.location'
- '@ezpublish.api.service.content_type'
tags:
- { name: kernel.event_subscriber }

0 comments on commit 801d9ef

Please sign in to comment.