You are here

public static function WebformContentCreatorUtilities::getDecryptedTokenValue in Webform Content Creator 2.x

Same name and namespace in other branches
  1. 8 src/WebformContentCreatorUtilities.php \Drupal\webform_content_creator\WebformContentCreatorUtilities::getDecryptedTokenValue()

Get decrypted 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.

1 call to WebformContentCreatorUtilities::getDecryptedTokenValue()
WebformContentCreatorEntity::mapContentField in src/Entity/WebformContentCreatorEntity.php
Use a single mapping to set a Content field value.

File

src/WebformContentCreatorUtilities.php, line 330

Class

WebformContentCreatorUtilities
Provides useful functions required in Webform content creator module.

Namespace

Drupal\webform_content_creator

Code

public static function getDecryptedTokenValue($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.
      $dec_token_value = self::getDecryptedValue($token_value, $encryption_profile);
    }
    else {
      $dec_token_value = $token_value;
    }
    $token_keys[] = $val;
    $token_values[] = $dec_token_value;
  }
  if (empty($token_values)) {
    return $value;
  }

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