function EntityViewBuilder::getRenderRecursionKey

Generates a key for an entity render array for recursion protection.

Parameters

array $build: The entity render array.

Return value

string The key to ID the build array within recursion tracking.

2 calls to EntityViewBuilder::getRenderRecursionKey()
EntityViewBuilder::setRecursiveRenderProtection in core/lib/Drupal/Core/Entity/EntityViewBuilder.php
Entity render array #pre_render callback.
EntityViewBuilder::unsetRecursiveRenderProtection in core/lib/Drupal/Core/Entity/EntityViewBuilder.php
Entity render array #post_render callback.

File

core/lib/Drupal/Core/Entity/EntityViewBuilder.php, line 594

Class

EntityViewBuilder
Base class for entity view builders.

Namespace

Drupal\Core\Entity

Code

protected function getRenderRecursionKey(array $build) : string {
  /** @var \Drupal\Core\Entity\EntityInterface $entity */
  $entity = $build['#' . $this->entityTypeId];
  $recursion_keys = [
    $entity->getEntityTypeId(),
  ];
  // If entity is new and has no ID, generate unique ID from entity object.
  // This is to prevent false positives, for example when previewing a new
  // node that is referencing a new node without either node yet being saved.
  if ($entity->id()) {
    $recursion_keys[] = 'entity_id';
    $recursion_keys[] = $entity->id();
    if ($entity instanceof RevisionableInterface) {
      $recursion_keys[] = $entity->getRevisionId();
    }
  }
  else {
    $recursion_keys[] = 'object_id';
    $recursion_keys[] = spl_object_id($entity);
  }
  if ($entity instanceof TranslatableDataInterface) {
    $recursion_keys[] = $entity->language()
      ->getId();
  }
  $recursion_keys[] = $build['#view_mode'];
  // It seems very unlikely that the same entity displayed in the same view
  // mode would be recursively nested and meant to be displayed differently.
  return implode(':', $recursion_keys);
}

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