diff --git a/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php b/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php index 813b64c8e0..1bb24e07cb 100644 --- a/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php +++ b/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php @@ -6,6 +6,7 @@ use Drupal\Core\Entity\Entity\EntityViewDisplay as BaseEntityViewDisplay; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\FieldableEntityInterface; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Plugin\Context\Context; use Drupal\Core\Plugin\Context\ContextDefinition; use Drupal\Core\StringTranslation\TranslatableMarkup; @@ -204,35 +205,7 @@ protected function addSectionField($entity_type_id, $bundle, $field_name) { * {@inheritdoc} */ protected function getDefaultRegion() { - if (!$this->hasSection(0)) { - return parent::getDefaultRegion(); - } - - $section = $this->getSection(0); - return $this->getLayoutDefinition($section->getLayoutId())->getDefaultRegion(); - } - - /** - * Gets a layout definition. - * - * @param string $layout_id - * The layout ID. - * - * @return \Drupal\Core\Layout\LayoutDefinition - * The layout definition. - */ - protected function getLayoutDefinition($layout_id) { - return $this->layoutPluginManager()->getDefinition($layout_id); - } - - /** - * Wraps the layout plugin manager. - * - * @return \Drupal\Core\Layout\LayoutPluginManagerInterface - * The layout plugin manager. - */ - protected function layoutPluginManager() { - return \Drupal::service('plugin.manager.core.layout'); + return $this->getDefaultSection()->getDefaultRegion(); } /** @@ -397,8 +370,12 @@ protected function getRouteParameters() { */ public function onDependencyRemoval(array $dependencies) { $return = parent::onDependencyRemoval($dependencies); - foreach ($dependencies['config'] as $field_config) { - $id = 'field_block:' . $this->getTargetEntityTypeId() . ':' . $field_config->getName(); + foreach ($dependencies['config'] as $config_entity) { + if (!$config_entity instanceof FieldDefinitionInterface) { + continue; + } + + $id = 'field_block:' . $this->getTargetEntityTypeId() . ':' . $config_entity->getName(); foreach ($this->getSections() as $delta => $section) { foreach ($section->getComponents() as $uuid => $component) { if ($component->getPluginId() === $id) { @@ -428,24 +405,34 @@ public function setComponent($name, array $options = []) { // Provide backwards compatibility by converting to a section component. $field_definition = $this->getFieldDefinition($name); if ($field_definition && $field_definition->isDisplayConfigurable('view') && isset($options['type'])) { - if (!$this->hasSection(0)) { - $section = new Section('layout_onecol'); - $this->appendSection($section); - } - else { - $section = $this->getSection(0); - } - $configuration = []; $configuration['id'] = 'field_block:' . $this->getTargetEntityTypeId() . ':' . $name; $configuration['label_display'] = FALSE; $keys = array_flip(['type', 'label', 'settings', 'third_party_settings']); $configuration['formatter'] = array_intersect_key($options, $keys); $configuration['context_mapping']['entity'] = 'layout_builder.entity'; - $new_component = (new SectionComponent(\Drupal::service('uuid')->generate(), $options['region'], $configuration)); - $section->appendComponent($new_component); + + $region = isset($options['region']) ? $options['region'] : $this->getDefaultRegion(); + $new_component = (new SectionComponent(\Drupal::service('uuid')->generate(), $region, $configuration)); + $this->getDefaultSection()->appendComponent($new_component); } return $this; } + /** + * Gets a default section. + * + * @return \Drupal\layout_builder\Section + * The default section. + */ + protected function getDefaultSection() { + // If no section exists, append a new one. + if (!$this->hasSection(0)) { + $this->appendSection(new Section('layout_onecol')); + } + + // Return the first section. + return $this->getSection(0); + } + } diff --git a/core/modules/layout_builder/src/Section.php b/core/modules/layout_builder/src/Section.php index 20e9dec579..aa103f133f 100644 --- a/core/modules/layout_builder/src/Section.php +++ b/core/modules/layout_builder/src/Section.php @@ -132,6 +132,16 @@ public function setLayoutSettings(array $layout_settings) { return $this; } + /** + * Gets the default region. + * + * @return string + * The machine-readable name of the default region. + */ + public function getDefaultRegion() { + return $this->layoutPluginManager()->getDefinition($this->getLayoutId())->getDefaultRegion(); + } + /** * Returns the components of the section. * diff --git a/core/modules/layout_builder/tests/src/Functional/LayoutSectionTest.php b/core/modules/layout_builder/tests/src/Functional/LayoutSectionTest.php index 67e8a06e3f..5d3f17d1e0 100644 --- a/core/modules/layout_builder/tests/src/Functional/LayoutSectionTest.php +++ b/core/modules/layout_builder/tests/src/Functional/LayoutSectionTest.php @@ -48,6 +48,7 @@ protected function setUp() { 'configure any layout', 'administer node display', 'administer node fields', + 'administer content types', ], 'foobar')); } @@ -281,12 +282,14 @@ public function testLayoutUrlNoSectionField() { * Tests that deleting a field removes it from the layout. */ public function testLayoutDeletingField() { + $assert_session = $this->assertSession(); + LayoutBuilderEntityViewDisplay::load('node.bundle_with_section_field.default') - ->setComponent('body', ['type' => 'text_default', 'region' => 'content']) + ->setComponent('body', ['type' => 'text_default']) ->save(); $this->drupalGet('/admin/structure/types/manage/bundle_with_section_field/display-layout/default'); - $this->assertSession()->statusCodeEquals(200); + $assert_session->statusCodeEquals(200); // Delete the field from both bundles. $this->drupalGet('/admin/structure/types/manage/bundle_with_section_field/fields/node.bundle_with_section_field.body/delete'); @@ -295,7 +298,23 @@ public function testLayoutDeletingField() { $this->submitForm([], 'Delete'); $this->drupalGet('/admin/structure/types/manage/bundle_with_section_field/display-layout/default'); - $this->assertSession()->statusCodeEquals(200); + $assert_session->statusCodeEquals(200); + } + + /** + * Tests that deleting a bundle removes the layout. + */ + public function testLayoutDeletingBundle() { + $assert_session = $this->assertSession(); + + $display = LayoutBuilderEntityViewDisplay::load('node.bundle_with_section_field.default'); + $this->assertInstanceOf(LayoutBuilderEntityViewDisplay::class, $display); + + $this->drupalPostForm('/admin/structure/types/manage/bundle_with_section_field/delete', [], 'Delete'); + $assert_session->statusCodeEquals(200); + + $display = LayoutBuilderEntityViewDisplay::load('node.bundle_with_section_field.default'); + $this->assertNull($display); } /** diff --git a/core/modules/layout_builder/tests/src/Kernel/LayoutBuilderCompatibilityTestBase.php b/core/modules/layout_builder/tests/src/Kernel/LayoutBuilderCompatibilityTestBase.php index 5e405bbca0..d5a4cd0b8a 100644 --- a/core/modules/layout_builder/tests/src/Kernel/LayoutBuilderCompatibilityTestBase.php +++ b/core/modules/layout_builder/tests/src/Kernel/LayoutBuilderCompatibilityTestBase.php @@ -69,7 +69,7 @@ protected function setUp() { 'status' => TRUE, ]); $this->display - ->setComponent('test_field_display_configurable', ['region' => 'content', 'weight' => 5]) + ->setComponent('test_field_display_configurable', ['weight' => 5]) ->save(); // Create an entity with fields that are configurable and non-configurable. diff --git a/core/modules/layout_builder/tests/src/Kernel/LayoutBuilderInstallTest.php b/core/modules/layout_builder/tests/src/Kernel/LayoutBuilderInstallTest.php index 59ce3535db..fa646df1c9 100644 --- a/core/modules/layout_builder/tests/src/Kernel/LayoutBuilderInstallTest.php +++ b/core/modules/layout_builder/tests/src/Kernel/LayoutBuilderInstallTest.php @@ -72,7 +72,7 @@ public function testCompatibility() { $this->display = $this->reloadEntity($this->display); $this->display - ->setComponent('test_field_display_post_install', ['region' => 'content', 'weight' => 50]) + ->setComponent('test_field_display_post_install', ['weight' => 50]) ->save(); $new_expected_fields = [ 'field field--name-name field--type-string field--label-hidden field__item',