diff --git a/core/includes/form.inc b/core/includes/form.inc index b41d15e..a58102c 100644 --- a/core/includes/form.inc +++ b/core/includes/form.inc @@ -7,6 +7,7 @@ use Drupal\Component\Utility\NestedArray; use Drupal\Core\Form\FormInterface; +use Drupal\Core\Form\BaseFormInterface; use Drupal\Core\Database\Database; use Drupal\Core\Template\Attribute; use Drupal\Core\Datetime\DrupalDateTime; @@ -119,6 +120,9 @@ function _drupal_form_id($form_arg, &$form_state) { // the callback object and determine the form ID. if (is_object($form_arg) && $form_arg instanceof FormInterface) { $form_state['build_info']['callback_object'] = $form_arg; + if ($form_arg instanceof BaseFormInterface) { + $form_state['build_info']['base_form_id'] = $form_arg->getBaseFormID(); + } return $form_arg->getFormID(); } diff --git a/core/lib/Drupal/Core/CoreBundle.php b/core/lib/Drupal/Core/CoreBundle.php index 9ef08f6..2dbf11b 100644 --- a/core/lib/Drupal/Core/CoreBundle.php +++ b/core/lib/Drupal/Core/CoreBundle.php @@ -373,6 +373,9 @@ protected function registerRouting(ContainerBuilder $container) { $container->register('route_enhancer.ajax', 'Drupal\Core\Routing\Enhancer\AjaxEnhancer') ->addArgument(new Reference('content_negotiation')) ->addTag('route_enhancer', array('priority' => 20)); + $container->register('route_enhancer.entity_form', 'Drupal\Core\Entity\Enhancer\EntityFormEnhancer') + ->addArgument(new Reference('content_negotiation')) + ->addTag('route_enhancer', array('priority' => 15)); $container->register('route_enhancer.form', 'Drupal\Core\Routing\Enhancer\FormEnhancer') ->addArgument(new Reference('content_negotiation')) ->addTag('route_enhancer', array('priority' => 10)); diff --git a/core/lib/Drupal/Core/Entity/Enhancer/EntityFormEnhancer.php b/core/lib/Drupal/Core/Entity/Enhancer/EntityFormEnhancer.php new file mode 100644 index 0000000..c706052 --- /dev/null +++ b/core/lib/Drupal/Core/Entity/Enhancer/EntityFormEnhancer.php @@ -0,0 +1,46 @@ +negotiation = $negotiation; + } + + /** + * Implements \Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface::enhance() + */ + public function enhance(array $defaults, Request $request) { + if (empty($defaults['_controller']) && !empty($defaults['_entity_form']) && $this->negotiation->getContentType($request) === 'html') { + $defaults['_controller'] = '\Drupal\Core\Entity\HtmlEntityFormController::content'; + } + return $defaults; + } + +} diff --git a/core/lib/Drupal/Core/Entity/EntityFormController.php b/core/lib/Drupal/Core/Entity/EntityFormController.php index d5ba5a9..0d1c4b9 100644 --- a/core/lib/Drupal/Core/Entity/EntityFormController.php +++ b/core/lib/Drupal/Core/Entity/EntityFormController.php @@ -52,7 +52,7 @@ public function setOperation($operation) { } /** - * Implements EntityFormControllerInterface::getBaseFormID(). + * Implements \Drupal\Core\Entity\EntityFormControllerInterface::getBaseFormID(). */ public function getBaseFormID() { return $this->entity->entityType() . '_form'; diff --git a/core/lib/Drupal/Core/Entity/EntityFormControllerInterface.php b/core/lib/Drupal/Core/Entity/EntityFormControllerInterface.php index b83b2b4..28b2562 100644 --- a/core/lib/Drupal/Core/Entity/EntityFormControllerInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityFormControllerInterface.php @@ -7,12 +7,12 @@ namespace Drupal\Core\Entity; -use Drupal\Core\Form\FormInterface; +use Drupal\Core\Form\BaseFormInterface; /** * Defines a common interface for entity form controller classes. */ -interface EntityFormControllerInterface extends FormInterface { +interface EntityFormControllerInterface extends BaseFormInterface { /** * Returns the code identifying the active form language. @@ -114,14 +114,4 @@ public function validate(array $form, array &$form_state); */ public function submit(array $form, array &$form_state); - /** - * Returns a string identifying the base form. - * - * @return string|false - * The string identifying the base form or FALSE if this is not a base form. - * - * @todo Move to \Drupal\Core\Form\FormInterface. - */ - public function getBaseFormID(); - } diff --git a/core/lib/Drupal/Core/Entity/EntityManager.php b/core/lib/Drupal/Core/Entity/EntityManager.php index c975b91..be46ac9 100644 --- a/core/lib/Drupal/Core/Entity/EntityManager.php +++ b/core/lib/Drupal/Core/Entity/EntityManager.php @@ -324,29 +324,4 @@ public function getAccessController($entity_type) { return $this->controllers['access'][$entity_type]; } - /** - * Returns the built and processed entity form for the given entity. - * - * @param EntityInterface|string $entity - * The entity edited, or the entity type of the entity to be created. - * @param string $operation - * (optional) The operation identifying the form variation to be returned. - * Defaults to 'default'. - * - * @return array - * The processed form for the given entity and operation. - */ - public function getForm($entity, $operation = 'default') { - if (is_string($entity)) { - $entity = $this->getStorageController($entity)->create(array()); - } - - $controller = $this->getFormController($entity->entityType(), $operation)->setEntity($entity); - $form_state['build_info']['callback_object'] = $controller; - $form_state['build_info']['base_form_id'] = $controller->getBaseFormID(); - $form_state['build_info']['args'] = array(); - $form_id = $controller->getFormID(); - return drupal_build_form($form_id, $form_state); - } - } diff --git a/core/lib/Drupal/Core/Entity/HtmlEntityFormController.php b/core/lib/Drupal/Core/Entity/HtmlEntityFormController.php new file mode 100644 index 0000000..7ede74e --- /dev/null +++ b/core/lib/Drupal/Core/Entity/HtmlEntityFormController.php @@ -0,0 +1,49 @@ +attributes->has($entity)) { + $entity = $request->attributes->get($entity); + } + elseif (is_string($entity)) { + $entity = $manager->getStorageController($entity)->create(array()); + } + + return $manager->getFormController($entity->entityType(), $operation)->setEntity($entity); + } + +} diff --git a/core/lib/Drupal/Core/Form/BaseFormInterface.php b/core/lib/Drupal/Core/Form/BaseFormInterface.php new file mode 100644 index 0000000..fa624cd --- /dev/null +++ b/core/lib/Drupal/Core/Form/BaseFormInterface.php @@ -0,0 +1,25 @@ +container); - } - else { - $form_arg = new $_form(); - } - } - // Otherwise, it is a service. - else { - $form_arg = $this->container->get($_form); - } + $form_arg = $this->getFormArg($request, $_form); // Using reflection, find all of the parameters needed by the form in the // request attributes, skipping $form and $form_state. @@ -78,4 +66,20 @@ public function content(Request $request, $_form) { return new Response(drupal_render_page($form)); } + /** + * @todo. + */ + protected function getFormArg(Request $request, $arg) { + // If this is a class, instantiate it. + if (class_exists($arg)) { + if (in_array('Drupal\Core\ControllerInterface', class_implements($arg))) { + return $arg::create($this->container); + } + + return new $arg(); + } + // Otherwise, it is a service. + return $this->container->get($arg); + } + } diff --git a/core/modules/user/user.routing.yml b/core/modules/user/user.routing.yml index 251254e..69ceba2 100644 --- a/core/modules/user/user.routing.yml +++ b/core/modules/user/user.routing.yml @@ -1,9 +1,7 @@ user_register: pattern: '/user/register' defaults: - _controller: 'plugin.manager.entity:getForm' - entity: 'user' - operation: 'register' + _entity_form: 'user.register' requirements: _access_user_register: 'TRUE' diff --git a/core/modules/views/views_ui/lib/Drupal/views_ui/ViewAddFormController.php b/core/modules/views/views_ui/lib/Drupal/views_ui/ViewAddFormController.php index 295e45f..c5aa03c 100644 --- a/core/modules/views/views_ui/lib/Drupal/views_ui/ViewAddFormController.php +++ b/core/modules/views/views_ui/lib/Drupal/views_ui/ViewAddFormController.php @@ -17,7 +17,7 @@ class ViewAddFormController extends ViewFormControllerBase { /** - * Overrides ViewFormControllerBase::init(). + * Overrides \Drupal\views_ui\ViewFormControllerBase::init(). */ public function init(array &$form_state) { parent::init($form_state); diff --git a/core/modules/views/views_ui/lib/Drupal/views_ui/ViewCloneFormController.php b/core/modules/views/views_ui/lib/Drupal/views_ui/ViewCloneFormController.php index 138fe02..dd03d9c 100644 --- a/core/modules/views/views_ui/lib/Drupal/views_ui/ViewCloneFormController.php +++ b/core/modules/views/views_ui/lib/Drupal/views_ui/ViewCloneFormController.php @@ -13,6 +13,15 @@ class ViewCloneFormController extends ViewFormControllerBase { /** + * Overrides \Drupal\views_ui\ViewFormControllerBase::init(). + */ + public function init(array &$form_state) { + parent::init($form_state); + + drupal_set_title(t('Clone of @human_name', array('@human_name' => $this->entity->getHumanName()))); + } + + /** * Overrides \Drupal\Core\Entity\EntityFormController::prepareForm(). */ protected function prepareEntity() { @@ -25,7 +34,6 @@ protected function prepareEntity() { public function form(array $form, array &$form_state) { parent::form($form, $form_state); - drupal_set_title(t('Clone of @human_name', array('@human_name' => $this->entity->getHumanName()))); $form['human_name'] = array( '#type' => 'textfield', '#title' => t('View name'), diff --git a/core/modules/views/views_ui/views_ui.routing.yml b/core/modules/views/views_ui/views_ui.routing.yml index ddd8e66..f2046f6 100644 --- a/core/modules/views/views_ui/views_ui.routing.yml +++ b/core/modules/views/views_ui/views_ui.routing.yml @@ -8,9 +8,7 @@ views_ui.list: views_ui.add: pattern: '/admin/structure/views/add' defaults: - _controller: 'plugin.manager.entity:getForm' - entity: 'view' - operation: 'add' + _entity_form: 'view.add' requirements: _permission: 'administer views' @@ -51,13 +49,9 @@ views_ui.operation: op: 'enable|disable' views_ui.clone: - pattern: '/admin/structure/views/view/{entity}/clone' - options: - converters: - entity: 'view' + pattern: '/admin/structure/views/view/{view}/clone' defaults: - _controller: 'plugin.manager.entity:getForm' - operation: 'clone' + _entity_form: 'view.clone' requirements: _permission: 'administer views' @@ -97,16 +91,13 @@ views_ui.edit.display: _permission: 'administer views' views_ui.preview: - pattern: '/admin/structure/views/view/{entity}/preview/{display_id}' + pattern: '/admin/structure/views/view/{view}/preview/{display_id}' options: - converters: - entity: 'view' tempstore: - entity: 'views' + view: 'views' defaults: - _controller: 'plugin.manager.entity:getForm' + _entity_form: 'view.preview' display_id: NULL - operation: 'preview' requirements: _permission: 'administer views'