Issue #3038442 by Sam152, amateescu, tim.plunkett: Use hook_entity_form_display_alter() instead of Layout Builder's custom overrides display altering hook
parent
3ab46e219d
commit
8d1a7d7c86
|
@ -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 @@ class EntityFormDisplay extends EntityDisplayBase implements EntityFormDisplayIn
|
|||
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 @@ class EntityFormDisplay extends EntityDisplayBase implements EntityFormDisplayIn
|
|||
$display = $storage->create([
|
||||
'targetEntityType' => $entity_type,
|
||||
'bundle' => $bundle,
|
||||
'mode' => $form_mode,
|
||||
'mode' => $default_fallback ? $form_mode : static::CUSTOM_MODE,
|
||||
'status' => TRUE,
|
||||
]);
|
||||
}
|
||||
|
|
|
@ -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' => [],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,32 +0,0 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Hooks provided by the Layout Builder module.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup hooks
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* Allows customization of the Layout Builder UI for per-entity overrides.
|
||||
*
|
||||
* The Layout Builder widget will be added with a weight of -10 after this hook
|
||||
* is invoked.
|
||||
*
|
||||
* @see hook_entity_form_display_alter()
|
||||
* @see \Drupal\layout_builder\Form\OverridesEntityForm::init()
|
||||
*/
|
||||
function hook_layout_builder_overrides_entity_form_display_alter(\Drupal\Core\Entity\Display\EntityFormDisplayInterface $display) {
|
||||
$display->setComponent('moderation_state', [
|
||||
'type' => 'moderation_state_default',
|
||||
'weight' => 2,
|
||||
'settings' => [],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @} End of "addtogroup hooks".
|
||||
*/
|
|
@ -76,24 +76,14 @@ class OverridesEntityForm extends ContentEntityForm {
|
|||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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,
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -85,6 +85,9 @@ class LayoutBuilderTest extends BrowserTestBase {
|
|||
// 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');
|
||||
|
|
Loading…
Reference in New Issue