public function WebformTwigExtension::webformToken in Webform 6.x
Same name and namespace in other branches
- 8.5 src/Twig/WebformTwigExtension.php \Drupal\webform\Twig\WebformTwigExtension::webformToken()
Replace tokens in text.
Parameters
string|array $token: A string of text that may contain tokens.
\Drupal\Core\Entity\EntityInterface|null $entity: A Webform or Webform submission entity.
array $data: (optional) An array of keyed objects.
array $options: (optional) A keyed array of settings and flags to control the token replacement process.
Return value
string|array Text or array with tokens replaced.
See also
\Drupal\Core\Utility\Token::replace
File
- src/
Twig/ WebformTwigExtension.php, line 102
Class
- WebformTwigExtension
- Twig extension with some useful functions and filters.
Namespace
Drupal\webform\TwigCode
public function webformToken($token, EntityInterface $entity = NULL, array $data = [], array $options = []) {
// Allow the webform_token function to be tested during validation without
// a valid entity.
if (!$entity) {
return $token;
}
$original_token = $token;
if (WebformLogicHelper::startRecursionTracking($token) === FALSE) {
return '';
}
// Parse options included in the token.
// @see \Drupal\webform\Twig\TwigExtension::renderTwigTemplate
foreach (static::$options as $option_name => $option_setting) {
if (strpos($token, ":{$option_setting}")) {
$options[$option_name] = TRUE;
$token = str_replace(":{$option_setting}", '', $token);
}
}
// IMPORTANT: We are not injecting the WebformTokenManager to prevent
// errors being thrown when updating the Webform.module.
// ISSUE. This TwigExtension is loaded on every page load, even when a
// website is in maintenance mode.
// @see https://www.drupal.org/node/2907960
/** @var \Drupal\webform\WebformTokenManagerInterface $token_manager */
$token_manager = \Drupal::service('webform.token_manager');
$value = $token_manager
->replace($token, $entity, $data, $options);
if (WebformLogicHelper::stopRecursionTracking($original_token) === FALSE) {
return '';
}
return WebformHtmlHelper::containsHtml($value) ? [
'#markup' => $value,
'#allowed_tags' => WebformXss::getAdminTagList(),
] : $value;
}