You are here

public static function TwigWidget::validateTemplate in Twig Field 8

Validation callback for a Template element.

File

src/Plugin/Field/FieldWidget/TwigWidget.php, line 167

Class

TwigWidget
Defines the Twig field widget.

Namespace

Drupal\twig_field\Plugin\Field\FieldWidget

Code

public static function validateTemplate(&$element, FormStateInterface $form_state) {
  $build = [
    '#type' => 'inline_template',
    '#template' => $element['#value'],
    '#context' => twig_field_default_context(),
  ];
  try {
    \Drupal::service('renderer')
      ->renderPlain($build);
  } catch (\Exception $exception) {
    $form_state
      ->setError($element, t('Template error: @error', [
      '@error' => $exception
        ->getMessage(),
    ]));
  }
}