You are here

protected function EntityViewTrait::buildEntityView in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/EntityViewTrait.php \Drupal\Tests\EntityViewTrait::buildEntityView()
  2. 10 core/tests/Drupal/Tests/EntityViewTrait.php \Drupal\Tests\EntityViewTrait::buildEntityView()

Builds the renderable view of an entity.

Entities postpone the composition of their renderable arrays to #pre_render functions in order to maximize cache efficacy. This means that the full renderable array for an entity is constructed in drupal_render(). Some tests require the complete renderable array for an entity outside of the drupal_render process in order to verify the presence of specific values. This method isolates the steps in the render process that produce an entity's renderable array.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity to prepare a renderable array for.

string $view_mode: (optional) The view mode that should be used to build the entity.

null $langcode: (optional) For which language the entity should be prepared, defaults to the current content language.

bool $reset: (optional) Whether to clear the cache for this entity.

Return value

array

See also

\Drupal\Core\Render\RendererInterface::render()

4 calls to EntityViewTrait::buildEntityView()
CommentOrphanTest::testOrphan in core/modules/comment/tests/src/Kernel/CommentOrphanTest.php
Test loading/deleting/rendering orphaned comments.
ImageFieldDefaultImagesTest::testDefaultImages in core/modules/image/tests/src/Functional/ImageFieldDefaultImagesTest.php
Tests CRUD for fields and field storages with default images.
NodeEntityViewModeAlterTest::testNodeViewModeChange in core/modules/node/tests/src/Functional/NodeEntityViewModeAlterTest.php
Create a "Basic page" node and verify its consistency in the database.
SummaryLengthTest::testSummaryLength in core/modules/node/tests/src/Kernel/SummaryLengthTest.php
Tests the node summary length functionality.

File

core/tests/Drupal/Tests/EntityViewTrait.php, line 38

Class

EntityViewTrait
Provides helper methods to deal with building entity views in tests.

Namespace

Drupal\Tests

Code

protected function buildEntityView(EntityInterface $entity, $view_mode = 'full', $langcode = NULL, $reset = FALSE) {
  $ensure_fully_built = function (&$elements) use (&$ensure_fully_built) {

    // If the default values for this element have not been loaded yet, populate
    // them.
    if (isset($elements['#type']) && empty($elements['#defaults_loaded'])) {
      $elements += \Drupal::service('element_info')
        ->getInfo($elements['#type']);
    }

    // Make any final changes to the element before it is rendered. This means
    // that the $element or the children can be altered or corrected before the
    // element is rendered into the final text.
    if (isset($elements['#pre_render'])) {
      foreach ($elements['#pre_render'] as $callable) {
        $elements = call_user_func($callable, $elements);
      }
    }

    // And recurse.
    $children = Element::children($elements, TRUE);
    foreach ($children as $key) {
      $ensure_fully_built($elements[$key]);
    }
  };
  $render_controller = $this->container
    ->get('entity_type.manager')
    ->getViewBuilder($entity
    ->getEntityTypeId());
  if ($reset) {
    $render_controller
      ->resetCache([
      $entity
        ->id(),
    ]);
  }
  $build = $render_controller
    ->view($entity, $view_mode, $langcode);
  $ensure_fully_built($build);
  return $build;
}