You are here

public function DynamicTwigField::build in Display Suite 8.4

Same name and namespace in other branches
  1. 8.3 src/Plugin/DsField/DynamicTwigField.php \Drupal\ds\Plugin\DsField\DynamicTwigField::build()

Renders a field.

Return value

array A renderable array representing the content of the field.

Overrides TokenBase::build

File

src/Plugin/DsField/DynamicTwigField.php, line 20

Class

DynamicTwigField
Defines a generic dynamic twig field.

Namespace

Drupal\ds\Plugin\DsField

Code

public function build() {
  $output = '';
  $content = $this
    ->content();
  $render = [
    '#type' => 'inline_template',
    '#template' => $content,
    '#context' => [
      $this
        ->getEntityTypeId() => $this
        ->entity(),
      'entity' => $this
        ->entity(),
    ],
  ];
  try {
    $output = \Drupal::service('renderer')
      ->render($render);
  } catch (\Exception $e) {
    \Drupal::logger('ds_twig_field')
      ->error('Error rendering @field: @message', [
      '@field' => $this
        ->getDerivativeId(),
      '@message' => $e
        ->getMessage(),
    ]);
  }
  if ($output) {
    return [
      '#markup' => FilteredMarkup::create($output),
    ];
  }
  return [];
}