diff --git a/core/modules/field_layout/field_layout.module b/core/modules/field_layout/field_layout.module index 503c622..7b0ca6d 100644 --- a/core/modules/field_layout/field_layout.module +++ b/core/modules/field_layout/field_layout.module @@ -45,10 +45,26 @@ function field_layout_entity_type_alter(array &$entity_types) { } /** - * Implements hook_entity_presave(). + * Implements hook_ENTITY_TYPE_presave() for entity_form_display entities. */ -function field_layout_entity_presave(EntityInterface $entity) { - // Whenever creating a new entity display, set the layout to 'one column'. +function field_layout_entity_form_display_presave(EntityInterface $entity) { + _field_layout_entity_display_presave($entity); +} + +/** + * Implements hook_ENTITY_TYPE_presave() for entity_view_display entities. + */ +function field_layout_entity_view_display_presave(EntityInterface $entity) { + _field_layout_entity_display_presave($entity); +} + +/** + * Ensure there is a layout set on a display entity. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity object. + */ +function _field_layout_entity_display_presave(EntityInterface $entity) { if ($entity instanceof EntityDisplayWithLayoutInterface && !$entity->getLayoutId()) { $entity->setLayoutId('onecol'); } @@ -59,7 +75,7 @@ function field_layout_entity_presave(EntityInterface $entity) { */ function field_layout_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) { if ($display instanceof EntityDisplayWithLayoutInterface) { - _field_layout_apply_layout($build, $display); + _field_layout_apply_layout($build, $display, 'view'); } } @@ -70,7 +86,7 @@ function field_layout_form_alter(&$form, FormStateInterface $form_state, $form_i $form_object = $form_state->getFormObject(); if ($form_object instanceof ContentEntityFormInterface && $display = $form_object->getFormDisplay($form_state)) { if ($display instanceof EntityDisplayWithLayoutInterface) { - _field_layout_apply_layout($form, $display, TRUE); + _field_layout_apply_layout($form, $display, 'form'); } } } @@ -83,11 +99,12 @@ function field_layout_form_alter(&$form, FormStateInterface $form_state, $form_i * @param \Drupal\field_layout\Display\EntityDisplayWithLayoutInterface $display * The entity display holding the display options configured for the * entity components. - * @param bool $in_form_context - * (optional) If in a form context, an alternate method will be used to render - * fields in their regions. Defaults to FALSE. + * @param string $display_context + * The display context, either 'form' or 'view'. If in a 'form' context, an + * alternate method will be used to render fields in their regions. */ -function _field_layout_apply_layout(array &$build, EntityDisplayWithLayoutInterface $display, $in_form_context = FALSE) { +function _field_layout_apply_layout(array &$build, EntityDisplayWithLayoutInterface $display, $display_context) { + $in_form_context = $display_context === 'form'; if (!$layout_definition = \Drupal::service('field_layout.layout_repository')->getLayout($display->getLayoutId())) { return; } @@ -114,7 +131,7 @@ function _field_layout_apply_layout(array &$build, EntityDisplayWithLayoutInterf // Move the field from the top-level of $build into a region-specific section. foreach ($display->getComponents() as $name => $field) { // If the component is a true field, but not configurable, do not alter it. - if (isset($field_definitions[$name]) && !$field_definitions[$name]->isDisplayConfigurable($display->get('displayContext'))) { + if (isset($field_definitions[$name]) && !$field_definitions[$name]->isDisplayConfigurable($display_context)) { continue; }