You are here

public function Loader::loadField in Bamboo Twig 8

Returns the render array for a single entity field.

Parameters

string $field_name: The field name.

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 field.

string $langcode: (optional) Language code to load translation.

Return value

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

File

bamboo_twig_loaders/src/TwigExtension/Loader.php, line 187

Class

Loader
Provides a 'Loader' Twig Extensions.

Namespace

Drupal\bamboo_twig_loaders\TwigExtension

Code

public function loadField($field_name, $entity_type, $id = NULL, $view_mode = 'default', $langcode = NULL) {
  $entity = $id ? $this->entityTypeManager
    ->getStorage($entity_type)
    ->load($id) : $this->routeMatch
    ->getParameter($entity_type);
  if ($langcode && $entity
    ->hasTranslation($langcode)) {
    $entity = $entity
      ->getTranslation($langcode);
  }
  if (isset($entity->{$field_name})) {
    return $entity->{$field_name}
      ->view($view_mode);
  }
  return NULL;
}