You are here

public function WebformTokenManager::replace in Webform 6.x

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

Replace tokens in text.

Parameters

string|array $text: 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. Supported options are:

  • langcode: A language code to be used when generating locale-sensitive tokens.
  • callback: A callback function that will be used to post-process the array of token replacements after they are generated.
  • clear: A boolean flag indicating that tokens should be removed from the final text if no replacement value can be generated.

\Drupal\Core\Render\BubbleableMetadata|null $bubbleable_metadata: (optional) An object to which static::generate() and the hooks and functions that it invokes will add their required bubbleable metadata.

Return value

string|array Text or array with tokens replaced.

Overrides WebformTokenManagerInterface::replace

See also

\Drupal\Core\Utility\Token::replace

1 call to WebformTokenManager::replace()
WebformTokenManager::replaceNoRenderContext in src/WebformTokenManager.php
Replace tokens in text with no render context.

File

src/WebformTokenManager.php, line 111

Class

WebformTokenManager
Defines a class to manage token replacement.

Namespace

Drupal\webform

Code

public function replace($text, EntityInterface $entity = NULL, array $data = [], array $options = [], BubbleableMetadata $bubbleable_metadata = NULL) {

  // Replace tokens within an array.
  if (is_array($text)) {
    foreach ($text as $key => $token_value) {
      $text[$key] = $this
        ->replace($token_value, $entity, $data, $options, $bubbleable_metadata);
    }
    return $text;
  }

  // Most strings won't contain tokens so let's check and return ASAP.
  if (!is_string($text) || strpos($text, '[') === FALSE) {
    return $text;
  }
  if ($entity) {

    // Replace @deprecated [webform-submission] with [webform_submission].
    $text = str_replace('[webform-submission:', '[webform_submission:', $text);

    // Set token data based on entity type.
    $this
      ->setTokenData($data, $entity);

    // Set token options based on entity.
    $this
      ->setTokenOptions($options, $entity);
  }

  // For anonymous users remove all [current-user] tokens to prevent
  // anonymous user properties from being displayed.
  // For example, the [current-user:display-name] token will return
  // 'Anonymous', which is not an expected behavior.
  if ($this->currentUser
    ->isAnonymous() && strpos($text, '[current-user:') !== FALSE) {
    $text = preg_replace('/\\[current-user:[^]]+\\]/', '', $text);
  }

  // Get supported suffixes.
  $suffixes = $this
    ->getSuffixes($options);

  // Prepare suffixes.
  $text = $this
    ->prepareSuffixes($text, $suffixes);

  // Replace the webform related tokens.
  $text = $this->token
    ->replace($text, $data, $options, $bubbleable_metadata);

  // Process token suffixes.
  $text = $this
    ->processSuffixes($text);

  // Clear current user tokens for undefined values.
  if (strpos($text, '[current-user:') !== FALSE) {
    $text = preg_replace('/\\[current-user:[^\\]]+\\]/', '', $text);
  }
  return $text;
}