You are here

class EntityViewBuilder 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

Entity view builder.

Hierarchy

Expanded class hierarchy of EntityViewBuilder

1 string reference to 'EntityViewBuilder'
twig_tweak.services.yml in ./twig_tweak.services.yml
twig_tweak.services.yml
1 service uses EntityViewBuilder
twig_tweak.entity_view_builder in ./twig_tweak.services.yml
Drupal\twig_tweak\View\EntityViewBuilder

File

src/View/EntityViewBuilder.php, line 13

Namespace

Drupal\twig_tweak\View
View source
class EntityViewBuilder {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs an EntityViewBuilder object.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * Builds a render array for a given entity.
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityViewBuilder::$entityTypeManager protected property The entity type manager.
EntityViewBuilder::build public function Builds a render array for a given entity.
EntityViewBuilder::__construct public function Constructs an EntityViewBuilder object.