diff --git a/core/modules/layout_builder/layout_builder.module b/core/modules/layout_builder/layout_builder.module index f9ef0855daeb0459a6771e9bdbd30c8ad7b0521c..38d02219b974e1a563e8b01990ff2886d601d6d7 100644 --- a/core/modules/layout_builder/layout_builder.module +++ b/core/modules/layout_builder/layout_builder.module @@ -7,6 +7,7 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Render\Element; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Url; use Drupal\field\FieldConfigInterface; @@ -14,7 +15,6 @@ use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplayStorage; use Drupal\layout_builder\Form\LayoutBuilderEntityViewDisplayForm; use Drupal\Core\Entity\Display\EntityViewDisplayInterface; -use Drupal\layout_builder\Entity\LayoutEntityDisplayInterface; use Drupal\layout_builder\Plugin\Block\ExtraFieldBlock; use Drupal\layout_builder\InlineBlockEntityOperations; use Drupal\Core\Session\AccountInterface; @@ -106,7 +106,9 @@ function layout_builder_field_config_delete(FieldConfigInterface $field_config) * @see layout_builder_module_implements_alter() */ function layout_builder_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) { - if ($display instanceof LayoutEntityDisplayInterface) { + // Only replace extra fields when Layout Builder has been used to alter the + // build. See \Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay::buildMultiple(). + if (isset($build['_layout_builder']) && !Element::isEmpty($build['_layout_builder'])) { /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager */ $field_manager = \Drupal::service('entity_field.manager'); $extra_fields = $field_manager->getExtraFields($entity->getEntityTypeId(), $entity->bundle()); diff --git a/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php index 277819e093aa98bd3d25f68f5f2c9a077ac25888..ee15bdfc767d485d19df25356667decefd507815 100644 --- a/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php +++ b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php @@ -530,6 +530,23 @@ public function testLayoutBuilderChooseBlocksAlter() { $assert_session->linkExists('Changed'); } + /** + * Tests that extra fields work before and after enabling Layout Builder. + */ + public function testExtraFields() { + $assert_session = $this->assertSession(); + + $this->drupalLogin($this->drupalCreateUser(['administer node display'])); + + $this->drupalGet('node'); + $assert_session->linkExists('Read more'); + + $this->drupalPostForm('admin/structure/types/manage/bundle_with_section_field/display/default', ['layout[enabled]' => TRUE], 'Save'); + + $this->drupalGet('node'); + $assert_session->linkExists('Read more'); + } + /** * Tests that deleting a View block used in Layout Builder works. */