diff --git a/core/modules/field_layout/field_layout.install b/core/modules/field_layout/field_layout.install index 2ad8d8e..f3162b9 100644 --- a/core/modules/field_layout/field_layout.install +++ b/core/modules/field_layout/field_layout.install @@ -8,15 +8,15 @@ use Drupal\Core\Cache\Cache; use Drupal\Core\Entity\Entity\EntityFormDisplay; use Drupal\Core\Entity\Entity\EntityViewDisplay; -use Drupal\Core\Entity\EntityInterface; +use Drupal\field_layout\Display\EntityDisplayWithLayoutInterface; /** * Implements hook_install(). */ function field_layout_install() { - // Save each entity display in order to trigger ::preSave(). - $entity_save = function (EntityInterface $entity) { - $entity->save(); + // Ensure each entity display has a layout. + $entity_save = function (EntityDisplayWithLayoutInterface $entity) { + $entity->ensureLayout()->save(); }; array_map($entity_save, EntityViewDisplay::loadMultiple()); array_map($entity_save, EntityFormDisplay::loadMultiple()); @@ -24,10 +24,3 @@ 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() { - \Drupal::state()->set('field_layout_being_uninstalled', TRUE); -} diff --git a/core/modules/field_layout/field_layout.layouts.yml b/core/modules/field_layout/field_layout.layouts.yml index 2852708..7f51a69 100644 --- a/core/modules/field_layout/field_layout.layouts.yml +++ b/core/modules/field_layout/field_layout.layouts.yml @@ -1,17 +1,18 @@ -field_layout_onecol: +# @todo Move to layout_discovery in https://www.drupal.org/node/2840832. +layout_onecol: label: 'One column' path: layouts/onecol - template: field-layout--onecol + template: layout--onecol category: 'Columns: 1' default_region: content regions: content: label: Content -field_layout_twocol: +layout_twocol: label: 'Two column' path: layouts/twocol - template: field-layout--twocol - library: field_layout/drupal.field_layout.twocol + template: layout--twocol + library: field_layout/drupal.layout.twocol category: 'Columns: 2' default_region: left regions: diff --git a/core/modules/field_layout/field_layout.libraries.yml b/core/modules/field_layout/field_layout.libraries.yml index d87df5e..aafe7ee 100644 --- a/core/modules/field_layout/field_layout.libraries.yml +++ b/core/modules/field_layout/field_layout.libraries.yml @@ -1,4 +1,4 @@ -drupal.field_layout.twocol: +drupal.layout.twocol: version: VERSION css: layout: diff --git a/core/modules/field_layout/layouts/onecol/field-layout--onecol.html.twig b/core/modules/field_layout/layouts/onecol/layout--onecol.html.twig similarity index 80% rename from core/modules/field_layout/layouts/onecol/field-layout--onecol.html.twig rename to core/modules/field_layout/layouts/onecol/layout--onecol.html.twig index c6361c3..69fed03 100644 --- a/core/modules/field_layout/layouts/onecol/field-layout--onecol.html.twig +++ b/core/modules/field_layout/layouts/onecol/layout--onecol.html.twig @@ -12,12 +12,12 @@ #} {% set classes = [ -'field-layout--onecol', +'layout--onecol', ] %} {% if content %} -
+
{{ content }}
diff --git a/core/modules/field_layout/layouts/twocol/field-layout--twocol.html.twig b/core/modules/field_layout/layouts/twocol/layout--twocol.html.twig similarity index 73% rename from core/modules/field_layout/layouts/twocol/field-layout--twocol.html.twig rename to core/modules/field_layout/layouts/twocol/layout--twocol.html.twig index 735db5a..8e54d30 100644 --- a/core/modules/field_layout/layouts/twocol/field-layout--twocol.html.twig +++ b/core/modules/field_layout/layouts/twocol/layout--twocol.html.twig @@ -12,16 +12,16 @@ #} {% set classes = [ -'field-layout--twocol', +'layout--twocol', ] %} {% if content %} -
+
{{ content.left }}
-
+
{{ content.right }}
diff --git a/core/modules/field_layout/layouts/twocol/twocol.layout.css b/core/modules/field_layout/layouts/twocol/twocol.layout.css index 8e2f623..78a447d 100644 --- a/core/modules/field_layout/layouts/twocol/twocol.layout.css +++ b/core/modules/field_layout/layouts/twocol/twocol.layout.css @@ -1,14 +1,14 @@ -.field-layout--twocol { +.layout--twocol { display: flex; flex-wrap: wrap; justify-content: space-between; } -.field-layout--twocol > .field-layout-region { +.layout--twocol > .layout-region { flex: 0 1 50%; max-width: 50%; } -.field-layout--twocol > .field-layout-region--left { +.layout--twocol > .layout-region--left { max-width: calc(50% - 10px); margin-right: 10px; } diff --git a/core/modules/field_layout/src/Display/EntityDisplayWithLayoutInterface.php b/core/modules/field_layout/src/Display/EntityDisplayWithLayoutInterface.php index 54a2d52..3bee65e 100644 --- a/core/modules/field_layout/src/Display/EntityDisplayWithLayoutInterface.php +++ b/core/modules/field_layout/src/Display/EntityDisplayWithLayoutInterface.php @@ -64,4 +64,15 @@ public function setLayout(LayoutInterface $layout); */ public function getLayout(); + /** + * Ensures this entity has a layout. + * + * @param string $default_layout_id + * (optional) The layout ID to use as a default. Defaults to + * 'layout_onecol'. + * + * @return $this + */ + public function ensureLayout($default_layout_id = 'layout_onecol'); + } diff --git a/core/modules/field_layout/src/Entity/FieldLayoutEntityDisplayTrait.php b/core/modules/field_layout/src/Entity/FieldLayoutEntityDisplayTrait.php index 0b4e01c..8d89d21 100644 --- a/core/modules/field_layout/src/Entity/FieldLayoutEntityDisplayTrait.php +++ b/core/modules/field_layout/src/Entity/FieldLayoutEntityDisplayTrait.php @@ -77,31 +77,21 @@ protected function init() { * Overrides \Drupal\Core\Entity\EntityDisplayBase::preSave(). */ public function preSave(EntityStorageInterface $storage) { - if (!\Drupal::state()->get('field_layout_being_uninstalled')) { - $this->ensureLayout(); - } - parent::preSave($storage); // Ensure the plugin configuration is updated. Once layouts are no longer // stored as third party settings, this will be handled by the code in // \Drupal\Core\Config\Entity\ConfigEntityBase::preSave() that handles // \Drupal\Core\Entity\EntityWithPluginCollectionInterface. - if (!\Drupal::state()->get('field_layout_being_uninstalled')) { + if ($this->getLayoutId()) { $this->setLayout($this->getLayout()); } } /** - * Ensures this entity has a layout. - * - * @param string $default_layout_id - * (optional) The layout ID to use as a default. Defaults to - * 'field_layout_onecol'. - * - * @return $this + * {@inheritdoc} */ - protected function ensureLayout($default_layout_id = 'field_layout_onecol') { + public function ensureLayout($default_layout_id = 'layout_onecol') { if (!$this->getLayoutId()) { $this->setLayoutId($default_layout_id); } diff --git a/core/modules/field_layout/tests/src/Functional/FieldLayoutTest.php b/core/modules/field_layout/tests/src/Functional/FieldLayoutTest.php index e54fa5b..683ccd2 100644 --- a/core/modules/field_layout/tests/src/Functional/FieldLayoutTest.php +++ b/core/modules/field_layout/tests/src/Functional/FieldLayoutTest.php @@ -49,8 +49,8 @@ protected function setUp() { public function testNodeView() { // By default, the one-column layout is used. $this->drupalGet('node/1'); - $this->assertSession()->elementExists('css', '.field-layout--onecol'); - $this->assertSession()->elementExists('css', '.field-layout-region--content .field--name-body'); + $this->assertSession()->elementExists('css', '.layout--onecol'); + $this->assertSession()->elementExists('css', '.layout-region--content .field--name-body'); $this->drupalGet('admin/structure/types/manage/article/display'); $this->assertEquals(['Content', 'Disabled'], $this->getRegionTitles()); diff --git a/core/modules/field_layout/tests/src/FunctionalJavascript/FieldLayoutTest.php b/core/modules/field_layout/tests/src/FunctionalJavascript/FieldLayoutTest.php index 0953278..46ab6c2 100644 --- a/core/modules/field_layout/tests/src/FunctionalJavascript/FieldLayoutTest.php +++ b/core/modules/field_layout/tests/src/FunctionalJavascript/FieldLayoutTest.php @@ -47,9 +47,9 @@ protected function setUp() { public function testEntityViewModes() { // By default, the field is not visible. $this->drupalGet('entity_test/1/test'); - $this->assertSession()->elementNotExists('css', '.field-layout-region--content .field--name-field-test-text'); + $this->assertSession()->elementNotExists('css', '.layout-region--content .field--name-field-test-text'); $this->drupalGet('entity_test/1'); - $this->assertSession()->elementNotExists('css', '.field-layout-region--content .field--name-field-test-text'); + $this->assertSession()->elementNotExists('css', '.layout-region--content .field--name-field-test-text'); // Change the layout for the "test" view mode. See // core.entity_view_mode.entity_test.test.yml. @@ -65,9 +65,9 @@ public function testEntityViewModes() { // Each view mode has a different layout. $this->drupalGet('entity_test/1/test'); - $this->assertSession()->elementExists('css', '.field-layout-region--content .field--name-field-test-text'); + $this->assertSession()->elementExists('css', '.layout-region--content .field--name-field-test-text'); $this->drupalGet('entity_test/1'); - $this->assertSession()->elementNotExists('css', '.field-layout-region--content .field--name-field-test-text'); + $this->assertSession()->elementNotExists('css', '.layout-region--content .field--name-field-test-text'); } /** @@ -84,7 +84,7 @@ public function testEntityForm() { // Switch the layout to two columns. $this->click('#edit-field-layouts'); - $this->getSession()->getPage()->selectFieldOption('field_layout', 'field_layout_twocol'); + $this->getSession()->getPage()->selectFieldOption('field_layout', 'layout_twocol'); $this->assertSession()->assertWaitOnAjaxRequest(); $this->submitForm([], 'Save'); @@ -95,7 +95,7 @@ public function testEntityForm() { $this->drupalGet('entity_test/manage/1/edit'); // No fields are visible, and the regions don't display when empty. $this->assertFieldInRegion('field_test_text[0][value]', 'left'); - $this->assertSession()->elementExists('css', '.field-layout-region--left .field--name-field-test-text'); + $this->assertSession()->elementExists('css', '.layout-region--left .field--name-field-test-text'); // After a refresh the new regions are still there. $this->drupalGet('entity_test/structure/entity_test/form-display'); @@ -111,7 +111,7 @@ public function testEntityForm() { // The new layout is used. $this->drupalGet('entity_test/manage/1/edit'); - $this->assertSession()->elementExists('css', '.field-layout-region--right .field--name-field-test-text'); + $this->assertSession()->elementExists('css', '.layout-region--right .field--name-field-test-text'); $this->assertFieldInRegion('field_test_text[0][value]', 'right'); // Move the field to the right region without tabledrag. @@ -142,7 +142,7 @@ public function testEntityView() { // Switch the layout to two columns. $this->click('#edit-field-layouts'); - $this->getSession()->getPage()->selectFieldOption('field_layout', 'field_layout_twocol'); + $this->getSession()->getPage()->selectFieldOption('field_layout', 'layout_twocol'); $this->assertSession()->assertWaitOnAjaxRequest(); $this->submitForm([], 'Save'); @@ -151,8 +151,8 @@ public function testEntityView() { $this->drupalGet('entity_test/1'); // No fields are visible, and the regions don't display when empty. - $this->assertSession()->elementNotExists('css', '.field-layout--twocol'); - $this->assertSession()->elementNotExists('css', '.field-layout-region'); + $this->assertSession()->elementNotExists('css', '.layout--twocol'); + $this->assertSession()->elementNotExists('css', '.layout-region'); $this->assertSession()->elementNotExists('css', '.field--name-field-test-text'); // After a refresh the new regions are still there. @@ -171,8 +171,8 @@ public function testEntityView() { // The new layout is used. $this->drupalGet('entity_test/1'); - $this->assertSession()->elementExists('css', '.field-layout--twocol'); - $this->assertSession()->elementExists('css', '.field-layout-region--left .field--name-field-test-text'); + $this->assertSession()->elementExists('css', '.layout--twocol'); + $this->assertSession()->elementExists('css', '.layout-region--left .field--name-field-test-text'); // Move the field to the right region without tabledrag. $this->drupalGet('entity_test/structure/entity_test/display'); @@ -184,13 +184,13 @@ public function testEntityView() { // The updated region is used. $this->drupalGet('entity_test/1'); - $this->assertSession()->elementExists('css', '.field-layout-region--right .field--name-field-test-text'); + $this->assertSession()->elementExists('css', '.layout-region--right .field--name-field-test-text'); // The layout is still in use without Field UI. $this->container->get('module_installer')->uninstall(['field_ui']); $this->drupalGet('entity_test/1'); - $this->assertSession()->elementExists('css', '.field-layout--twocol'); - $this->assertSession()->elementExists('css', '.field-layout-region--right .field--name-field-test-text'); + $this->assertSession()->elementExists('css', '.layout--twocol'); + $this->assertSession()->elementExists('css', '.layout-region--right .field--name-field-test-text'); } /** diff --git a/core/modules/field_layout/tests/src/Kernel/FieldLayoutEntityDisplayTest.php b/core/modules/field_layout/tests/src/Kernel/FieldLayoutEntityDisplayTest.php index 41bb59d..09f1d79 100644 --- a/core/modules/field_layout/tests/src/Kernel/FieldLayoutEntityDisplayTest.php +++ b/core/modules/field_layout/tests/src/Kernel/FieldLayoutEntityDisplayTest.php @@ -40,7 +40,7 @@ public function testPreSave() { 'dependencies' => [], 'third_party_settings' => [ 'field_layout' => [ - 'id' => 'field_layout_onecol', + 'id' => 'layout_onecol', 'settings' => [], ], ], @@ -73,7 +73,7 @@ public function testPreSave() { ], 'third_party_settings' => [ 'field_layout' => [ - 'id' => 'field_layout_onecol', + 'id' => 'layout_onecol', 'settings' => [], ], 'entity_test' => [ diff --git a/core/modules/field_layout/tests/src/Unit/FieldLayoutBuilderTest.php b/core/modules/field_layout/tests/src/Unit/FieldLayoutBuilderTest.php index 40aa155..5d881bc 100644 --- a/core/modules/field_layout/tests/src/Unit/FieldLayoutBuilderTest.php +++ b/core/modules/field_layout/tests/src/Unit/FieldLayoutBuilderTest.php @@ -50,8 +50,8 @@ protected function setUp() { parent::setUp(); $this->pluginDefinition = new LayoutDefinition([ - 'library' => 'field_layout/drupal.field_layout.twocol', - 'theme_hook' => 'field_layout__twocol', + 'library' => 'field_layout/drupal.layout.twocol', + 'theme_hook' => 'layout__twocol', 'regions' => [ 'left' => [ 'label' => 'Left', @@ -120,10 +120,10 @@ public function testBuildView() { ], '#settings' => [], '#layout' => $this->pluginDefinition, - '#theme' => 'field_layout__twocol', + '#theme' => 'layout__twocol', '#attached' => [ 'library' => [ - 'field_layout/drupal.field_layout.twocol', + 'field_layout/drupal.layout.twocol', ], ], ], @@ -187,10 +187,10 @@ public function testBuildForm() { ], '#settings' => [], '#layout' => $this->pluginDefinition, - '#theme' => 'field_layout__twocol', + '#theme' => 'layout__twocol', '#attached' => [ 'library' => [ - 'field_layout/drupal.field_layout.twocol', + 'field_layout/drupal.layout.twocol', ], ], ],