You are here

public static function WebformContentCreatorUtilities::getTokenValue in Webform Content Creator 3.x

Get values inside text with tokens.

Parameters

string $value: String with tokens.

string $encryption_profile: Encryption profile.

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

string $type: Token type.

Return value

string Token value.

3 calls to WebformContentCreatorUtilities::getTokenValue()
WebformContentCreatorEntity::createNode in src/Entity/WebformContentCreatorEntity.php
Create node from webform submission.
WebformContentCreatorEntity::mapNodeField in src/Entity/WebformContentCreatorEntity.php
Use a single mapping to set a Node field value.
WebformContentCreatorEntity::updateNode in src/Entity/WebformContentCreatorEntity.php
Update node from webform submission.

File

src/WebformContentCreatorUtilities.php, line 366

Class

WebformContentCreatorUtilities
Provides useful functions required in Webform content creator module.

Namespace

Drupal\webform_content_creator

Code

public static function getTokenValue($value, $encryption_profile, WebformSubmissionInterface $webform_submission, $type = self::WEBFORM_SUBMISSION) {
  if (empty($value) || empty($webform_submission)) {
    return '';
  }

  // Get tokens in string.
  $tokens = \Drupal::token()
    ->scan($value);
  $token_keys = [];
  $token_values = [];
  if (empty($tokens)) {
    return $value;
  }
  foreach ($tokens[$type] as $val) {
    $token_value = \Drupal::token()
      ->replace($val, [
      self::WEBFORM_SUBMISSION => $webform_submission,
    ]);
    if (!empty($encryption_profile)) {

      // Decrypt single token value.
      $token_value = self::getDecryptedValue($token_value, $encryption_profile);
    }
    $token_keys[] = $val;
    $token_values[] = $token_value;
  }
  if (empty($token_values)) {
    return $value;
  }

  // Replace all token values in string.
  return str_replace($token_keys, $token_values, $value);
}