You are here

public function EntityViewBuilder::build in Twig Tweak 3.1.x

Same name and namespace in other branches
  1. 3.x src/View/EntityViewBuilder.php \Drupal\twig_tweak\View\EntityViewBuilder::build()

Builds a render array for a given entity.

File

src/View/EntityViewBuilder.php, line 32

Class

EntityViewBuilder
Entity view builder.

Namespace

Drupal\twig_tweak\View

Code

public function build(EntityInterface $entity, string $view_mode = 'full', string $langcode = NULL, bool $check_access = TRUE) : array {
  $build = [];
  $access = $check_access ? $entity
    ->access('view', NULL, TRUE) : AccessResult::allowed();
  if ($access
    ->isAllowed()) {
    $build = $this->entityTypeManager
      ->getViewBuilder($entity
      ->getEntityTypeId())
      ->view($entity, $view_mode, $langcode);
  }
  CacheableMetadata::createFromRenderArray($build)
    ->merge(CacheableMetadata::createFromObject($entity))
    ->merge(CacheableMetadata::createFromObject($access))
    ->applyTo($build);
  return $build;
}