You are here

public static function WebformTwigExtension::buildTwigTemplate in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Twig/WebformTwigExtension.php \Drupal\webform\Twig\WebformTwigExtension::buildTwigTemplate()

Build a Twig template with a webform submission.

Parameters

\Drupal\webform\WebformSubmissionInterface $webform_submission: A webform submission.

string $template: A inline Twig template.

array $options: (optional) Template, token options, and context.

array $context: (optional) Context to be passed to inline Twig template.

Return value

array A renderable containing an inline twig template.

5 calls to WebformTwigExtension::buildTwigTemplate()
WebformCodeMirror::validateTwig in src/Element/WebformCodeMirror.php
Validate Twig.
WebformElementBase::formatCustomItem in src/Plugin/WebformElementBase.php
Format an element's item using custom HTML or plain text.
WebformElementBase::formatCustomItems in src/Plugin/WebformElementBase.php
Format an element's items using custom HTML or plain text.
WebformSubmissionViewBuilder::buildComponents in src/WebformSubmissionViewBuilder.php
Builds the component fields and properties of a set of entities.
WebformTwigExtension::renderTwigTemplate in src/Twig/WebformTwigExtension.php
Render a Twig template with a webform submission.

File

src/Twig/WebformTwigExtension.php, line 278

Class

WebformTwigExtension
Twig extension with some useful functions and filters.

Namespace

Drupal\webform\Twig

Code

public static function buildTwigTemplate(WebformSubmissionInterface $webform_submission, $template, array $options = [], array $context = []) {
  $options += [
    'html' => FALSE,
    'email' => FALSE,
  ];

  // Include 'html' and 'email' options in the token.
  // @see \Drupal\webform\Twig\TwigExtension::renderTwigTemplate
  foreach (static::$options as $option_name => $option_setting) {
    if ($options[$option_name]) {
      $template = preg_replace('/\\[(webform_submission:values:?[^]]*)\\]/', '[\\1:' . $option_setting . ']', $template);
    }
  }

  // If the template does NOT use the webform_token() function, but contains
  // simple tokens, convert the simple tokens to use
  // the webform_token() function.
  if (strpos($template, 'webform_token(') === FALSE && strpos($template, '[webform') !== FALSE) {
    $template = preg_replace('#([^"\']|^)(\\[[^]]+\\])([^"\']|$)#', '\\1{{ webform_token(\'\\2\', webform_submission) }}\\3', $template);
  }
  $context += [
    'webform_submission' => $webform_submission,
    'webform' => $webform_submission
      ->getWebform(),
    'elements' => $webform_submission
      ->getWebform()
      ->getElementsDecoded(),
    'elements_flattened' => $webform_submission
      ->getWebform()
      ->getElementsDecodedAndFlattened(),
    'options' => $options,
    'original_data' => $webform_submission
      ->getOriginalData(),
  ] + $webform_submission
    ->toArray(TRUE);
  return [
    '#type' => 'inline_template',
    '#template' => $template,
    '#context' => $context,
  ];
}