diff --git a/core/includes/entity.inc b/core/includes/entity.inc index fbe78f8..09f3005 100644 --- a/core/includes/entity.inc +++ b/core/includes/entity.inc @@ -8,7 +8,6 @@ use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Entity\EntityStorageException; use Drupal\Core\Entity\EntityInterface; -use Drupal\Core\Entity\EntityFormControllerInterface; /** * Gets the entity definition for an entity type. @@ -415,29 +414,43 @@ function entity_form_controller($entity_type, $operation = 'default') { } /** - * Returns the built and processed entity form for the given form controller. + * Returns the default form state for the given entity and operation. * - * @param Drupal\Core\Entity\EntityFormControllerInterface $controller - * The entity form controller. - * @param array $form_state - * (optional) An associative array containing the current state of the form. - * Use this to pass additional information to the form, such as the langcode. - * @code - * $form_state['langcode'] = $langcode; - * $form = entity_build_form($controller, $form_state); - * @endcode + * @param EntityInterface $entity + * The entity to be created or edited. + * @param $operation + * (optional) The operation identifying the form to be processed. * - * @return array - * The processed form for the given form controller. + * @return + * A $form_state array already filled the entity form controller. */ -function entity_build_form(EntityFormControllerInterface $controller, array $form_state = array()) { +function entity_form_state_defaults(EntityInterface $entity, $operation = 'default') { + $form_state = array(); + $controller = drupal_container()->get('plugin.manager.entity')->getFormController($entity->entityType(), $operation); + $controller->setEntity($entity); $form_state['build_info']['callback_object'] = $controller; $form_state['build_info']['base_form_id'] = $controller->getBaseFormID(); - if (!isset($form_state['build_info']['args'])) { - $form_state['build_info']['args'] = array(); - } - $form_id = $controller->getFormID(); - return drupal_build_form($form_id, $form_state); + $form_state['build_info']['args'] = array(); + return $form_state; +} + +/** + * Retrieves, populates, and processes an entity form. + * + * @param EntityInterface $entity + * The entity to be created or edited. + * @param $operation + * (optional) The operation identifying the form to be submitted. + * @param $form_state + * (optional) A keyed array containing the current state of the form. + * + * @return + * A $form_state array already filled with the entity form controller. + */ +function entity_form_submit(EntityInterface $entity, $operation = 'default', &$form_state = array()) { + $form_state += entity_form_state_defaults($entity, $operation); + $form_id = $form_state['build_info']['callback_object']->getFormID(); + drupal_form_submit($form_id, $form_state); } /** @@ -459,9 +472,9 @@ function entity_build_form(EntityFormControllerInterface $controller, array $for * The processed form for the given entity and operation. */ function entity_get_form(EntityInterface $entity, $operation = 'default', array $form_state = array()) { - $controller = entity_form_controller($entity->entityType(), $operation); - $controller->setEntity($entity); - return entity_build_form($controller, $form_state); + $form_state += entity_form_state_defaults($entity, $operation); + $form_id = $form_state['build_info']['callback_object']->getFormID(); + return drupal_build_form($form_id, $form_state); } /** diff --git a/core/lib/Drupal/Core/Entity/EntityManager.php b/core/lib/Drupal/Core/Entity/EntityManager.php index 566b2cc..b140ed9 100644 --- a/core/lib/Drupal/Core/Entity/EntityManager.php +++ b/core/lib/Drupal/Core/Entity/EntityManager.php @@ -330,4 +330,29 @@ 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/modules/openid/openid.module b/core/modules/openid/openid.module index b8956b0..9c9a689 100644 --- a/core/modules/openid/openid.module +++ b/core/modules/openid/openid.module @@ -778,15 +778,7 @@ function openid_authentication($response) { $form_state['values'] = array(); $form_state['values']['op'] = t('Create new account'); $account = entity_create('user', array()); - - // @todo Do not call drupal_form_submit() directly. - $controller = entity_form_controller('user', 'register'); - $controller->setEntity($account); - $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(); - drupal_form_submit($form_id, $form_state); + entity_form_submit($account, 'register', $form_state); if (empty($form_state['user'])) { module_invoke_all('openid_response', $response, NULL); diff --git a/core/modules/options/lib/Drupal/options/Tests/OptionsFieldTest.php b/core/modules/options/lib/Drupal/options/Tests/OptionsFieldTest.php index e715f55..997be90 100644 --- a/core/modules/options/lib/Drupal/options/Tests/OptionsFieldTest.php +++ b/core/modules/options/lib/Drupal/options/Tests/OptionsFieldTest.php @@ -63,11 +63,11 @@ function setUp() { * Test that allowed values can be updated. */ function testUpdateAllowedValues() { + $manager = $this->container->get('plugin.manager.entity'); $langcode = LANGUAGE_NOT_SPECIFIED; // All three options appear. - $entity = entity_create('entity_test', array()); - $form = entity_get_form($entity); + $form = $manager->getForm('entity_test'); $this->assertTrue(!empty($form[$this->field_name][$langcode][1]), 'Option 1 exists'); $this->assertTrue(!empty($form[$this->field_name][$langcode][2]), 'Option 2 exists'); $this->assertTrue(!empty($form[$this->field_name][$langcode][3]), 'Option 3 exists'); @@ -92,8 +92,7 @@ function testUpdateAllowedValues() { // Removed options do not appear. $this->field['settings']['allowed_values'] = array(2 => 'Two'); field_update_field($this->field); - $entity = entity_create('entity_test', array()); - $form = entity_get_form($entity); + $form = $manager->getForm('entity_test'); $this->assertTrue(empty($form[$this->field_name][$langcode][1]), 'Option 1 does not exist'); $this->assertTrue(!empty($form[$this->field_name][$langcode][2]), 'Option 2 exists'); $this->assertTrue(empty($form[$this->field_name][$langcode][3]), 'Option 3 does not exist'); @@ -101,7 +100,7 @@ function testUpdateAllowedValues() { // Completely new options appear. $this->field['settings']['allowed_values'] = array(10 => 'Update', 20 => 'Twenty'); field_update_field($this->field); - $form = entity_get_form($entity); + $form = $manager->getForm($entity); $this->assertTrue(empty($form[$this->field_name][$langcode][1]), 'Option 1 does not exist'); $this->assertTrue(empty($form[$this->field_name][$langcode][2]), 'Option 2 does not exist'); $this->assertTrue(empty($form[$this->field_name][$langcode][3]), 'Option 3 does not exist'); @@ -122,8 +121,7 @@ function testUpdateAllowedValues() { ), ); $this->instance = field_create_instance($this->instance); - $entity = entity_create('entity_test', array()); - $form = entity_get_form($entity); + $form = $manager->getForm('entity_test'); $this->assertTrue(!empty($form[$this->field_name][$langcode][1]), 'Option 1 exists'); $this->assertTrue(!empty($form[$this->field_name][$langcode][2]), 'Option 2 exists'); $this->assertTrue(!empty($form[$this->field_name][$langcode][3]), 'Option 3 exists'); diff --git a/core/modules/user/lib/Drupal/user/UserAutocompleteController.php b/core/modules/user/lib/Drupal/user/UserAutocompleteController.php index 9b668bf..e75aabc 100644 --- a/core/modules/user/lib/Drupal/user/UserAutocompleteController.php +++ b/core/modules/user/lib/Drupal/user/UserAutocompleteController.php @@ -60,7 +60,7 @@ public function autocompleteUser(Request $request, $include_anonymous = FALSE) { * @return \Symfony\Component\HttpFoundation\JsonResponse * A JSON response containing the autocomplete suggestions for existing users. * - * @see \Drupal\user\UserRouteController\autocompleteUser + * @see \Drupal\user\UserAutocomplete::autocompleteUser() */ public function autocompleteUserAnonymous(Request $request) { return $this->autocompleteUser($request, TRUE); diff --git a/core/modules/user/lib/Drupal/user/UserRouteController.php b/core/modules/user/lib/Drupal/user/UserRouteController.php deleted file mode 100644 index cf3b50e..0000000 --- a/core/modules/user/lib/Drupal/user/UserRouteController.php +++ /dev/null @@ -1,28 +0,0 @@ -entityManager->getStorageController('view')->create(array()); - return entity_get_form($entity, 'add'); - } - - /** * Lists all instances of fields on any views. * * @return array @@ -252,22 +239,6 @@ public function edit(ViewUI $view, $display_id = NULL) { } /** - * Returns the form to preview a view. - * - * @param \Drupal\views_ui\ViewUI $view - * The view being deleted. - * @param string|null $display_id - * (optional) The display ID being edited. Defaults to NULL, which will - * load the first available display. - * - * @return array - * The Views preview form. - */ - public function preview(ViewUI $view, $display_id = NULL) { - return entity_get_form($view, 'preview', array('display_id' => $display_id)); - } - - /** * Provides a generic entry point to handle AJAX forms. * * @param string $js 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 195f2f8..2e6f61b 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 @@ -16,6 +16,15 @@ class ViewAddFormController extends ViewFormControllerBase { /** + * Overrides ViewFormControllerBase::init(). + */ + protected function init(array &$form_state) { + parent::init($form_state); + + drupal_set_title(t('Add new view')); + } + + /** * Overrides Drupal\Core\Entity\EntityFormController::prepareForm(). */ protected function prepareEntity() { 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 f4e295a..138fe02 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 @@ -12,14 +12,6 @@ */ class ViewCloneFormController extends ViewFormControllerBase { - public function getForm($view, $operation) { - drupal_set_title(t('Clone of @human_name', array('@human_name' => $view->getHumanName()))); - - $this->setOperation($operation); - $this->setEntity($view); - return entity_build_form($this); - } - /** * Overrides \Drupal\Core\Entity\EntityFormController::prepareForm(). */ @@ -33,6 +25,7 @@ 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/lib/Drupal/views_ui/ViewEditFormController.php b/core/modules/views/views_ui/lib/Drupal/views_ui/ViewEditFormController.php index 5e31eef..d318fb0 100644 --- a/core/modules/views/views_ui/lib/Drupal/views_ui/ViewEditFormController.php +++ b/core/modules/views/views_ui/lib/Drupal/views_ui/ViewEditFormController.php @@ -18,11 +18,6 @@ */ class ViewEditFormController extends ViewFormControllerBase { - protected function init(array &$form_state) { - parent::init($form_state); - $form_state['view'] = $this->entity; - } - /** * Overrides Drupal\Core\Entity\EntityFormController::form(). */ diff --git a/core/modules/views/views_ui/lib/Drupal/views_ui/ViewFormControllerBase.php b/core/modules/views/views_ui/lib/Drupal/views_ui/ViewFormControllerBase.php index 897e15c..3cc8262 100644 --- a/core/modules/views/views_ui/lib/Drupal/views_ui/ViewFormControllerBase.php +++ b/core/modules/views/views_ui/lib/Drupal/views_ui/ViewFormControllerBase.php @@ -24,17 +24,16 @@ protected $displayID; /** - * Overrides \Drupal\Core\Entity\EntityFormController::buildForm(). + * Overrides \Drupal\Core\Entity\EntityFormController::init(). */ - public function buildForm(array $form, array &$form_state) { - if (isset($form_state['display_id'])) { - $this->displayID = $form_state['display_id']; - } + public function init(array &$form_state) { + parent::init($form_state); + + $this->displayID = drupal_container()->get('request')->attributes->get('display_id'); // @todo Remove the need for this. form_load_include($form_state, 'inc', 'views_ui', 'admin'); - - return parent::buildForm($form, $form_state); + $form_state['view'] = $this->entity; } /** diff --git a/core/modules/views/views_ui/lib/Drupal/views_ui/ViewPreviewFormController.php b/core/modules/views/views_ui/lib/Drupal/views_ui/ViewPreviewFormController.php index 9cc607d..3a8adf6 100644 --- a/core/modules/views/views_ui/lib/Drupal/views_ui/ViewPreviewFormController.php +++ b/core/modules/views/views_ui/lib/Drupal/views_ui/ViewPreviewFormController.php @@ -16,6 +16,8 @@ class ViewPreviewFormController extends ViewFormControllerBase { * Overrides Drupal\Core\Entity\EntityFormController::form(). */ public function form(array $form, array &$form_state) { + $view = $this->entity; + $form['#prefix'] = '