You are here

protected function GetTokenValue::doExecute in Rules Token 8

Same name and namespace in other branches
  1. 2.x src/Plugin/RulesAction/GetTokenValue.php \Drupal\rules_token\Plugin\RulesAction\GetTokenValue::doExecute()
  2. 1.x src/Plugin/RulesAction/GetTokenValue.php \Drupal\rules_token\Plugin\RulesAction\GetTokenValue::doExecute()

Getting a token value and provide it into context.

Parameters

string $token: The token.

mixed $token_entity: The entity from the context used in token.

File

src/Plugin/RulesAction/GetTokenValue.php, line 42

Class

GetTokenValue
Provides an 'Get token value' action.

Namespace

Drupal\rules_token\Plugin\RulesAction

Code

protected function doExecute($token, $token_entity) {

  // Set flag for removing token from the final text if no replacement value
  // can be generated.
  // For, instance, if a node body is empty then token [node:body] will return
  // '[node:body]' string. Setting 'clear' to TRUE prevents such behaviour.
  $token_options = [
    'clear' => TRUE,
  ];

  // Get the value of the token.
  if ($token && $token_entity) {

    // Extract entity name from a token, for instance if token
    // is [node:created] then entity name will be 'node'.
    $entity_name = mb_substr($token, 1, strpos($token, ':') - 1);
    $token_data = [
      $entity_name => $token_entity,
    ];
    $value = \Drupal::token()
      ->replace($token, $token_data, $token_options);
  }
  elseif ($token) {
    $value = \Drupal::token()
      ->replace($token, [], $token_options);
  }
  $this
    ->setProvidedValue('token_value', $value);
}