You are here

protected function WebformTokenManager::processSuffixes in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/WebformTokenManager.php \Drupal\webform\WebformTokenManager::processSuffixes()

Process token suffixes after all tokens are replaced.

Parameters

string|array $text: A string of text that may contain {webform-token-suffixes} tags.

Return value

string String to text with all tokens suffixes processed.

1 call to WebformTokenManager::processSuffixes()
WebformTokenManager::replace in src/WebformTokenManager.php
Replace tokens in text.

File

src/WebformTokenManager.php, line 427

Class

WebformTokenManager
Defines a class to manage token replacement.

Namespace

Drupal\webform

Code

protected function processSuffixes($text) {
  if (preg_match_all('/{webform-token-suffixes:([^}]+)}(.*?){\\/webform-token-suffixes}/ms', $text, $matches)) {
    foreach ($matches[0] as $index => $match) {
      $search = $matches[0][$index];
      $replace = $matches[2][$index];
      $value = $matches[2][$index];
      $suffixes = explode(':', $matches[1][$index]);
      $suffixes = array_combine($suffixes, $suffixes);

      // If token is not replaced then only the :clear suffix is applicable.
      if (preg_match('/^\\[[^}]+\\]$/', $value)) {

        // Clear token text or restore the original token.
        $original = str_replace(']', ':' . $matches[1][$index] . ']', $value);
        $replace = isset($suffixes['clear']) ? '' : $original;
      }
      else {

        // Decode and XSS filter value first.
        if (isset($suffixes['htmldecode'])) {
          $replace = html_entity_decode($replace, ENT_QUOTES);
          $replace = isset($suffixes['striptags']) ? strip_tags($replace) : html_entity_decode(Xss::filterAdmin($replace));
        }

        // Encode URL.
        if (isset($suffixes['urlencode'])) {
          $replace = urlencode($replace);
        }
        if (isset($suffixes['rawurlencode'])) {
          $replace = rawurlencode($replace);
        }

        // Encode xml.
        if (isset($suffixes['xmlencode'])) {
          $replace = htmlspecialchars($replace, ENT_XML1);
        }
      }
      $text = str_replace($search, $replace, $text);
    }
  }
  return $text;
}