diff --git a/core/modules/field_layout/field_layout.install b/core/modules/field_layout/field_layout.install index f3162b9..5956bf1 100644 --- a/core/modules/field_layout/field_layout.install +++ b/core/modules/field_layout/field_layout.install @@ -24,3 +24,19 @@ function field_layout_install() { // Invalidate the render cache since all content will now have a layout. Cache::invalidateTags(['rendered']); } + +/** + * Implements hook_uninstall(). + */ +function field_layout_uninstall() { + // Reset each entity display to use the one-column layout to best approximate + // the absence of layouts. + $entity_save = function (EntityDisplayWithLayoutInterface $entity) { + $entity->setLayoutId('layout_onecol')->save(); + }; + array_map($entity_save, EntityViewDisplay::loadMultiple()); + array_map($entity_save, EntityFormDisplay::loadMultiple()); + + // Invalidate the render cache since all content will no longer have a layout. + Cache::invalidateTags(['rendered']); +} diff --git a/core/modules/field_layout/src/Entity/FieldLayoutEntityDisplayTrait.php b/core/modules/field_layout/src/Entity/FieldLayoutEntityDisplayTrait.php index 8d89d21..91cf936 100644 --- a/core/modules/field_layout/src/Entity/FieldLayoutEntityDisplayTrait.php +++ b/core/modules/field_layout/src/Entity/FieldLayoutEntityDisplayTrait.php @@ -45,6 +45,17 @@ public function getLayoutSettings() { * Implements \Drupal\field_layout\Display\EntityDisplayWithLayoutInterface::setLayoutId(). */ public function setLayoutId($layout_id, array $layout_settings = []) { + if ($this->getLayoutId() !== $layout_id) { + // @todo Devise a mechanism for mapping old regions to new ones in + // https://www.drupal.org/node/2796877. + $new_region = $this->getLayoutDefinition($layout_id)->getDefaultRegion(); + foreach ($this->getComponents() as $name => $component) { + if (isset($component['region']) && !isset($layout_regions[$component['region']])) { + $component['region'] = $new_region; + $this->setComponent($name, $component); + } + } + } $this->setThirdPartySetting('field_layout', 'id', $layout_id); $this->setThirdPartySetting('field_layout', 'settings', $layout_settings); return $this; diff --git a/core/modules/field_layout/src/Form/FieldLayoutEntityDisplayFormTrait.php b/core/modules/field_layout/src/Form/FieldLayoutEntityDisplayFormTrait.php index 9de90aa..b1256fb 100644 --- a/core/modules/field_layout/src/Form/FieldLayoutEntityDisplayFormTrait.php +++ b/core/modules/field_layout/src/Form/FieldLayoutEntityDisplayFormTrait.php @@ -162,40 +162,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { $layout_plugin->submitConfigurationForm($form['field_layouts']['settings_wrapper']['layout_settings'], SubformState::createForSubform($form['field_layouts']['settings_wrapper']['layout_settings'], $form, $form_state)); } - // If the layout is changing the regions will also change, update the - // placement of all fields. - if ($this->updateLayout($entity, $form_state)) { - // @todo Devise a mechanism for mapping old regions to new ones in - // https://www.drupal.org/node/2796877. - $new_region = $entity->getDefaultRegion(); - foreach ($form_state->getValue('fields') as $field_name => $values) { - if (($component = $entity->getComponent($field_name)) && $new_region !== 'hidden') { - $component['region'] = $new_region; - $entity->setComponent($field_name, $component); - } - else { - $entity->removeComponent($field_name); - } - } - } - } - - /** - * Updates the entity with a new layout. - * - * @param \Drupal\field_layout\Display\EntityDisplayWithLayoutInterface $entity - * The display entity. - * @param \Drupal\Core\Form\FormStateInterface $form_state - * The current form state. - * - * @return bool - * Returns TRUE if the layout has changed, FALSE if it is the same. - */ - protected function updateLayout(EntityDisplayWithLayoutInterface $entity, FormStateInterface $form_state) { - $old_layout = $entity->getLayoutId(); - $new_layout = $form_state->getValue('field_layout'); - $entity->setLayout($this->getLayout($entity, $form_state)); - return $old_layout !== $new_layout; + $entity->setLayout($layout_plugin); } /** diff --git a/core/modules/field_layout/tests/src/FunctionalJavascript/FieldLayoutTest.php b/core/modules/field_layout/tests/src/FunctionalJavascript/FieldLayoutTest.php index 46ab6c2..1336a70 100644 --- a/core/modules/field_layout/tests/src/FunctionalJavascript/FieldLayoutTest.php +++ b/core/modules/field_layout/tests/src/FunctionalJavascript/FieldLayoutTest.php @@ -258,7 +258,7 @@ protected function getRegionTitles() { * The machine name of the region. */ protected function assertFieldInRegion($field_selector, $region_name) { - $region_element = $this->getSession()->getPage()->find('css', ".field-layout-region--$region_name"); + $region_element = $this->getSession()->getPage()->find('css', ".layout-region--$region_name"); $this->assertNotNull($region_element); $this->assertSession()->fieldExists($field_selector, $region_element); } diff --git a/core/modules/field_layout/tests/src/Kernel/FieldLayoutEntityDisplayTest.php b/core/modules/field_layout/tests/src/Kernel/FieldLayoutEntityDisplayTest.php index 09f1d79..a70ecdd 100644 --- a/core/modules/field_layout/tests/src/Kernel/FieldLayoutEntityDisplayTest.php +++ b/core/modules/field_layout/tests/src/Kernel/FieldLayoutEntityDisplayTest.php @@ -129,7 +129,10 @@ public function testPreSave() { 'content' => [ 'foo' => [ 'type' => 'visible', - 'region' => 'content', + 'region' => 'main', + 'weight' => -4, + 'settings' => [], + 'third_party_settings' => [], ], ], 'hidden' => [ @@ -172,7 +175,10 @@ public function testPreSave() { 'content' => [ 'foo' => [ 'type' => 'visible', - 'region' => 'content', + 'region' => 'main', + 'weight' => -4, + 'settings' => [], + 'third_party_settings' => [], ], ], 'hidden' => [ @@ -203,6 +209,9 @@ public function testPreSave() { 'foo' => [ 'type' => 'visible', 'region' => 'content', + 'weight' => -4, + 'settings' => [], + 'third_party_settings' => [], ], ], 'hidden' => [