function PluginBase::viewsTokenReplace
Same name in other branches
- 9 core/modules/views/src/Plugin/views/PluginBase.php \Drupal\views\Plugin\views\PluginBase::viewsTokenReplace()
- 8.9.x core/modules/views/src/Plugin/views/PluginBase.php \Drupal\views\Plugin\views\PluginBase::viewsTokenReplace()
- 10 core/modules/views/src/Plugin/views/PluginBase.php \Drupal\views\Plugin\views\PluginBase::viewsTokenReplace()
Replaces Views' tokens in a given string.
The resulting string will be sanitized with Xss::filterAdmin.
Parameters
$text: Unsanitized string with possible tokens.
$tokens: Array of token => replacement_value items.
Return value
string
6 calls to PluginBase::viewsTokenReplace()
- DisplayPluginBase::getMoreUrl in core/
modules/ views/ src/ Plugin/ views/ display/ DisplayPluginBase.php - Get the more URL for this view.
- FieldPluginBase::renderAltered in core/
modules/ views/ src/ Plugin/ views/ field/ FieldPluginBase.php - Render this field as user-defined altered text.
- FieldPluginBase::renderAsLink in core/
modules/ views/ src/ Plugin/ views/ field/ FieldPluginBase.php - Render this field as a link, with the info from a fieldset set by the user.
- FieldPluginBase::renderText in core/
modules/ views/ src/ Plugin/ views/ field/ FieldPluginBase.php - Links::getLinks in core/
modules/ views/ src/ Plugin/ views/ field/ Links.php - Gets the list of links used by this field.
1 method overrides PluginBase::viewsTokenReplace()
- TestPluginBase::viewsTokenReplace in core/
modules/ views/ tests/ src/ Kernel/ Plugin/ PluginBaseTest.php - Replaces Views' tokens in a given string.
File
-
core/
modules/ views/ src/ Plugin/ views/ PluginBase.php, line 358
Class
- PluginBase
- Base class for any views plugin types.
Namespace
Drupal\views\Plugin\viewsCode
protected function viewsTokenReplace($text, $tokens) {
if (!strlen($text)) {
// No need to run filterAdmin on an empty string.
return '';
}
if (empty($tokens)) {
return Xss::filterAdmin($text);
}
$twig_tokens = [];
foreach ($tokens as $token => $replacement) {
// Twig wants a token replacement array stripped of curly-brackets.
// Some Views tokens come with curly-braces, others do not.
// @todo https://www.drupal.org/node/2544392
if (str_contains($token, '{{')) {
// Twig wants a token replacement array stripped of curly-brackets.
$token = trim(str_replace([
'{{',
'}}',
], '', $token));
}
// Check for arrays in Twig tokens. Internally these are passed as
// dot-delimited strings, but need to be turned into associative arrays
// for parsing.
if (!str_contains($token, '.')) {
// We need to validate tokens are valid Twig variables. Twig uses the
// same variable naming rules as PHP.
// @see http://php.net/manual/language.variables.basics.php
assert(preg_match('/^[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*$/', $token) === 1, 'Tokens need to be valid Twig variables.');
$twig_tokens[$token] = $replacement;
}
else {
$parts = explode('.', $token);
$top = array_shift($parts);
assert(preg_match('/^[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*$/', $top) === 1, 'Tokens need to be valid Twig variables.');
$token_array = [
array_pop($parts) => $replacement,
];
foreach (array_reverse($parts) as $key) {
// The key could also be numeric (array index) so allow that.
assert(is_numeric($key) || preg_match('/^[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*$/', $key) === 1, 'Tokens need to be valid Twig variables.');
$token_array = [
$key => $token_array,
];
}
if (!isset($twig_tokens[$top])) {
$twig_tokens[$top] = [];
}
$twig_tokens[$top] += $token_array;
}
}
if ($twig_tokens) {
// Use the unfiltered text for the Twig template, then filter the output.
// Otherwise, Xss::filterAdmin could remove valid Twig syntax before the
// template is parsed.
$build = [
'#type' => 'inline_template',
'#template' => $text,
'#context' => $twig_tokens,
'#post_render' => [
function ($children, $elements) {
return Xss::filterAdmin($children);
},
],
];
// Currently you cannot attach assets to tokens with
// Renderer::renderInIsolation(). This may be unnecessarily limiting. Consider
// using Renderer::executeInRenderContext() instead.
// @todo https://www.drupal.org/node/2566621
return (string) $this->getRenderer()
->renderInIsolation($build);
}
else {
return Xss::filterAdmin($text);
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.