You are here

public static function TwigTweakExtension::drupalEntity in Twig Tweak 3.1.x

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

Returns the render array to represent an entity.

File

src/TwigTweakExtension.php, line 158

Class

TwigTweakExtension
Twig extension with some useful functions and filters.

Namespace

Drupal\twig_tweak

Code

public static function drupalEntity(string $entity_type, string $selector, string $view_mode = 'full', ?string $langcode = NULL, bool $check_access = TRUE) : array {
  $storage = \Drupal::entityTypeManager()
    ->getStorage($entity_type);
  if (Uuid::isValid($selector)) {
    $entities = $storage
      ->loadByProperties([
      'uuid' => $selector,
    ]);
    $entity = reset($entities);
  }
  else {
    $entity = $storage
      ->load($selector);
  }
  if ($entity) {
    return \Drupal::service('twig_tweak.entity_view_builder')
      ->build($entity, $view_mode, $langcode, $check_access);
  }
  return [];
}