DynamicTwigField.php in Display Suite 8.4
File
src/Plugin/DsField/DynamicTwigField.php
View source
<?php
namespace Drupal\ds\Plugin\DsField;
use Drupal\filter\Render\FilteredMarkup;
class DynamicTwigField extends TokenBase {
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 [];
}
public function content() {
$definition = $this
->getPluginDefinition();
return $definition['properties']['content'];
}
}