You are here

public function Render::renderEntity in Bamboo Twig 8.5

Same name and namespace in other branches
  1. 8.2 bamboo_twig_loader/src/TwigExtension/Render.php \Drupal\bamboo_twig_loader\TwigExtension\Render::renderEntity()
  2. 8.3 bamboo_twig_loader/src/TwigExtension/Render.php \Drupal\bamboo_twig_loader\TwigExtension\Render::renderEntity()
  3. 8.4 bamboo_twig_loader/src/TwigExtension/Render.php \Drupal\bamboo_twig_loader\TwigExtension\Render::renderEntity()

Returns the render array for an entity.

Parameters

string $entity_type: The entity type.

mixed $id: (optional) The ID of the entity to render.

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

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

Return value

null|array A render array for the entity or NULL if the entity does not exist.

File

bamboo_twig_loader/src/TwigExtension/Render.php, line 88

Class

Render
Provides some renderer as Twig Extensions.

Namespace

Drupal\bamboo_twig_loader\TwigExtension

Code

public function renderEntity($entity_type, $id = NULL, $view_mode = NULL, $langcode = NULL) {

  // Lazy load the entity type manager only when needed.
  $entityTypeManager = $this
    ->getEntityTypeManager();
  $entity = $id ? $entityTypeManager
    ->getStorage($entity_type)
    ->load($id) : $this
    ->getCurrentRouteMatch()
    ->getParameter($entity_type);
  if (!$entity) {
    return NULL;
  }

  // Load the entity view using the current content language.
  if (!$langcode) {
    $langcode = $this
      ->getLanguageManager()
      ->getCurrentLanguage()
      ->getId();
  }
  $render_controller = $entityTypeManager
    ->getViewBuilder($entity_type);
  return $render_controller
    ->view($entity, $view_mode, $langcode);
}