From 86e257b9e57e6080a4e9efcded79ab38b36e6349 Mon Sep 17 00:00:00 2001 From: xjm <xjm@65776.no-reply.drupal.org> Date: Thu, 21 Feb 2019 08:34:34 -0600 Subject: [PATCH] Issue #3033686 by tedbow, tim.plunkett: Saving Layout override will revert other field values to their values when the Layout was started --- .../src/Form/OverridesEntityForm.php | 21 ++++++++- .../src/Functional/LayoutBuilderTest.php | 47 +++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/core/modules/layout_builder/src/Form/OverridesEntityForm.php b/core/modules/layout_builder/src/Form/OverridesEntityForm.php index c2a28aa2f9cb..ed6c35751829 100644 --- a/core/modules/layout_builder/src/Form/OverridesEntityForm.php +++ b/core/modules/layout_builder/src/Form/OverridesEntityForm.php @@ -114,9 +114,28 @@ public function buildForm(array $form, FormStateInterface $form_state, SectionSt * {@inheritdoc} */ public function buildEntity(array $form, FormStateInterface $form_state) { + /** @var \Drupal\Core\Entity\FieldableEntityInterface $section_storage_entity */ + $section_storage_entity = $this->sectionStorage->getContextValue('entity'); + + // @todo Replace with new API in + // https://www.drupal.org/project/drupal/issues/2942907. + /** @var \Drupal\Core\Entity\FieldableEntityInterface $active_entity */ + $active_entity = $this->entityTypeManager->getStorage($section_storage_entity->getEntityTypeId())->load($section_storage_entity->id()); + + // Any fields that are not editable on this form should be updated with the + // value from the active entity for editing. This avoids overwriting fields + // that have been updated since the entity was stored in the section + // storage. + $edited_field_names = $this->getEditedFieldNames($form_state); + foreach ($section_storage_entity->getFieldDefinitions() as $field_name => $field_definition) { + if (!in_array($field_name, $edited_field_names) && !$field_definition->isReadOnly() && !$field_definition->isComputed()) { + $section_storage_entity->{$field_name} = $active_entity->{$field_name}; + } + } + // \Drupal\Core\Entity\EntityForm::buildEntity() clones the entity object. // Keep it in sync with the one used by the section storage. - $this->setEntity($this->sectionStorage->getContextValue('entity')); + $this->setEntity($section_storage_entity); $entity = parent::buildEntity($form, $form_state); $this->sectionStorage->setContextValue('entity', $entity); return $entity; diff --git a/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php index b3b1a4d90df0..05470c229d21 100644 --- a/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php +++ b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php @@ -106,6 +106,53 @@ public function testOverrides() { $assert_session->pageTextNotContains('This is an override'); } + /** + * Tests that the Layout Builder preserves entity values. + */ + public function testPreserverEntityValues() { + $assert_session = $this->assertSession(); + $page = $this->getSession()->getPage(); + + $this->drupalLogin($this->drupalCreateUser([ + 'configure any layout', + 'administer node display', + ])); + + // From the manage display page, go to manage the layout. + $this->drupalGet('admin/structure/types/manage/bundle_with_section_field/display/default'); + $this->drupalPostForm(NULL, ['layout[enabled]' => TRUE], 'Save'); + $this->drupalPostForm(NULL, ['layout[allow_custom]' => TRUE], 'Save'); + // @todo This should not be necessary. + $this->container->get('entity_field.manager')->clearCachedFieldDefinitions(); + + $this->drupalGet('node/1'); + $assert_session->pageTextContains('The first node body'); + + // Create a layout override which will store the current node in the + // tempstore. + $page->clickLink('Layout'); + $page->clickLink('Add Block'); + $page->clickLink('Powered by Drupal'); + $page->pressButton('Add Block'); + + // Update the node to make a change that is not in the tempstore version. + $node = Node::load(1); + $node->set('body', 'updated body'); + $node->save(); + + $page->clickLink('View'); + $assert_session->pageTextNotContains('The first node body'); + $assert_session->pageTextContains('updated body'); + + $page->clickLink('Layout'); + $page->pressButton('Save layout'); + + // Ensure that saving the layout does not revert other field values. + $assert_session->addressEquals('node/1'); + $assert_session->pageTextNotContains('The first node body'); + $assert_session->pageTextContains('updated body'); + } + /** * {@inheritdoc} */ -- GitLab