From 38d72469498793cf118fdf491c4024d96fe5e31f Mon Sep 17 00:00:00 2001 From: catch <catch@35733.no-reply.drupal.org> Date: Wed, 24 Apr 2024 10:45:19 +0100 Subject: [PATCH] Issue #3423920 by DanielVeza, smustgrave, acbramley, bradallenfisher, larowlan, alexpott: Custom Blocks with Layout Builder Enabled No Longer Use Front End Theme --- .../OverridesSectionStorage.php | 1 + ...LayoutBuilderOverridesEditingThemeTest.php | 122 ++++++++++++++++++ .../src/Unit/OverridesSectionStorageTest.php | 9 ++ 3 files changed, 132 insertions(+) create mode 100644 core/modules/layout_builder/tests/src/Functional/LayoutBuilderOverridesEditingThemeTest.php diff --git a/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php b/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php index 393941aac1c8..1fefa2b455ea 100644 --- a/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php +++ b/core/modules/layout_builder/src/Plugin/SectionStorage/OverridesSectionStorage.php @@ -241,6 +241,7 @@ public function buildRoutes(RouteCollection $collection) { // Ensure that upcasting is run in the correct order. $options['parameters']['section_storage'] = []; $options['parameters'][$entity_type_id]['type'] = 'entity:' . $entity_type_id; + $options['_admin_route'] = FALSE; $template = $entity_type->getLinkTemplate('canonical') . '/layout'; $this->buildLayoutRoutes($collection, $this->getPluginDefinition(), $template, $defaults, $requirements, $options, $entity_type_id, $entity_type_id); diff --git a/core/modules/layout_builder/tests/src/Functional/LayoutBuilderOverridesEditingThemeTest.php b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderOverridesEditingThemeTest.php new file mode 100644 index 000000000000..d89b3a7c0e09 --- /dev/null +++ b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderOverridesEditingThemeTest.php @@ -0,0 +1,122 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\Tests\layout_builder\Functional; + +use Drupal\block_content\Entity\BlockContent; +use Drupal\block_content\Entity\BlockContentType; +use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay; +use Drupal\Tests\layout_builder\Traits\EnableLayoutBuilderTrait; +use Drupal\user\Entity\Role; +use Drupal\user\UserInterface; + +/** + * Tests overrides editing uses the correct theme. + * + * Block content is used for this test as its canonical & editing routes + * are in the admin section, so we need to test that layout builder editing + * uses the front end theme. + * + * @group layout_builder + */ +class LayoutBuilderOverridesEditingThemeTest extends LayoutBuilderTestBase { + + use EnableLayoutBuilderTrait; + + /** + * {@inheritdoc} + */ + protected $defaultTheme = 'test_theme'; + + /** + * {@inheritdoc} + */ + protected static $modules = [ + 'block_content', + ]; + + /** + * Permissions to grant admin user. + */ + protected array $permissions = [ + 'administer blocks', + 'access block library', + 'administer block types', + 'administer block content', + 'administer block_content display', + 'configure any layout', + 'view the administration theme', + 'edit any basic block content', + ]; + + /** + * Admin user. + */ + protected UserInterface $adminUser; + + /** + * {@inheritdoc} + */ + protected function setUp(): void { + parent::setUp(); + // Create a basic block content type. + BlockContentType::create([ + 'id' => 'basic', + 'label' => 'Basic', + 'revision' => FALSE, + ])->save(); + $this->adminUser = $this->drupalCreateUser($this->permissions); + } + + /** + * Tests editing block content with Layout Builder. + */ + public function testEditing(): void { + // Create a new role for additional permissions needed. + $role = Role::create([ + 'id' => 'layout_builder_tester', + 'label' => 'Layout Builder Tester', + ]); + // Set a different theme for the admin pages. So we can assert the theme + // in Layout Builder is not the same as the admin theme. + \Drupal::service('theme_installer')->install(['claro']); + $this->config('system.theme')->set('admin', 'claro')->save(); + + // Enable layout builder for the block content display. + $display = LayoutBuilderEntityViewDisplay::create([ + 'targetEntityType' => 'block_content', + 'bundle' => 'basic', + 'mode' => 'default', + 'status' => TRUE, + ]); + $display->save(); + $this->enableLayoutBuilder($display); + $role->grantPermission('configure all basic block_content layout overrides'); + $role->save(); + $this->adminUser + ->addRole($role->id()) + ->save(); + $this->drupalLogin($this->adminUser); + // Create a block content and test the themes used. + $blockContent = BlockContent::create([ + 'info' => $this->randomMachineName(), + 'type' => 'basic', + 'langcode' => 'en', + ]); + $blockContent->save(); + // Assert the test_theme is being used for overrides. + $this->drupalGet('admin/content/block/' . $blockContent->id() . '/layout'); + $this->assertSession()->statusCodeEquals(200); + // Assert the test_theme is being used. + $this->assertSession()->responseContains('test_theme/kitten.css'); + // Assert the claro theme is not being used. + $this->assertSession()->elementNotExists('css', '#block-claro-content'); + // Assert the default still uses the test_theme. + $this->drupalGet('admin/structure/block-content/manage/basic/display/default/layout'); + $this->assertSession()->statusCodeEquals(200); + $this->assertSession()->responseContains('test_theme/kitten.css'); + $this->assertSession()->elementNotExists('css', '#block-claro-content'); + } + +} diff --git a/core/modules/layout_builder/tests/src/Unit/OverridesSectionStorageTest.php b/core/modules/layout_builder/tests/src/Unit/OverridesSectionStorageTest.php index a9f8ba3b4c2a..fbafb7eef13b 100644 --- a/core/modules/layout_builder/tests/src/Unit/OverridesSectionStorageTest.php +++ b/core/modules/layout_builder/tests/src/Unit/OverridesSectionStorageTest.php @@ -272,6 +272,7 @@ public function testBuildRoutes() { 'from_canonical' => ['type' => 'entity:from_canonical'], ], '_layout_builder' => TRUE, + '_admin_route' => FALSE, ] ), 'layout_builder.overrides.from_canonical.discard_changes' => new Route( @@ -292,6 +293,7 @@ public function testBuildRoutes() { 'from_canonical' => ['type' => 'entity:from_canonical'], ], '_layout_builder' => TRUE, + '_admin_route' => FALSE, ] ), 'layout_builder.overrides.from_canonical.revert' => new Route( @@ -312,6 +314,7 @@ public function testBuildRoutes() { 'from_canonical' => ['type' => 'entity:from_canonical'], ], '_layout_builder' => TRUE, + '_admin_route' => FALSE, ] ), 'layout_builder.overrides.with_string_id.view' => new Route( @@ -332,6 +335,7 @@ public function testBuildRoutes() { 'with_string_id' => ['type' => 'entity:with_string_id'], ], '_layout_builder' => TRUE, + '_admin_route' => FALSE, ] ), 'layout_builder.overrides.with_string_id.discard_changes' => new Route( @@ -351,6 +355,7 @@ public function testBuildRoutes() { 'with_string_id' => ['type' => 'entity:with_string_id'], ], '_layout_builder' => TRUE, + '_admin_route' => FALSE, ] ), 'layout_builder.overrides.with_string_id.revert' => new Route( @@ -370,6 +375,7 @@ public function testBuildRoutes() { 'with_string_id' => ['type' => 'entity:with_string_id'], ], '_layout_builder' => TRUE, + '_admin_route' => FALSE, ] ), 'layout_builder.overrides.with_integer_id.view' => new Route( @@ -391,6 +397,7 @@ public function testBuildRoutes() { 'with_integer_id' => ['type' => 'entity:with_integer_id'], ], '_layout_builder' => TRUE, + '_admin_route' => FALSE, ] ), 'layout_builder.overrides.with_integer_id.discard_changes' => new Route( @@ -411,6 +418,7 @@ public function testBuildRoutes() { 'with_integer_id' => ['type' => 'entity:with_integer_id'], ], '_layout_builder' => TRUE, + '_admin_route' => FALSE, ] ), 'layout_builder.overrides.with_integer_id.revert' => new Route( @@ -431,6 +439,7 @@ public function testBuildRoutes() { 'with_integer_id' => ['type' => 'entity:with_integer_id'], ], '_layout_builder' => TRUE, + '_admin_route' => FALSE, ] ), ]; -- GitLab