3 namespace Drupal\Tests\layout_builder\Kernel;
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage;
8 use Drupal\layout_builder\Section;
11 * Ensures that Layout Builder and core EntityViewDisplays are compatible.
13 * @group layout_builder
15 class LayoutBuilderInstallTest extends LayoutBuilderCompatibilityTestBase {
18 * Tests the compatibility of Layout Builder with existing entity displays.
20 public function testCompatibility() {
21 // Ensure that the fields are shown.
23 'field field--name-name field--type-string field--label-hidden field__item',
24 'field field--name-test-field-display-configurable field--type-boolean field--label-above',
25 'clearfix text-formatted field field--name-test-display-configurable field--type-text field--label-above',
26 'clearfix text-formatted field field--name-test-display-non-configurable field--type-text field--label-above',
27 'clearfix text-formatted field field--name-test-display-multiple field--type-text field--label-above',
29 $this->assertFieldAttributes($this->entity, $expected_fields);
31 $this->installLayoutBuilder();
33 // Without using Layout Builder for an override, the result has not changed.
34 $this->assertFieldAttributes($this->entity, $expected_fields);
36 // Add a layout override.
37 $this->enableOverrides();
38 $this->entity = $this->reloadEntity($this->entity);
39 $this->entity->get(OverridesSectionStorage::FIELD_NAME)->appendSection(new Section('layout_onecol'));
40 $this->entity->save();
42 // The rendered entity has now changed. The non-configurable field is shown
43 // outside the layout, the configurable field is not shown at all, and the
44 // layout itself is rendered (but empty).
45 $new_expected_fields = [
46 'field field--name-name field--type-string field--label-hidden field__item',
47 'clearfix text-formatted field field--name-test-display-non-configurable field--type-text field--label-above',
48 'clearfix text-formatted field field--name-test-display-multiple field--type-text field--label-above',
50 $this->assertFieldAttributes($this->entity, $new_expected_fields);
51 $this->assertNotEmpty($this->cssSelect('.layout--onecol'));
53 // Removing the layout restores the original rendering of the entity.
54 $this->entity->get(OverridesSectionStorage::FIELD_NAME)->removeSection(0);
55 $this->entity->save();
56 $this->assertFieldAttributes($this->entity, $expected_fields);
58 // Test that adding a new field after Layout Builder has been installed will
59 // add the new field to the default region of the first section.
60 $field_storage = FieldStorageConfig::create([
61 'entity_type' => 'entity_test_base_field_display',
62 'field_name' => 'test_field_display_post_install',
65 $field_storage->save();
67 'field_storage' => $field_storage,
68 'bundle' => 'entity_test_base_field_display',
69 'label' => 'FieldConfig with configurable display',
72 $this->entity = $this->reloadEntity($this->entity);
73 $this->entity->test_field_display_post_install = 'Test string';
74 $this->entity->save();
76 $this->display = $this->reloadEntity($this->display);
78 ->setComponent('test_field_display_post_install', ['weight' => 50])
80 $new_expected_fields = [
81 'field field--name-name field--type-string field--label-hidden field__item',
82 'field field--name-test-field-display-configurable field--type-boolean field--label-above',
83 'clearfix text-formatted field field--name-test-display-configurable field--type-text field--label-above',
84 'clearfix text-formatted field field--name-test-field-display-post-install field--type-text field--label-above',
85 'clearfix text-formatted field field--name-test-display-non-configurable field--type-text field--label-above',
86 'clearfix text-formatted field field--name-test-display-multiple field--type-text field--label-above',
88 $this->assertFieldAttributes($this->entity, $new_expected_fields);
89 $this->assertNotEmpty($this->cssSelect('.layout--onecol'));
90 $this->assertText('Test string');