Skip to content
Snippets Groups Projects
Commit 86e257b9 authored by Jess's avatar Jess
Browse files

Issue #3033686 by tedbow, tim.plunkett: Saving Layout override will revert...

Issue #3033686 by tedbow, tim.plunkett: Saving Layout override will revert other field values to their values when the Layout was started
parent c7f47000
No related branches found
No related tags found
Loading
......@@ -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;
......
......@@ -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}
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment