class EntityViewBuilder in Twig Tweak 3.1.x
Same name and namespace in other branches
- 3.x src/View/EntityViewBuilder.php \Drupal\twig_tweak\View\EntityViewBuilder
Entity view builder.
Hierarchy
- class \Drupal\twig_tweak\View\EntityViewBuilder
Expanded class hierarchy of EntityViewBuilder
1 string reference to 'EntityViewBuilder'
1 service uses EntityViewBuilder
File
- src/
View/ EntityViewBuilder.php, line 13
Namespace
Drupal\twig_tweak\ViewView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityViewBuilder:: |
protected | property | The entity type manager. | |
EntityViewBuilder:: |
public | function | Builds a render array for a given entity. | |
EntityViewBuilder:: |
public | function | Constructs an EntityViewBuilder object. |