From 2227375f8b0d31bea055bbf24656a754b564dec0 Mon Sep 17 00:00:00 2001 From: xjm Date: Mon, 15 Oct 2018 20:12:33 -0500 Subject: [PATCH] Issue #2990517 by tim.plunkett, alphex, xjm, phenaproxima, neclimdul: Adding a display mode to a content type using layout, and disabling layout on that new display mode removes the layout_builder__layout field and breaks layout in already configured display modes --- .../Entity/LayoutBuilderEntityViewDisplay.php | 29 +++++- .../src/Functional/LayoutDisplayTest.php | 94 +++++++++++++++++++ 2 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 core/modules/layout_builder/tests/src/Functional/LayoutDisplayTest.php diff --git a/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php b/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php index 78ff430b0095..2bdeb0387020 100644 --- a/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php +++ b/core/modules/layout_builder/src/Entity/LayoutBuilderEntityViewDisplay.php @@ -112,8 +112,8 @@ class LayoutBuilderEntityViewDisplay extends BaseEntityViewDisplay implements La if ($new_value) { $this->addSectionField($entity_type_id, $bundle, 'layout_builder__layout'); } - elseif ($field = FieldConfig::loadByName($entity_type_id, $bundle, 'layout_builder__layout')) { - $field->delete(); + else { + $this->removeSectionField($entity_type_id, $bundle, 'layout_builder__layout'); } } @@ -139,6 +139,31 @@ class LayoutBuilderEntityViewDisplay extends BaseEntityViewDisplay implements La } } + /** + * Removes a layout section field if it is no longer needed. + * + * Because the field is shared across all view modes, the field will only be + * removed if no other view modes are using it. + * + * @param string $entity_type_id + * The entity type ID. + * @param string $bundle + * The bundle. + * @param string $field_name + * The name for the layout section field. + */ + protected function removeSectionField($entity_type_id, $bundle, $field_name) { + $query = $this->entityTypeManager()->getStorage($this->getEntityTypeId())->getQuery() + ->condition('targetEntityType', $this->getTargetEntityTypeId()) + ->condition('bundle', $this->getTargetBundle()) + ->condition('mode', $this->getMode(), '<>') + ->condition('third_party_settings.layout_builder.allow_custom', TRUE); + $enabled = (bool) $query->count()->execute(); + if (!$enabled && $field = FieldConfig::loadByName($entity_type_id, $bundle, $field_name)) { + $field->delete(); + } + } + /** * Adds a layout section field to a given bundle. * diff --git a/core/modules/layout_builder/tests/src/Functional/LayoutDisplayTest.php b/core/modules/layout_builder/tests/src/Functional/LayoutDisplayTest.php new file mode 100644 index 000000000000..4b26c628bc93 --- /dev/null +++ b/core/modules/layout_builder/tests/src/Functional/LayoutDisplayTest.php @@ -0,0 +1,94 @@ +drupalPlaceBlock('local_tasks_block'); + + $this->createContentType([ + 'type' => 'bundle_with_section_field', + ]); + $this->createNode(['type' => 'bundle_with_section_field']); + + $this->drupalLogin($this->drupalCreateUser([ + 'configure any layout', + 'administer node display', + 'administer display modes', + ], 'foobar')); + } + + /** + * Tests the interaction between multiple view modes. + */ + public function testMultipleViewModes() { + $assert_session = $this->assertSession(); + $page = $this->getSession()->getPage(); + $field_ui_prefix = 'admin/structure/types/manage/bundle_with_section_field/display'; + + // Enable Layout Builder for the default view modes, and overrides. + $this->drupalGet("$field_ui_prefix/default"); + $page->checkField('layout[enabled]'); + $page->pressButton('Save'); + $page->checkField('layout[allow_custom]'); + $page->pressButton('Save'); + + $this->drupalGet('node/1'); + $assert_session->pageTextNotContains('Powered by Drupal'); + + $assert_session->linkExists('Layout'); + $this->clickLink('Layout'); + $assert_session->linkExists('Add Block'); + $this->clickLink('Add Block'); + $assert_session->linkExists('Powered by Drupal'); + $this->clickLink('Powered by Drupal'); + $page->pressButton('Add Block'); + $assert_session->linkExists('Save Layout'); + $this->clickLink('Save Layout'); + $assert_session->pageTextContains('Powered by Drupal'); + + // Add a new view mode. + $this->drupalGet('admin/structure/display-modes/view/add/node'); + $page->fillField('label', 'New'); + $page->fillField('id', 'new'); + $page->pressButton('Save'); + + // Enable the new view mode. + $this->drupalGet("$field_ui_prefix/default"); + $page->checkField('display_modes_custom[new]'); + $page->pressButton('Save'); + + // Enable and disable Layout Builder for the new view mode. + $this->drupalGet("$field_ui_prefix/new"); + $page->checkField('layout[enabled]'); + $page->pressButton('Save'); + $page->uncheckField('layout[enabled]'); + $page->pressButton('Save'); + $page->pressButton('Confirm'); + + // The node using the default view mode still contains its overrides. + $this->drupalGet('node/1'); + $assert_session->pageTextContains('Powered by Drupal'); + } + +}