diff --git a/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php b/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php index 183e428ba3..0139bda8a7 100644 --- a/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php +++ b/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php @@ -65,6 +65,9 @@ class EntityFormDisplay extends EntityDisplayBase implements EntityFormDisplayIn * The entity for which the form is being built. * @param string $form_mode * The form mode. + * @param bool $transient + * (optional) If the render display should be instantiated without + * attempting to load any display from configuration. Defaults to FALSE. * * @return \Drupal\Core\Entity\Display\EntityFormDisplayInterface * The display object that should be used to build the entity form. @@ -72,28 +75,30 @@ 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, $transient = FALSE) { $entity_type = $entity->getEntityTypeId(); $bundle = $entity->bundle(); - - // Check the existence and status of: - // - the display for the form mode, - // - the 'default' display. - if ($form_mode != 'default') { - $candidate_ids[] = $entity_type . '.' . $bundle . '.' . $form_mode; - } - $candidate_ids[] = $entity_type . '.' . $bundle . '.default'; - $results = \Drupal::entityQuery('entity_form_display') - ->condition('id', $candidate_ids) - ->condition('status', TRUE) - ->execute(); - - // Load the first valid candidate display, if any. $storage = \Drupal::entityManager()->getStorage('entity_form_display'); - foreach ($candidate_ids as $candidate_id) { - if (isset($results[$candidate_id])) { - $display = $storage->load($candidate_id); - break; + + if (!$transient) { + // Check the existence and status of: + // - the display for the form mode, + // - the 'default' display. + if ($form_mode != 'default') { + $candidate_ids[] = $entity_type . '.' . $bundle . '.' . $form_mode; + } + $candidate_ids[] = $entity_type . '.' . $bundle . '.default'; + $results = \Drupal::entityQuery('entity_form_display') + ->condition('id', $candidate_ids) + ->condition('status', TRUE) + ->execute(); + + // Load the first valid candidate display, if any. + foreach ($candidate_ids as $candidate_id) { + if (isset($results[$candidate_id])) { + $display = $storage->load($candidate_id); + break; + } } } // Else create a fresh runtime object. @@ -103,6 +108,7 @@ public static function collectRenderDisplay(FieldableEntityInterface $entity, $f 'bundle' => $bundle, 'mode' => $form_mode, 'status' => TRUE, + 'skipDisplayInitialization' => $transient, ]); } diff --git a/core/lib/Drupal/Core/Entity/EntityDisplayBase.php b/core/lib/Drupal/Core/Entity/EntityDisplayBase.php index e696a982fe..b97b30d990 100644 --- a/core/lib/Drupal/Core/Entity/EntityDisplayBase.php +++ b/core/lib/Drupal/Core/Entity/EntityDisplayBase.php @@ -56,6 +56,13 @@ */ protected $mode = self::CUSTOM_MODE; + /** + * Whether the display should be initialized or not. + * + * @var bool + */ + protected $skipDisplayInitialization = FALSE; + /** * Whether this display is enabled or not. If the entity (form) display * is disabled, we'll fall back to the 'default' display. @@ -153,7 +160,7 @@ public function __construct(array $values, $entity_type) { */ protected function init() { // Only populate defaults for "official" view modes and form modes. - if ($this->mode !== static::CUSTOM_MODE) { + if ($this->mode !== static::CUSTOM_MODE && !$this->skipDisplayInitialization) { $default_region = $this->getDefaultRegion(); // Fill in defaults for extra fields. $context = $this->displayContext == 'view' ? 'display' : $this->displayContext; 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..4de7c4affd 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(), TRUE); + $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 5b41c3fcfc..fc01019cd8 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');