You are here

public function YamlFormTokenManager::replace in YAML Form 8

Replace tokens in text.

Parameters

string|array $text: A string of text that may contain tokens.

\Drupal\Core\Entity\EntityInterface|null $entity: A YAML Form or YAML Form 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.

Overrides YamlFormTokenManagerInterface::replace

See also

\Drupal\Core\Utility\Token::replace

File

src/YamlFormTokenManager.php, line 44

Class

YamlFormTokenManager
Defines a class to manage token replacement.

Namespace

Drupal\yamlform

Code

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

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

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

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

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

  // Set token options.
  $options += [
    'clear' => TRUE,
  ];
  return $this->token
    ->replace($text, $data, $options);
}