You are here

public static function RenderThis::renderThisFilter in Twig Render This 8

Returns the rendered array for a single entity field.

Parameters

object $content: Entity or Field object.

string $view_mode: Name of the display mode.

Return value

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

File

src/TwigExtension/RenderThis.php, line 41

Class

RenderThis
Twig Render This filter.

Namespace

Drupal\twig_render_this\TwigExtension

Code

public static function renderThisFilter($content, $view_mode = 'default') {
  if ($content instanceof EntityInterface) {
    $view_builder = \Drupal::entityTypeManager()
      ->getViewBuilder($content
      ->getEntityTypeId());
    return $view_builder
      ->view($content, $view_mode);
  }
  elseif ($content instanceof FieldItemInterface || $content instanceof FieldItemListInterface || method_exists($content, 'view')) {
    return $content
      ->view($view_mode);
  }
  else {
    return t('Twig Render This: Unsupported content.');
  }
}