function EntityFormDisplay::collectRenderDisplay

Same name in other branches
  1. 9 core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php \Drupal\Core\Entity\Entity\EntityFormDisplay::collectRenderDisplay()
  2. 8.9.x core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php \Drupal\Core\Entity\Entity\EntityFormDisplay::collectRenderDisplay()
  3. 10 core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php \Drupal\Core\Entity\Entity\EntityFormDisplay::collectRenderDisplay()

Returns the entity_form_display object used to build an entity form.

Depending on the configuration of the form mode for the entity bundle, this can be either the display object associated with the form mode, or the 'default' display.

This method should only be used internally when rendering an entity form. When assigning suggested display options for a component in a given form mode, EntityDisplayRepositoryInterface::getFormDisplay() should be used instead, in order to avoid inadvertently modifying the output of other form modes that might happen to use the 'default' display too. Those options will then be effectively applied only if the form mode is configured to use them.

hook_entity_form_display_alter() is invoked on each display, allowing 3rd party code to alter the display options held in the display before they are used to generate render arrays.

Parameters

\Drupal\Core\Entity\FieldableEntityInterface $entity: The entity for which the form is being built.

string $form_mode: The form mode.

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 value

\Drupal\Core\Entity\Display\EntityFormDisplayInterface The display object that should be used to build the entity form.

See also

\Drupal\Core\Entity\EntityDisplayRepositoryInterface::getFormDisplay()

hook_entity_form_display_alter()

9 calls to EntityFormDisplay::collectRenderDisplay()
AddFormBase::buildEntityFormElement in core/modules/media_library/src/Form/AddFormBase.php
Builds the sub-form for setting required fields on a new media item.
AddFormBase::validateMediaEntity in core/modules/media_library/src/Form/AddFormBase.php
Validate a created media item.
ContentEntityForm::init in core/lib/Drupal/Core/Entity/ContentEntityForm.php
Initializes the form state and the entity before the first form build.
EmbeddedForm::buildForm in core/modules/field_layout/tests/modules/field_layout_test/src/Form/EmbeddedForm.php
Form constructor.
InlineBlock::blockSubmit in core/modules/layout_builder/src/Plugin/Block/InlineBlock.php

... See full list

File

core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php, line 85

Class

EntityFormDisplay
Configuration entity.

Namespace

Drupal\Core\Entity\Entity

Code

public static function collectRenderDisplay(FieldableEntityInterface $entity, $form_mode, $default_fallback = TRUE) {
    $entity_type = $entity->getEntityTypeId();
    $bundle = $entity->bundle();
    // Allow modules to change the form mode.
    \Drupal::moduleHandler()->alter([
        $entity_type . '_form_mode',
        'entity_form_mode',
    ], $form_mode, $entity);
    // 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;
    }
    if ($default_fallback) {
        $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::entityTypeManager()->getStorage('entity_form_display');
    foreach ($candidate_ids as $candidate_id) {
        if (isset($results[$candidate_id])) {
            $display = $storage->load($candidate_id);
            break;
        }
    }
    // Else create a fresh runtime object.
    if (empty($display)) {
        $display = $storage->create([
            'targetEntityType' => $entity_type,
            'bundle' => $bundle,
            'mode' => $default_fallback ? $form_mode : static::CUSTOM_MODE,
            'status' => TRUE,
        ]);
    }
    // Let the display know which form mode was originally requested.
    $display->originalMode = $form_mode;
    // Let modules alter the display.
    $display_context = [
        'entity_type' => $entity_type,
        'bundle' => $bundle,
        'form_mode' => $form_mode,
    ];
    \Drupal::moduleHandler()->alter('entity_form_display', $display, $display_context);
    return $display;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.