diff --git a/core/lib/Drupal/Core/Entity/ContentEntityFormController.php b/core/lib/Drupal/Core/Entity/ContentEntityFormController.php index e3a4d7e..568d5cb 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityFormController.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityFormController.php @@ -48,46 +48,14 @@ public static function create(ContainerInterface $container) { * {@inheritdoc} */ public function form(array $form, array &$form_state) { + $form = parent::form($form, $form_state); $this->getFormDisplay($form_state)->buildForm($this->entity, $form, $form_state); - - // Add a process callback so we can assign weights and hide extra fields. - $form['#process'][] = array($this, 'processForm'); - return $form; } /** * {@inheritdoc} */ - public function processForm($element, $form_state, $form) { - parent::processForm($element, $form_state, $form); - - $form_display = $this->getFormDisplay($form_state); - - // Assign the weights configured in the form display. - // @todo: Once https://drupal.org/node/1875974 provides the missing API, - // only do it for 'extra fields', since other components have been taken - // care of in EntityViewDisplay::buildMultiple(). - foreach ($form_display->getComponents() as $name => $options) { - if (isset($element[$name])) { - $element[$name]['#weight'] = $options['weight']; - } - } - - // Hide extra fields. - $extra_fields = field_info_extra_fields($this->entity->getEntityTypeId(), $this->entity->bundle(), 'form'); - foreach ($extra_fields as $extra_field => $info) { - if (!$form_display->getComponent($extra_field)) { - $element[$extra_field]['#access'] = FALSE; - } - } - - return $element; - } - - /** - * {@inheritdoc} - */ public function validate(array $form, array &$form_state) { $this->updateFormLangcode($form_state); $entity = $this->buildEntity($form, $form_state); @@ -137,9 +105,7 @@ public function isDefaultFormLangcode(array $form_state) { /** * {@inheritdoc} */ - public function buildEntity(array $form, array &$form_state) { - $entity = clone $this->entity; - + protected function copyFormValuesToEntity(EntityInterface $entity, array $form, array &$form_state) { // First, extract values from widgets. $extracted = $this->getFormDisplay($form_state)->extractFormValues($entity, $form, $form_state); @@ -148,18 +114,9 @@ public function buildEntity(array $form, array &$form_state) { // that are not being edited within this form untouched. foreach ($form_state['values'] as $name => $values) { if ($entity->hasField($name) && !isset($extracted[$name])) { - $entity->$name = $values; + $entity->set($name, $values); } } - - // Invoke all specified builders for copying form values to entity fields. - if (isset($form['#entity_builders'])) { - foreach ($form['#entity_builders'] as $function) { - call_user_func_array($function, array($entity->getEntityTypeId(), $entity, &$form, &$form_state)); - } - } - - return $entity; } /** diff --git a/core/lib/Drupal/Core/Entity/EntityFormController.php b/core/lib/Drupal/Core/Entity/EntityFormController.php index cbe920d..28ace6b 100644 --- a/core/lib/Drupal/Core/Entity/EntityFormController.php +++ b/core/lib/Drupal/Core/Entity/EntityFormController.php @@ -9,7 +9,6 @@ use Drupal\Core\Form\FormBase; use Drupal\Core\Extension\ModuleHandlerInterface; -use Drupal\entity\Entity\EntityFormDisplay; /** * Base class for entity form controllers. @@ -134,7 +133,7 @@ protected function init(array &$form_state) { public function form(array $form, array &$form_state) { $entity = $this->entity; - // Add a process callback so we can assign weights and hide extra fields. + // Add a process callback. $form['#process'][] = array($this, 'processForm'); if (!isset($form['langcode'])) { @@ -331,7 +330,7 @@ public function buildEntity(array $form, array &$form_state) { // controller of the current request. $form_state['controller'] = $this; - $this->copyFormValuesToEntity($entity, $form_state); + $this->copyFormValuesToEntity($entity, $form, $form_state); // Invoke all specified builders for copying form values to entity // properties. @@ -352,10 +351,12 @@ public function buildEntity(array $form, array &$form_state) { * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity the current form should operate upon. + * @param array $form + * A nested array of form elements comprising the form. * @param array $form_state * An associative array containing the current state of the form. */ - protected function copyFormValuesToEntity(EntityInterface $entity, array $form_state) { + protected function copyFormValuesToEntity(EntityInterface $entity, array $form, array &$form_state) { // @todo: This relies on a method that only exists for config and content // entities, in a different way. Consider moving this logic to a config // entity specific implementation. diff --git a/core/modules/entity/lib/Drupal/entity/Entity/EntityFormDisplay.php b/core/modules/entity/lib/Drupal/entity/Entity/EntityFormDisplay.php index 35fcab4..29fd6e0 100644 --- a/core/modules/entity/lib/Drupal/entity/Entity/EntityFormDisplay.php +++ b/core/modules/entity/lib/Drupal/entity/Entity/EntityFormDisplay.php @@ -164,6 +164,32 @@ public function buildForm(ContentEntityInterface $entity, array &$form, array &$ $form[$name]['#weight'] = $this->getComponent($name)['weight']; } } + + // Add a process callback so we can assign weights and hide extra fields. + $form['#process'][] = array($this, 'processForm'); + } + + /** + * Process callback: assigns weights and hides extra fields. + * + * @see \Drupal\entity\Entity\EntityFormDisplay::buildForm() + */ + public function processForm($element, $form_state, $form) { + // Assign the weights configured in the form display. + foreach ($this->getComponents() as $name => $options) { + if (isset($element[$name])) { + $element[$name]['#weight'] = $options['weight']; + } + } + + // Hide extra fields. + $extra_fields = field_info_extra_fields($this->targetEntityType, $this->bundle, 'form'); + foreach ($extra_fields as $extra_field => $info) { + if (!$this->getComponent($extra_field)) { + $element[$extra_field]['#access'] = FALSE; + } + } + return $element; } /** diff --git a/core/modules/image/lib/Drupal/image/Form/ImageStyleEditForm.php b/core/modules/image/lib/Drupal/image/Form/ImageStyleEditForm.php index a589feb..41465a2 100644 --- a/core/modules/image/lib/Drupal/image/Form/ImageStyleEditForm.php +++ b/core/modules/image/lib/Drupal/image/Form/ImageStyleEditForm.php @@ -252,7 +252,7 @@ protected function updateEffectWeights(array $effects) { /** * {@inheritdoc} */ - protected function copyFormValuesToEntity(EntityInterface $entity, array $form_state) { + protected function copyFormValuesToEntity(EntityInterface $entity, array $form, array &$form_state) { foreach ($form_state['values'] as $key => $value) { // Do not copy effects here, see self::updateEffectWeights(). if ($key != 'effects') {