public static function EntityFormDisplay::collectRenderDisplay

public static EntityFormDisplay::collectRenderDisplay(FieldableEntityInterface $entity, $form_mode)

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()

File

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

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;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!Entity!EntityFormDisplay.php/function/EntityFormDisplay::collectRenderDisplay/8.1.x