class GetTokenValue in Rules Token 1.x
Same name and namespace in other branches
- 8 src/Plugin/RulesAction/GetTokenValue.php \Drupal\rules_token\Plugin\RulesAction\GetTokenValue
- 2.x src/Plugin/RulesAction/GetTokenValue.php \Drupal\rules_token\Plugin\RulesAction\GetTokenValue
Provides an 'Get token value' action.
Plugin annotation
@RulesAction(
id = "rules_token_get_token_value",
label = @Translation("Get token value"),
category = @Translation("Data"),
context_definitions = {
"token" = @ContextDefinition("string",
label = @Translation("Token"),
assignment_restriction = "input"
),
"token_entity" = @ContextDefinition("any",
label = @Translation("Entity of Token"),
description = @Translation("Select from the selector the entity used in token. Or if you use global tokens like [date:short] then keep this field empty."),
required = FALSE
)
},
provides = {
"token_value" = @ContextDefinition("any",
label = @Translation("Token value")
)
}
)
Hierarchy
- class \Drupal\rules_token\Plugin\RulesAction\GetTokenValue extends \Drupal\rules\Core\RulesActionBase
Expanded class hierarchy of GetTokenValue
File
- src/
Plugin/ RulesAction/ GetTokenValue.php, line 32
Namespace
Drupal\rules_token\Plugin\RulesActionView source
class GetTokenValue extends RulesActionBase {
/**
* Getting a token value and provide it into context.
*
* @param string $token
* The token.
* @param mixed $token_entity
* The entity from the context used in token.
*/
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);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
GetTokenValue:: |
protected | function | Getting a token value and provide it into context. |