You are here

public static function WebformComputedTwig::computeValue in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Element/WebformComputedTwig.php \Drupal\webform\Element\WebformComputedTwig::computeValue()

Compute value.

Parameters

array $element: An element.

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

Return value

string The computed value.

Overrides WebformComputedBase::computeValue

File

src/Element/WebformComputedTwig.php, line 41

Class

WebformComputedTwig
Provides an item to display computed webform submission values using Twig.

Namespace

Drupal\webform\Element

Code

public static function computeValue(array $element, WebformSubmissionInterface $webform_submission) {

  /** @var \Drupal\webform\WebformThemeManagerInterface $theme_manager */
  $theme_manager = \Drupal::service('webform.theme_manager');

  // Do not compute value via Twig if there is no active theme,
  // except for CLI.
  // Rendering a Twig template before the theme is activated can cause
  // unexpected behaviors.
  if (!$theme_manager
    ->hasActiveTheme() && PHP_SAPI !== 'cli') {
    return '';
  }
  $whitespace = !empty($element['#whitespace']) ? $element['#whitespace'] : '';
  $template = $whitespace === static::WHITESPACE_SPACELESS ? '{% spaceless %}' . $element['#template'] . '{% endspaceless %}' : $element['#template'];
  $options = [
    'html' => static::getMode($element) === WebformComputedInterface::MODE_HTML,
  ];
  $value = WebformTwigExtension::renderTwigTemplate($webform_submission, $template, $options);
  return $whitespace === static::WHITESPACE_TRIM ? trim($value) : $value;
}