diff --git a/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php b/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php index 183e428ba3..c282427028 100644 --- a/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php +++ b/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php @@ -65,6 +65,10 @@ class EntityFormDisplay extends EntityDisplayBase implements EntityFormDisplayIn * The entity for which the form is being built. * @param string $form_mode * The form mode. + * @param bool $default_fallback + * (optional) Whether the default display should be used to initialize the + * form display in case the specified display does not exist. Defaults to + * TRUE. * * @return \Drupal\Core\Entity\Display\EntityFormDisplayInterface * The display object that should be used to build the entity form. @@ -72,7 +76,7 @@ class EntityFormDisplay extends EntityDisplayBase implements EntityFormDisplayIn * @see entity_get_form_display() * @see hook_entity_form_display_alter() */ - public static function collectRenderDisplay(FieldableEntityInterface $entity, $form_mode) { + public static function collectRenderDisplay(FieldableEntityInterface $entity, $form_mode, $default_fallback = TRUE) { $entity_type = $entity->getEntityTypeId(); $bundle = $entity->bundle(); @@ -82,7 +86,9 @@ public static function collectRenderDisplay(FieldableEntityInterface $entity, $f if ($form_mode != 'default') { $candidate_ids[] = $entity_type . '.' . $bundle . '.' . $form_mode; } - $candidate_ids[] = $entity_type . '.' . $bundle . '.default'; + if ($default_fallback) { + $candidate_ids[] = $entity_type . '.' . $bundle . '.default'; + } $results = \Drupal::entityQuery('entity_form_display') ->condition('id', $candidate_ids) ->condition('status', TRUE) @@ -101,7 +107,7 @@ public static function collectRenderDisplay(FieldableEntityInterface $entity, $f $display = $storage->create([ 'targetEntityType' => $entity_type, 'bundle' => $bundle, - 'mode' => $form_mode, + 'mode' => $default_fallback ? $form_mode : static::CUSTOM_MODE, 'status' => TRUE, ]); } diff --git a/core/modules/content_moderation/content_moderation.module b/core/modules/content_moderation/content_moderation.module index 846575382b..18dae2575d 100644 --- a/core/modules/content_moderation/content_moderation.module +++ b/core/modules/content_moderation/content_moderation.module @@ -193,14 +193,16 @@ function content_moderation_entity_view(array &$build, EntityInterface $entity, } /** - * Implements hook_layout_builder_overrides_entity_form_display_alter(). + * Implements hook_entity_form_display_alter(). */ -function content_moderation_layout_builder_overrides_entity_form_display_alter(EntityFormDisplayInterface $display) { - $display->setComponent('moderation_state', [ - 'type' => 'moderation_state_default', - 'weight' => -900, - 'settings' => [], - ]); +function content_moderation_entity_form_display_alter(EntityFormDisplayInterface $form_display, array $context) { + if ($context['form_mode'] === 'layout_builder') { + $form_display->setComponent('moderation_state', [ + 'type' => 'moderation_state_default', + 'weight' => -900, + 'settings' => [], + ]); + } } /** diff --git a/core/modules/layout_builder/layout_builder.api.php b/core/modules/layout_builder/layout_builder.api.php deleted file mode 100644 index 2e9d5f1588..0000000000 --- a/core/modules/layout_builder/layout_builder.api.php +++ /dev/null @@ -1,32 +0,0 @@ -setComponent('moderation_state', [ - 'type' => 'moderation_state_default', - 'weight' => 2, - 'settings' => [], - ]); -} - -/** - * @} End of "addtogroup hooks". - */ diff --git a/core/modules/layout_builder/src/Form/OverridesEntityForm.php b/core/modules/layout_builder/src/Form/OverridesEntityForm.php index 6373e1062b..552fa5910e 100644 --- a/core/modules/layout_builder/src/Form/OverridesEntityForm.php +++ b/core/modules/layout_builder/src/Form/OverridesEntityForm.php @@ -76,24 +76,14 @@ public function getBaseFormId() { protected function init(FormStateInterface $form_state) { parent::init($form_state); - // Create a transient display that is not persisted, but used only for - // building the components required for the layout form. - $display = EntityFormDisplay::create([ - 'targetEntityType' => $this->getEntity()->getEntityTypeId(), - 'bundle' => $this->getEntity()->bundle(), - ]); - - // Allow modules to choose if they are relevant to the layout form. - $this->moduleHandler->alter('layout_builder_overrides_entity_form_display', $display); - - // Add the widget for Layout Builder after the alter. - $display->setComponent(OverridesSectionStorage::FIELD_NAME, [ + $form_display = EntityFormDisplay::collectRenderDisplay($this->entity, $this->getOperation(), FALSE); + $form_display->setComponent(OverridesSectionStorage::FIELD_NAME, [ 'type' => 'layout_builder_widget', 'weight' => -10, 'settings' => [], ]); - $this->setFormDisplay($display, $form_state); + $this->setFormDisplay($form_display, $form_state); } /** diff --git a/core/modules/layout_builder/tests/modules/layout_builder_test/layout_builder_test.module b/core/modules/layout_builder/tests/modules/layout_builder_test/layout_builder_test.module index 9b26f57d6a..bccf0236bb 100644 --- a/core/modules/layout_builder/tests/modules/layout_builder_test/layout_builder_test.module +++ b/core/modules/layout_builder/tests/modules/layout_builder_test/layout_builder_test.module @@ -83,13 +83,15 @@ function layout_builder_test_form_layout_builder_configure_block_alter(&$form, F } /** - * Implements hook_layout_builder_overrides_entity_form_display_alter(). + * Implements hook_entity_form_display_alter(). */ -function layout_builder_test_layout_builder_overrides_entity_form_display_alter(EntityFormDisplayInterface $display) { - $display->setComponent('status', [ - 'type' => 'boolean_checkbox', - 'settings' => [ - 'display_label' => TRUE, - ], - ]); +function layout_builder_entity_form_display_alter(EntityFormDisplayInterface $form_display, array $context) { + if ($context['form_mode'] === 'layout_builder') { + $form_display->setComponent('status', [ + 'type' => 'boolean_checkbox', + 'settings' => [ + 'display_label' => TRUE, + ], + ]); + } } diff --git a/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php index 7ce0e80ae7..4c11a0153f 100644 --- a/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php +++ b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php @@ -85,6 +85,9 @@ public function testOverrides() { // Add a block with a custom label. $this->drupalGet('node/1'); $page->clickLink('Layout'); + // The layout form should not contain fields for the title of the node by + // default. + $assert_session->fieldNotExists('title[0][value]'); $page->clickLink('Add Block'); $page->clickLink('Powered by Drupal'); $page->fillField('settings[label]', 'This is an override');