Commit 410d56a7 authored by alexpott's avatar alexpott

Issue #2059245 by tim.plunkett, msonnabaum: Add a FormBase class containing useful methods.

parent 5460c03e
......@@ -159,7 +159,7 @@ services:
- { name: persist }
plugin.manager.entity:
class: Drupal\Core\Entity\EntityManager
arguments: ['@container.namespaces', '@service_container', '@module_handler', '@cache.cache', '@language_manager']
arguments: ['@container.namespaces', '@service_container', '@module_handler', '@cache.cache', '@language_manager', '@string_translation']
plugin.manager.entity.field.field_type:
class: Drupal\Core\Entity\Field\FieldTypePluginManager
arguments: ['@container.namespaces', '@cache.entity', '@language_manager', '@module_handler']
......
......@@ -7,6 +7,7 @@
namespace Drupal\Core\Entity;
use Drupal\Component\Utility\Url;
use Drupal\Core\Form\ConfirmFormInterface;
use Symfony\Component\HttpFoundation\Request;
......@@ -15,13 +16,6 @@
*/
abstract class EntityConfirmFormBase extends EntityFormController implements ConfirmFormInterface {
/**
* The request object.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* {@inheritdoc}
*/
......@@ -60,8 +54,7 @@ public function getFormName() {
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state, Request $request = NULL) {
$this->request = $request;
public function buildForm(array $form, array &$form_state) {
$form = parent::buildForm($form, $form_state);
$form['#attributes']['class'][] = 'confirmation';
......@@ -94,8 +87,9 @@ protected function actions(array $form, array &$form_state) {
$path = $this->getCancelPath();
// Prepare cancel link.
if ($this->request->query->has('destination')) {
$options = drupal_parse_url($this->request->query->get('destination'));
$query = $this->getRequest()->query;
if ($query->has('destination')) {
$options = Url::parse($query->get('destination'));
}
elseif (is_array($path)) {
$options = $path;
......
......@@ -7,6 +7,7 @@
namespace Drupal\Core\Entity;
use Drupal\Core\Form\FormBase;
use Drupal\entity\EntityFormDisplayInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\Language;
......@@ -15,7 +16,7 @@
/**
* Base class for entity form controllers.
*/
class EntityFormController implements EntityFormControllerInterface {
class EntityFormController extends FormBase implements EntityFormControllerInterface {
/**
* The name of the current operation.
......@@ -41,36 +42,15 @@ class EntityFormController implements EntityFormControllerInterface {
*/
protected $entity;
/**
* Constructs an EntityFormController object.
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface
* The module handler service.
*/
public function __construct(ModuleHandlerInterface $module_handler) {
$this->moduleHandler = $module_handler;
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info) {
return new static(
$container->get('module_handler')
);
}
/**
* Sets the operation for this form.
*
* @param string $operation
* The name of the current operation.
*/
public function setOperation($operation) {
// If NULL is passed, do not overwrite the operation.
if ($operation) {
$this->operation = $operation;
}
return $this;
}
/**
......@@ -126,12 +106,6 @@ public function buildForm(array $form, array &$form_state) {
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, array &$form_state) {
}
/**
* {@inheritdoc}
*/
......@@ -269,7 +243,7 @@ protected function actions(array $form, array &$form_state) {
return array(
// @todo Rename the action key from submit to save.
'submit' => array(
'#value' => t('Save'),
'#value' => $this->t('Save'),
'#validate' => array(
array($this, 'validate'),
),
......@@ -279,7 +253,7 @@ protected function actions(array $form, array &$form_state) {
),
),
'delete' => array(
'#value' => t('Delete'),
'#value' => $this->t('Delete'),
// No need to validate the form when deleting the entity.
'#submit' => array(
array($this, 'delete'),
......@@ -566,4 +540,13 @@ public function setFormDisplay(EntityFormDisplayInterface $form_display, array &
public function getOperation() {
return $this->operation;
}
/**
* {@inheritdoc}
*/
public function setModuleHandler(ModuleHandlerInterface $module_handler) {
$this->moduleHandler = $module_handler;
return $this;
}
}
......@@ -7,7 +7,9 @@
namespace Drupal\Core\Entity;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\BaseFormIdInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\entity\EntityFormDisplayInterface;
/**
......@@ -37,6 +39,17 @@ public function getFormLangcode(array $form_state);
*/
public function isDefaultFormLangcode(array $form_state);
/**
* Sets the operation for this form.
*
* @param string $operation
* The name of the current operation.
*
* @return self
* The entity form.
*/
public function setOperation($operation);
/**
* Returns the operation identifying the form controller.
*
......@@ -139,4 +152,26 @@ public function validate(array $form, array &$form_state);
*/
public function submit(array $form, array &$form_state);
/**
* Sets the translation manager for this form.
*
* @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager
* The translation manager.
*
* @return self
* The entity form.
*/
public function setTranslationManager(TranslationInterface $translation_manager);
/**
* Sets the module handler for this form.
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
*
* @return self
* The entity form.
*/
public function setModuleHandler(ModuleHandlerInterface $module_handler);
}
......@@ -18,6 +18,7 @@
use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
use Drupal\Core\Plugin\Discovery\InfoHookDecorator;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -87,6 +88,13 @@ class EntityManager extends PluginManagerBase {
*/
protected $fieldDefinitions;
/**
* The string translationManager.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface
*/
protected $translationManager;
/**
* Constructs a new Entity plugin manager.
*
......@@ -101,8 +109,10 @@ class EntityManager extends PluginManagerBase {
* The cache backend to use.
* @param \Drupal\Core\Language\LanguageManager $language_manager
* The language manager.
* @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager
* The string translationManager.
*/
public function __construct(\Traversable $namespaces, ContainerInterface $container, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache, LanguageManager $language_manager) {
public function __construct(\Traversable $namespaces, ContainerInterface $container, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache, LanguageManager $language_manager, TranslationInterface $translation_manager) {
// Allow the plugin definition to be altered by hook_entity_info_alter().
$annotation_namespaces = array(
'Drupal\Core\Entity\Annotation' => DRUPAL_ROOT . '/core/lib',
......@@ -111,6 +121,7 @@ public function __construct(\Traversable $namespaces, ContainerInterface $contai
$this->moduleHandler = $module_handler;
$this->cache = $cache;
$this->languageManager = $language_manager;
$this->translationManager = $translation_manager;
$this->discovery = new AnnotatedClassDiscovery('Entity', $namespaces, $annotation_namespaces, 'Drupal\Core\Entity\Annotation\EntityType');
$this->discovery = new InfoHookDecorator($this->discovery, 'entity_info');
......@@ -229,13 +240,18 @@ public function getListController($entity_type) {
public function getFormController($entity_type, $operation) {
if (!isset($this->controllers['form'][$operation][$entity_type])) {
$class = $this->getControllerClass($entity_type, 'form', $operation);
if (in_array('Drupal\Core\Entity\EntityControllerInterface', class_implements($class))) {
$this->controllers['form'][$operation][$entity_type] = $class::createInstance($this->container, $entity_type, $this->getDefinition($entity_type));
if (in_array('Drupal\Core\Controller\ControllerInterface', class_implements($class))) {
$controller = $class::create($this->container);
}
else {
$this->controllers['form'][$operation][$entity_type] = new $class($this->container->get('module_handler'));
$controller = new $class();
}
$this->controllers['form'][$operation][$entity_type]->setOperation($operation);
$controller
->setTranslationManager($this->translationManager)
->setModuleHandler($this->moduleHandler)
->setOperation($operation);
$this->controllers['form'][$operation][$entity_type] = $controller;
}
return $this->controllers['form'][$operation][$entity_type];
}
......
......@@ -7,21 +7,14 @@
namespace Drupal\Core\Entity;
use Drupal\Component\Utility\Url;
use Drupal\Core\Form\ConfirmFormInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Provides a generic base class for an entity-based confirmation form.
*/
abstract class EntityNGConfirmFormBase extends EntityFormControllerNG implements ConfirmFormInterface {
/**
* The request object.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* {@inheritdoc}
*/
......@@ -60,8 +53,7 @@ public function getFormName() {
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state, Request $request = NULL) {
$this->request = $request;
public function buildForm(array $form, array &$form_state) {
$form = parent::buildForm($form, $form_state);
$form['#attributes']['class'][] = 'confirmation';
......@@ -94,8 +86,9 @@ protected function actions(array $form, array &$form_state) {
$path = $this->getCancelPath();
// Prepare cancel link.
if ($this->request->query->has('destination')) {
$options = drupal_parse_url($this->request->query->get('destination'));
$query = $this->getRequest()->query;
if ($query->has('destination')) {
$options = Url::parse($query->get('destination'));
}
elseif (is_array($path)) {
$options = $path;
......
......@@ -7,12 +7,12 @@
namespace Drupal\Core\Form;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Component\Utility\Url;
/**
* Provides an generic base class for a confirmation form.
*/
abstract class ConfirmFormBase implements ConfirmFormInterface {
abstract class ConfirmFormBase extends FormBase implements ConfirmFormInterface {
/**
* {@inheritdoc}
......@@ -45,11 +45,12 @@ public function getFormName() {
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state, Request $request = NULL) {
public function buildForm(array $form, array &$form_state) {
$path = $this->getCancelPath();
// Prepare cancel link.
if ($request->query->has('destination')) {
$options = drupal_parse_url($request->query->get('destination'));
$query = $this->getRequest()->query;
if ($query->has('destination')) {
$options = Url::parse($query->get('destination'));
}
elseif (is_array($path)) {
$options = $path;
......@@ -82,10 +83,4 @@ public function buildForm(array $form, array &$form_state, Request $request = NU
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, array &$form_state) {
}
}
<?php
/**
* @file
* Contains \Drupal\Core\Form\FormBase.
*/
namespace Drupal\Core\Form;
use Drupal\Core\Controller\ControllerInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Provides a base class for forms.
*/
abstract class FormBase implements FormInterface, ControllerInterface {
/**
* The translation manager service.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface
*/
protected $translationManager;
/**
* The current request.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static();
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, array &$form_state) {
// Validation is optional.
}
/**
* Translates a string to the current language or to a given language.
*
* @param string $string
* A string containing the English string to translate.
* @param array $args
* An associative array of replacements to make after translation. Based
* on the first character of the key, the value is escaped and/or themed.
* See \Drupal\Core\Utility\String::format() for details.
* @param array $options
* An associative array of additional options, with the following elements:
* - 'langcode': The language code to translate to a language other than
* what is used to display the page.
* - 'context': The context the source string belongs to.
*
* @return string
* The translated string.
*/
protected function t($string, array $args = array(), array $options = array()) {
return $this->getTranslationManager()->translate($string, $args, $options);
}
/**
* Gets the translation manager.
*
* @return \Drupal\Core\StringTranslation\TranslationInterface
* The translation manager.
*/
protected function getTranslationManager() {
if (!$this->translationManager) {
$this->translationManager = \Drupal::translation();
}
return $this->translationManager;
}
/**
* Sets the translation manager for this form.
*
* @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager
* The translation manager.
*
* @return self
* The entity form.
*/
public function setTranslationManager(TranslationInterface $translation_manager) {
$this->translationManager = $translation_manager;
return $this;
}
/**
* Gets the request object.
*
* @return \Symfony\Component\HttpFoundation\Request $request
* The request object.
*/
protected function getRequest() {
if (!$this->request) {
$this->request = \Drupal::request();
}
return $this->request;
}
/**
* Sets the request object to use.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The request object.
*/
public function setRequest(Request $request) {
$this->request = $request;
}
/**
* Gets the current user.
*
* @return \Drupal\Core\Session\AccountInterface
* The current user.
*/
protected function getCurrentUser() {
return $this->getRequest()->attributes->get('_account');
}
}
......@@ -9,15 +9,13 @@
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Action\ActionManager;
use Drupal\Core\Entity\EntityControllerInterface;
use Drupal\Core\Entity\EntityStorageControllerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a form controller for action add forms.
*/
class ActionAddFormController extends ActionFormControllerBase implements EntityControllerInterface {
class ActionAddFormController extends ActionFormControllerBase {
/**
* The action manager.
......@@ -29,15 +27,13 @@ class ActionAddFormController extends ActionFormControllerBase implements Entity
/**
* Constructs a new ActionAddFormController.
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface
* The module handler service.
* @param \Drupal\Core\Entity\EntityStorageControllerInterface $storage_controller
* The action storage controller.
* @param \Drupal\Core\Action\ActionManager $action_manager
* The action plugin manager.
*/
public function __construct(ModuleHandlerInterface $module_handler, EntityStorageControllerInterface $storage_controller, ActionManager $action_manager) {
parent::__construct($module_handler, $storage_controller);
public function __construct(EntityStorageControllerInterface $storage_controller, ActionManager $action_manager) {
parent::__construct($storage_controller);
$this->actionManager = $action_manager;
}
......@@ -45,10 +41,9 @@ public function __construct(ModuleHandlerInterface $module_handler, EntityStorag
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info) {
public static function create(ContainerInterface $container) {
return new static(
$container->get('module_handler'),
$container->get('plugin.manager.entity')->getStorageController($entity_type),
$container->get('plugin.manager.entity')->getStorageController('action'),
$container->get('plugin.manager.action')
);
}
......
......@@ -7,9 +7,7 @@
namespace Drupal\action;
use Drupal\Core\Entity\EntityControllerInterface;
use Drupal\Core\Entity\EntityFormController;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Entity\EntityStorageControllerInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -17,7 +15,7 @@
/**
* Provides a base form controller for action forms.
*/
abstract class ActionFormControllerBase extends EntityFormController implements EntityControllerInterface {
abstract class ActionFormControllerBase extends EntityFormController {
/**
* The action plugin being configured.
......@@ -36,24 +34,19 @@ abstract class ActionFormControllerBase extends EntityFormController implements
/**
* Constructs a new action form.
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface
* The module handler service.
* @param \Drupal\Core\Entity\EntityStorageControllerInterface $storage_controller
* The action storage controller.
*/
public function __construct(ModuleHandlerInterface $module_handler, EntityStorageControllerInterface $storage_controller) {
parent::__construct($module_handler);
public function __construct(EntityStorageControllerInterface $storage_controller) {
$this->storageController = $storage_controller;
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info) {
public static function create(ContainerInterface $container) {
return new static(
$container->get('module_handler'),
$container->get('plugin.manager.entity')->getStorageController($entity_type)
$container->get('plugin.manager.entity')->getStorageController('action')
);
}
......
......@@ -13,10 +13,8 @@
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Provides a confirm delete form.
*/
......@@ -50,13 +48,6 @@ class CategoryDeleteForm extends ConfirmFormBase implements ControllerInterface
*/
protected $categoryStorageController;
/**
* The current request.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* Creates a new CategoryDeleteForm.
*
......@@ -126,8 +117,6 @@ public function getDescription() {
* An associative array containing the structure of the form.
* @param array $form_state
* An associative array containing the current state of the form.
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request.
* @param int|null $cid
* The category ID.
*
......@@ -137,14 +126,13 @@ public function getDescription() {
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
* If the cid param or category is not found.
*/
public function buildForm(array $form, array &$form_state, Request $request = NULL, $cid = NULL) {
public function buildForm(array $form, array &$form_state, $cid = NULL) {
$category = $this->categoryStorageController->load($cid);
if (empty($cid) || empty($category)) {
throw new NotFoundHttpException();
}
$this->category = $category;
$this->request = $request;
return parent::buildForm($form, $form_state, $request);
return parent::buildForm($form, $form_state);
}
/**
......@@ -158,7 +146,7 @@ public function submitForm(array &$form, array &$form_state) {
$this->deleteBlocks($cid);
watchdog('aggregator', 'Category %category deleted.', array('%category' => $title));
drupal_set_message(t('The category %category has been deleted.', array('%category' => $title)));
if (preg_match('/^\/admin/', $this->request->getPathInfo())) {
if (preg_match('/^\/admin/', $this->getRequest()->getPathInfo())) {
$form_state['redirect'] = 'admin/config/services/aggregator/';
}
else {
......
......@@ -7,6 +7,7 @@
namespace Drupal\aggregator\Form;
use Drupal\Core\Config\Context\ContextInterface;
use Drupal\system\SystemConfigFormBase;
use Drupal\Core\Config\ConfigFactory;
use Drupal\aggregator\Plugin\AggregatorPluginManager;
......@@ -40,6 +41,8 @@ class SettingsForm extends SystemConfigFormBase {
*
* @param \Drupal\Core\Config\ConfigFactory $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\Config\Context\ContextInterface $context
* The configuration context to use.
* @param \Drupal\aggregator\Plugin\AggregatorPluginManager $fetcher_manager
* The aggregator fetcher plugin manager.
* @param \Drupal\aggregator\Plugin\AggregatorPluginManager $parser_manager
......@@ -47,8 +50,9 @@ class SettingsForm extends SystemConfigFormBase {
* @param \Drupal\aggregator\Plugin\AggregatorPluginManager $processor_manager
* The aggregator processor plugin manager.
*/
public function __construct(ConfigFactory $config_factory, AggregatorPluginManager $fetcher_manager, AggregatorPluginManager $parser_manager, AggregatorPluginManager $processor_manager) {
$this->configFactory = $config_factory;
public function __construct(ConfigFactory $config_factory, ContextInterface $context, AggregatorPluginManager $fetcher_manager, AggregatorPluginManager $parser_manager, AggregatorPluginManager $processor_manager) {
parent::__construct($config_factory, $context);
$this->managers = array(
'fetcher' => $fetcher_manager,
'parser' => $parser_manager,
......@@ -68,6 +72,7 @@ public function __construct(ConfigFactory $config_factory, AggregatorPluginManag
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('config.context.free'),
$container->get('plugin.manager.aggregator.fetcher'),
$container->get('plugin.manager.aggregator.parser'),
$container->get('plugin.manager.aggregator.processor')
......
......@@ -11,7 +11,6 @@
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\ban\BanIpManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
......@@ -30,6 +29,7 @@ class BanDelete extends ConfirmFormBase implements ControllerInterface {
* Constructs a new BanDelete object.
*
* @param \Drupal\ban\BanIpManager $ip_manager
* The IP manager.
*/
public function __construct(BanIpManager $ip_manager) {
$this->ipManager = $ip_manager;
......@@ -55,14 +55,14 @@ public function getFormID() {
* {@inheritdoc}
*/
public function getQuestion() {
return t('Are you sure you want to unblock %ip?', array('%ip' => $this->banIp));
return $this->t('Are you sure you want to unblock %ip?', array('%ip' => $this->banIp));
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return t