diff --git a/core/includes/entity.inc b/core/includes/entity.inc index 66c9470..3937636 100644 --- a/core/includes/entity.inc +++ b/core/includes/entity.inc @@ -765,15 +765,12 @@ function entity_get_render_display(EntityInterface $entity, $view_mode) { * @param string $bundle * The bundle. * @param string $form_mode - * (optional) The form mode. Defaults to 'default'. - * - * @todo Make the $form_mode parameter non-optional by introducing the form - * modes concept. + * The form mode. * * @return \Drupal\entity\Plugin\Core\Entity\EntityFormDisplay * The EntityFormDisplay object associated to the form mode. */ -function entity_get_form_display($entity_type, $bundle, $form_mode = 'default') { +function entity_get_form_display($entity_type, $bundle, $form_mode) { // Try loading the entity from configuration. $entity_form_display = entity_load('entity_form_display', $entity_type . '.' . $bundle . '.' . $form_mode); diff --git a/core/lib/Drupal/Core/Entity/EntityFormController.php b/core/lib/Drupal/Core/Entity/EntityFormController.php index 2332d06..f852b9c 100644 --- a/core/lib/Drupal/Core/Entity/EntityFormController.php +++ b/core/lib/Drupal/Core/Entity/EntityFormController.php @@ -7,6 +7,8 @@ namespace Drupal\Core\Entity; +use Drupal\entity\EntityFormDisplayInterface; + /** * Base class for entity form controllers. */ @@ -36,11 +38,10 @@ public function __construct($operation) { * Implements \Drupal\Core\Entity\EntityFormControllerInterface::build(). */ public function build(array $form, array &$form_state, EntityInterface $entity) { - - // During the initial form build, add the entity to the form state for use - // during form building and processing. During a rebuild, use what is in the - // form state. - if (!$this->getEntity($form_state)) { + // During the initial form build, add the entity and the form display to the + // form state for use during form building and processing. During a rebuild, + // use what is in the form state. + if (!$this->getEntity($form_state) || !$this->getFormDisplay($form_state)) { $this->init($form_state, $entity); } @@ -66,6 +67,12 @@ protected function init(array &$form_state, EntityInterface $entity) { $form_state['controller'] = $this; $this->setEntity($entity, $form_state); $this->prepareEntity($entity); + + // @todo Allow the usage of different form modes by exposing a hook and the + // UI for them. + $form_display = entity_get_render_form_display($entity, 'default'); + $this->prepareFormDisplay($form_display, $entity, 'default'); + $this->setFormDisplay($form_display, $form_state); } /** @@ -74,21 +81,6 @@ protected function init(array &$form_state, EntityInterface $entity) { * @see Drupal\Core\Entity\EntityFormController::build() */ public function form(array $form, array &$form_state, EntityInterface $entity) { - // Get the entity_form_display object for this form. - $form_display = entity_get_render_form_display($entity, 'default'); - - // Let modules alter the form display. - $form_display_context = array( - 'entity_type' => $entity->entityType(), - 'bundle' => $entity->bundle(), - 'form_mode' => $this->operation, - ); - drupal_alter('entity_form_display', $form_display, $form_display_context); - - // Persist the altered form display in $form_state so we can use it during - // validation and submit. - $form_state['form_display'] = $form_display; - // @todo Exploit the Field API to generate the default widgets for the // entity properties. $info = $entity->entityInfo(); @@ -97,7 +89,7 @@ public function form(array $form, array &$form_state, EntityInterface $entity) { } // Assign the weights configured in the form display. - foreach ($form_display->getComponents() as $name => $options) { + foreach ($this->getFormDisplay($form_state)->getComponents() as $name => $options) { if (isset($form[$name])) { $form[$name]['#weight'] = $options['weight']; } @@ -365,6 +357,33 @@ protected function prepareEntity(EntityInterface $entity) { } /** + * {@inheritdoc} + */ + public function getFormDisplay(array $form_state) { + return isset($form_state['form_display']) ? $form_state['form_display'] : NULL; + } + + /** + * {@inheritdoc} + */ + protected function setFormDisplay(EntityFormDisplayInterface $form_display, array &$form_state) { + $form_state['form_display'] = $form_display; + } + + /** + * {@inheritdoc} + */ + protected function prepareFormDisplay(EntityFormDisplayInterface $form_display, EntityInterface $entity, $form_mode) { + // Let modules alter the form display. + $form_display_context = array( + 'entity_type' => $entity->entityType(), + 'bundle' => $entity->bundle(), + 'form_mode' => $form_mode, + ); + \Drupal::moduleHandler()->alter('entity_form_display', $form_display, $form_display_context); + } + + /** * Implements \Drupal\Core\Entity\EntityFormControllerInterface::getOperation(). */ public function getOperation() { diff --git a/core/lib/Drupal/Core/Entity/EntityFormControllerInterface.php b/core/lib/Drupal/Core/Entity/EntityFormControllerInterface.php index 09a06bf..9ae492d 100644 --- a/core/lib/Drupal/Core/Entity/EntityFormControllerInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityFormControllerInterface.php @@ -7,6 +7,8 @@ namespace Drupal\Core\Entity; +use Drupal\entity\EntityFormDisplayInterface; + /** * Defines a common interface for entity form controller classes. */ @@ -63,6 +65,30 @@ public function isDefaultFormLangcode(array $form_state); public function getOperation(); /** + * Returns the form display. + * + * @param array $form_state + * An associative array containing the current state of the form. + * + * @return \Drupal\entity\EntityFormDisplayInterface + * The current form display. + */ + public function getFormDisplay(array $form_state); + + /** + * Sets the form display. + * + * Sets the form display which will be used for populating form element + * defaults. + * + * @param \Drupal\entity\EntityFormDisplayInterface $form_display + * The form display that the current form operates with. + * @param array $form_state + * An associative array containing the current state of the form. + */ + public function setFormDisplay(EntityFormDisplayInterface $form_display, array &$form_state); + + /** * Returns the form entity. * * The form entity which has been used for populating form element defaults. diff --git a/core/lib/Drupal/Core/Entity/EntityFormControllerNG.php b/core/lib/Drupal/Core/Entity/EntityFormControllerNG.php index a2b17c3..8e682ef 100644 --- a/core/lib/Drupal/Core/Entity/EntityFormControllerNG.php +++ b/core/lib/Drupal/Core/Entity/EntityFormControllerNG.php @@ -23,21 +23,6 @@ class EntityFormControllerNG extends EntityFormController { * Overrides EntityFormController::form(). */ public function form(array $form, array &$form_state, EntityInterface $entity) { - // Get the entity_form_display object for this form. - $form_display = entity_get_render_form_display($entity, 'default'); - - // Let modules alter the form display. - $form_display_context = array( - 'entity_type' => $entity->entityType(), - 'bundle' => $entity->bundle(), - 'form_mode' => $this->operation, - ); - drupal_alter('entity_form_display', $form_display, $form_display_context); - - // Persist the altered form display in $form_state so we can use it during - // validation and submit. - $form_state['form_display'] = $form_display; - // @todo Exploit the Field API to generate the default widgets for the // entity fields. $info = $entity->entityInfo(); @@ -46,7 +31,7 @@ public function form(array $form, array &$form_state, EntityInterface $entity) { } // Assign the weights configured in the form display. - foreach ($form_display->getComponents() as $name => $options) { + foreach ($this->getFormDisplay($form_state)->getComponents() as $name => $options) { if (isset($form[$name])) { $form[$name]['#weight'] = $options['weight']; } diff --git a/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php b/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php index f846594..e8e7eb9 100644 --- a/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php +++ b/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php @@ -175,7 +175,7 @@ public function testFieldComponent() { $random_value = $this->randomString(); $formatter->randomValue = $random_value; $formatter = $display->getFormatter($field['field_name']); - $this->assertEqual($formatter->randomValue, $random_value ); + $this->assertEqual($formatter->randomValue, $random_value); // Check that changing the definition creates a new formatter. $display->setComponent($field['field_name'], array( diff --git a/core/modules/entity/lib/Drupal/entity/Tests/EntityFormDisplayTest.php b/core/modules/entity/lib/Drupal/entity/Tests/EntityFormDisplayTest.php index f30b69c..b92cfbf 100644 --- a/core/modules/entity/lib/Drupal/entity/Tests/EntityFormDisplayTest.php +++ b/core/modules/entity/lib/Drupal/entity/Tests/EntityFormDisplayTest.php @@ -96,7 +96,7 @@ public function testFieldComponent() { $random_value = $this->randomString(); $widget->randomValue = $random_value; $widget = $form_display->getWidget($field['field_name']); - $this->assertEqual($widget->randomValue, $random_value ); + $this->assertEqual($widget->randomValue, $random_value); // Check that changing the definition creates a new widget. $form_display->setComponent($field['field_name'], array( diff --git a/core/modules/field/field.api.php b/core/modules/field/field.api.php index 01d72aa..a8c5157 100644 --- a/core/modules/field/field.api.php +++ b/core/modules/field/field.api.php @@ -32,7 +32,8 @@ * - label: The human readable name of the element. * - description: A short description of the element contents. * - weight: The default weight of the element. - * - visible: The default visibility of the element. + * - visible: (optional) The default visibility of the element. Only for + * 'display' context. Defaults to TRUE. * - edit: (optional) String containing markup (normally a link) used as the * element's 'edit' operation in the administration interface. Only for * 'form' context.