You are here

public static function EntityFormDisplay::collectRenderDisplay in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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, entity_get_form_display() 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.

Return value

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

See also

entity_get_form_display()

hook_entity_form_display_alter()

3 calls to EntityFormDisplay::collectRenderDisplay()
ContentEntityForm::init in core/lib/Drupal/Core/Entity/ContentEntityForm.php
Initializes the form state and the entity before the first form build.
NestedEntityTestForm::buildForm in core/modules/field/tests/modules/field_test/src/Form/NestedEntityTestForm.php
{@inheritdoc]
QuickEditFieldForm::init in core/modules/quickedit/src/Form/QuickEditFieldForm.php
Initialize the form state and the entity before the first form build.

File

core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php, line 77
Contains \Drupal\Core\Entity\Entity\EntityFormDisplay.

Class

EntityFormDisplay
Configuration entity that contains widget options for all components of a entity form in a given form mode.

Namespace

Drupal\Core\Entity\Entity

Code

public static function collectRenderDisplay(FieldableEntityInterface $entity, $form_mode) {
  $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;
    }
  }

  // Else create a fresh runtime object.
  if (empty($display)) {
    $display = $storage
      ->create(array(
      'targetEntityType' => $entity_type,
      'bundle' => $bundle,
      'mode' => $form_mode,
      'status' => TRUE,
    ));
  }

  // Let the display know which form mode was originally requested.
  $display->originalMode = $form_mode;

  // Let modules alter the display.
  $display_context = array(
    'entity_type' => $entity_type,
    'bundle' => $bundle,
    'form_mode' => $form_mode,
  );
  \Drupal::moduleHandler()
    ->alter('entity_form_display', $display, $display_context);
  return $display;
}