3 namespace Drupal\Tests\layout_builder\Kernel;
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\layout_builder\Section;
10 * Ensures that Layout Builder and core EntityViewDisplays are compatible.
12 * @group layout_builder
14 class LayoutBuilderInstallTest extends LayoutBuilderCompatibilityTestBase {
17 * Tests the compatibility of Layout Builder with existing entity displays.
19 public function testCompatibility() {
20 // Ensure that the fields are shown.
22 'field field--name-name field--type-string field--label-hidden field__item',
23 'field field--name-test-field-display-configurable field--type-boolean field--label-above',
24 'clearfix text-formatted field field--name-test-display-configurable field--type-text field--label-above',
25 'clearfix text-formatted field field--name-test-display-non-configurable field--type-text field--label-above',
26 'clearfix text-formatted field field--name-test-display-multiple field--type-text field--label-above',
28 $this->assertFieldAttributes($this->entity, $expected_fields);
30 $this->installLayoutBuilder();
32 // Without using Layout Builder for an override, the result has not changed.
33 $this->assertFieldAttributes($this->entity, $expected_fields);
35 // Add a layout override.
36 $this->enableOverrides();
37 $this->entity = $this->reloadEntity($this->entity);
38 $this->entity->get('layout_builder__layout')->appendSection(new Section('layout_onecol'));
39 $this->entity->save();
41 // The rendered entity has now changed. The non-configurable field is shown
42 // outside the layout, the configurable field is not shown at all, and the
43 // layout itself is rendered (but empty).
44 $new_expected_fields = [
45 'field field--name-name field--type-string field--label-hidden field__item',
46 'clearfix text-formatted field field--name-test-display-non-configurable field--type-text field--label-above',
47 'clearfix text-formatted field field--name-test-display-multiple field--type-text field--label-above',
49 $this->assertFieldAttributes($this->entity, $new_expected_fields);
50 $this->assertNotEmpty($this->cssSelect('.layout--onecol'));
52 // Removing the layout restores the original rendering of the entity.
53 $this->entity->get('layout_builder__layout')->removeSection(0);
54 $this->entity->save();
55 $this->assertFieldAttributes($this->entity, $expected_fields);
57 // Test that adding a new field after Layout Builder has been installed will
58 // add the new field to the default region of the first section.
59 $field_storage = FieldStorageConfig::create([
60 'entity_type' => 'entity_test_base_field_display',
61 'field_name' => 'test_field_display_post_install',
64 $field_storage->save();
66 'field_storage' => $field_storage,
67 'bundle' => 'entity_test_base_field_display',
68 'label' => 'FieldConfig with configurable display',
71 $this->entity = $this->reloadEntity($this->entity);
72 $this->entity->test_field_display_post_install = 'Test string';
73 $this->entity->save();
75 $this->display = $this->reloadEntity($this->display);
77 ->setComponent('test_field_display_post_install', ['weight' => 50])
79 $new_expected_fields = [
80 'field field--name-name field--type-string field--label-hidden field__item',
81 'field field--name-test-field-display-configurable field--type-boolean field--label-above',
82 'clearfix text-formatted field field--name-test-display-configurable field--type-text field--label-above',
83 'clearfix text-formatted field field--name-test-field-display-post-install field--type-text field--label-above',
84 'clearfix text-formatted field field--name-test-display-non-configurable field--type-text field--label-above',
85 'clearfix text-formatted field field--name-test-display-multiple field--type-text field--label-above',
87 $this->assertFieldAttributes($this->entity, $new_expected_fields);
88 $this->assertNotEmpty($this->cssSelect('.layout--onecol'));
89 $this->assertText('Test string');