You are here

public function Loader::loadEntity in Bamboo Twig 8.5

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

Returns an entity object in the current context language.

Keep in mind languages loading priorities: 1. Get the entity in the current page lang, 2. When not found, try to fetch the entity in the default site lang, 3. When not found in 2 previous attempts, fetch the original entity lang.

Parameters

string $entity_type: The entity type.

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

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

Return value

null|Drupal\Core\Entity\EntityInterface An entity object for the entity or NULL if the entity does not exist.

1 call to Loader::loadEntity()
Loader::loadField in bamboo_twig_loader/src/TwigExtension/Loader.php
Returns the field object for a single entity field.

File

bamboo_twig_loader/src/TwigExtension/Loader.php, line 51

Class

Loader
Provides some loaders as Twig Extensions.

Namespace

Drupal\bamboo_twig_loader\TwigExtension

Code

public function loadEntity($entity_type, $id = NULL, $langcode = NULL) {
  $entityRepository = $this
    ->getEntityRepository();
  $entity = $id ? $this
    ->getEntityTypeManager()
    ->getStorage($entity_type)
    ->load($id) : $this
    ->getCurrentRouteMatch()
    ->getParameter($entity_type);
  if (!$entity) {
    return NULL;
  }

  // Get the entity in the current context language.
  return $entityRepository
    ->getTranslationFromContext($entity, $langcode);
}