Skip to content

Commit

Permalink
Merge pull request ezsystems#16 from ezsystems/EZP-24379_platformUIIn…
Browse files Browse the repository at this point in the history
…tegration

EZP-24379: Integrate ContentType edition with PlatformUI
  • Loading branch information
lolautruche committed May 29, 2015
2 parents c55dda7 + 8ad109c commit ed83e4e
Show file tree
Hide file tree
Showing 11 changed files with 270 additions and 183 deletions.
87 changes: 0 additions & 87 deletions bundle/Controller/ContentTypeController.php

This file was deleted.

7 changes: 0 additions & 7 deletions bundle/Resources/config/routing.yml

This file was deleted.

11 changes: 11 additions & 0 deletions bundle/Resources/config/services.yml
Expand Up @@ -15,6 +15,7 @@ parameters:
ezrepoforms.validator.default_field_value.class: EzSystems\RepositoryForms\Validator\Constraints\FieldDefinitionDefaultValueValidator

ezrepoforms.twig.field_edit_rendering_extension.class: EzSystems\RepositoryForms\Twig\FieldEditRenderingExtension
ezrepoforms.action_dispatcher.content_type.class: EzSystems\RepositoryForms\Form\ActionDispatcher\ContentTypeDispatcher
ezrepoforms.form_processor.content_type.class: EzSystems\RepositoryForms\Form\Processor\ContentTypeFormProcessor
ezrepoforms.form_processor.content_type.options.redirect_route_after_publish: ~
ezrepoforms.form_processor.content_type.options:
Expand Down Expand Up @@ -98,6 +99,16 @@ services:
tags:
- { name: twig.extension }

ezrepoforms.action_dispatcher.base:
class: EzSystems\RepositoryForms\Form\ActionDispatcher\AbstractActionDispatcher
abstract: true
calls:
- [setEventDispatcher, [@event_dispatcher]]

ezrepoforms.action_dispatcher.content_type:
class: %ezrepoforms.action_dispatcher.content_type.class%
parent: ezrepoforms.action_dispatcher.base

ezrepoforms.form_processor.content_type:
class: %ezrepoforms.form_processor.content_type.class%
arguments: [@ezpublish.api.service.content_type, @router, %ezrepoforms.form_processor.content_type.options%]
Expand Down
70 changes: 0 additions & 70 deletions bundle/Resources/views/ContentType/update_content_type.html.twig

This file was deleted.

41 changes: 33 additions & 8 deletions lib/Event/FormActionEvent.php
Expand Up @@ -23,11 +23,11 @@ class FormActionEvent extends FormEvent
private $clickedButton;

/**
* Language code current form is edited in.
* Hash of options.
*
* @var string
* @var array
*/
private $languageCode;
private $options;

/**
* Response to return after form post-processing. Typically a RedirectResponse.
Expand All @@ -36,11 +36,11 @@ class FormActionEvent extends FormEvent
*/
private $response;

public function __construct(FormInterface $form, $data, $clickedButton, $languageCode)
public function __construct(FormInterface $form, $data, $clickedButton, array $options = [])
{
parent::__construct($form, $data);
$this->clickedButton = $clickedButton;
$this->languageCode = $languageCode;
$this->options = $options;
}

/**
Expand All @@ -52,11 +52,36 @@ public function getClickedButton()
}

/**
* @return string
* @return array
*/
public function getOptions()
{
return $this->options;
}

/**
* @param string $optionName The option name
* @param mixed $defaultValue Default value to return if option is not set.
*
* @return mixed
*/
public function getOption($optionName, $defaultValue = null)
{
if (!isset($this->options[$optionName])) {
return $defaultValue;
}

return $this->options[$optionName];
}

/**
* @param string $optionName
*
* @return bool
*/
public function getLanguageCode()
public function hasOption($optionName)
{
return $this->languageCode;
return isset($this->options[$optionName]);
}

/**
Expand Down
90 changes: 90 additions & 0 deletions lib/Form/ActionDispatcher/AbstractActionDispatcher.php
@@ -0,0 +1,90 @@
<?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.
* @version //autogentag//
*/

namespace EzSystems\RepositoryForms\Form\ActionDispatcher;

use eZ\Publish\API\Repository\Values\ValueObject;
use EzSystems\RepositoryForms\Event\FormActionEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Base class for action dispatchers.
*/
abstract class AbstractActionDispatcher implements ActionDispatcherInterface
{
/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;

/**
* @var \Symfony\Component\HttpFoundation\Response
*/
protected $response;

public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}

public function dispatchFormAction(FormInterface $form, ValueObject $data, $actionName, array $options = [])
{
$resolver = new OptionsResolver();
$this->configureOptions($resolver);
$options = $resolver->resolve($options);

// First dispatch default action, then $actionName.
$event = new FormActionEvent($form, $data, $actionName, $options);
$defaultActionEventName = $this->getActionEventBaseName();
$this->dispatchDefaultAction($defaultActionEventName, $event);
$this->dispatchAction($defaultActionEventName . ($actionName ? ".$actionName" : ''), $event);
$this->response = $event->getResponse();
}

/**
* Configures options to pass to the form action event.
* Might do nothing if there are no options.
*
* @param OptionsResolver $resolver
*/
abstract protected function configureOptions(OptionsResolver $resolver);

/**
* Returns base for action event name. It will be used as default action event name.
* By convention, other action event names will have the format "<actionEventBaseName>.<actionName>".
*
* @return string
*/
abstract protected function getActionEventBaseName();

/**
* @param $defaultActionEventName
* @param $event
*/
protected function dispatchDefaultAction($defaultActionEventName, FormActionEvent $event)
{
$this->eventDispatcher->dispatch($defaultActionEventName, $event);
}

/**
* @param $actionEventName
* @param $event
*/
protected function dispatchAction($actionEventName, FormActionEvent $event)
{
$this->eventDispatcher->dispatch($actionEventName, $event);
}

public function getResponse()
{
return $this->response;
}
}
40 changes: 40 additions & 0 deletions lib/Form/ActionDispatcher/ActionDispatcherInterface.php
@@ -0,0 +1,40 @@
<?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.
* @version //autogentag//
*/

namespace EzSystems\RepositoryForms\Form\ActionDispatcher;

use eZ\Publish\API\Repository\Values\ValueObject;
use Symfony\Component\Form\FormInterface;

/**
* Form action dispatchers can be used to abstract actions when a complex form is submitted.
* Typical example is a multiple actions based form, with multiple submit buttons, where actions to take depend on
* which submit button is clicked.
*
* This would basically help reducing the amount of code in the controller receiving the form submission request.
*/
interface ActionDispatcherInterface
{
/**
* Dispatches the action of a given form.
*
* @param FormInterface $form The form that has been submitted.
* @param ValueObject $data Underlying data for the form. Most likely a create or update struct.
* @param string $actionName The form action itself. Typically the form clicked button name.
* @param array $options Arbitrary hash of options.
*/
public function dispatchFormAction(FormInterface $form, ValueObject $data, $actionName, array $options = []);

/**
* Returns the generated response, if any. Typically a RedirectResponse.
*
* @return \Symfony\Component\HttpFoundation\Response|null
*/
public function getResponse();
}

0 comments on commit ed83e4e

Please sign in to comment.