diff --git a/core/modules/taxonomy/src/Plugin/views/field/TaxonomyIndexTid.php b/core/modules/taxonomy/src/Plugin/views/field/TaxonomyIndexTid.php index 4fab01c..0ec6dc7 100644 --- a/core/modules/taxonomy/src/Plugin/views/field/TaxonomyIndexTid.php +++ b/core/modules/taxonomy/src/Plugin/views/field/TaxonomyIndexTid.php @@ -169,16 +169,15 @@ function render_item($count, $item) { } protected function documentSelfTokens(&$tokens) { - $tokens['[' . $this->options['id'] . '-tid' . ']'] = $this->t('The taxonomy term ID for the term.'); - $tokens['[' . $this->options['id'] . '-name' . ']'] = $this->t('The taxonomy term name for the term.'); - $tokens['[' . $this->options['id'] . '-vocabulary-vid' . ']'] = $this->t('The machine name for the vocabulary the term belongs to.'); - $tokens['[' . $this->options['id'] . '-vocabulary' . ']'] = $this->t('The name for the vocabulary the term belongs to.'); + $tokens['[' . $this->options['id'] . '__tid' . ']'] = $this->t('The taxonomy term ID for the term.'); + $tokens['[' . $this->options['id'] . '__name' . ']'] = $this->t('The taxonomy term name for the term.'); + $tokens['[' . $this->options['id'] . '__vocabulary_vid' . ']'] = $this->t('The machine name for the vocabulary the term belongs to.'); + $tokens['[' . $this->options['id'] . '__vocabulary' . ']'] = $this->t('The name for the vocabulary the term belongs to.'); } protected function addSelfTokens(&$tokens, $item) { foreach (array('tid', 'name', 'vocabulary_vid', 'vocabulary') as $token) { - // Replace _ with - for the vocabulary vid. - $tokens['[' . $this->options['id'] . '-' . str_replace('_', '-', $token) . ']'] = isset($item[$token]) ? $item[$token] : ''; + $tokens['[' . $this->options['id'] . '__' . $token . ']'] = isset($item[$token]) ? $item[$token] : ''; } } diff --git a/core/modules/user/src/Plugin/views/field/Roles.php b/core/modules/user/src/Plugin/views/field/Roles.php index 403e9a4..03e2adf 100644 --- a/core/modules/user/src/Plugin/views/field/Roles.php +++ b/core/modules/user/src/Plugin/views/field/Roles.php @@ -101,14 +101,14 @@ function render_item($count, $item) { } protected function documentSelfTokens(&$tokens) { - $tokens['[' . $this->options['id'] . '-role' . ']'] = $this->t('The name of the role.'); - $tokens['[' . $this->options['id'] . '-rid' . ']'] = $this->t('The role machine-name of the role.'); + $tokens['[' . $this->options['id'] . '__role' . ']'] = $this->t('The name of the role.'); + $tokens['[' . $this->options['id'] . '__rid' . ']'] = $this->t('The role machine-name of the role.'); } protected function addSelfTokens(&$tokens, $item) { if (!empty($item['role'])) { - $tokens['[' . $this->options['id'] . '-role' . ']'] = $item['role']; - $tokens['[' . $this->options['id'] . '-rid' . ']'] = $item['rid']; + $tokens['[' . $this->options['id'] . '__role' . ']'] = $item['role']; + $tokens['[' . $this->options['id'] . '__rid' . ']'] = $item['rid']; } } diff --git a/core/modules/views/src/Plugin/views/PluginBase.php b/core/modules/views/src/Plugin/views/PluginBase.php index e56dcae..f186bdd 100644 --- a/core/modules/views/src/Plugin/views/PluginBase.php +++ b/core/modules/views/src/Plugin/views/PluginBase.php @@ -365,6 +365,8 @@ protected function viewsTokenReplace($text, $tokens) { if (strpos($token, '{{') !== FALSE) { // Twig wants a token replacement array stripped of curly-brackets. $token = trim(str_replace(array('{', '}'), '', $token)); + // We need to validate they are valid twig variables. + assert('preg_match(\'/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/\', $token) === 1', 'Tokens need to be valid variables.'); $twig_tokens[$token] = $replacement; } else { diff --git a/core/modules/views/src/Plugin/views/field/Field.php b/core/modules/views/src/Plugin/views/field/Field.php index 251d025..ae67c95 100644 --- a/core/modules/views/src/Plugin/views/field/Field.php +++ b/core/modules/views/src/Plugin/views/field/Field.php @@ -903,7 +903,7 @@ function render_item($count, $item) { protected function documentSelfTokens(&$tokens) { $field = $this->getFieldDefinition(); foreach ($field->getColumns() as $id => $column) { - $tokens['{{ ' . $this->options['id'] . '-' . $id . ' }}'] = $this->t('Raw @column', array('@column' => $id)); + $tokens['{{ ' . $this->options['id'] . '__' . $id . ' }}'] = $this->t('Raw @column', array('@column' => $id)); } } @@ -913,19 +913,28 @@ protected function addSelfTokens(&$tokens, $item) { // Use \Drupal\Component\Utility\Xss::filterAdmin() because it's user data // and we can't be sure it is safe. We know nothing about the data, // though, so we can't really do much else. - if (isset($item['raw'])) { // If $item['raw'] is an array then we can use as is, if it's an object - // we cast it to an array, if it's neither, we can't use it. - $raw = is_array($item['raw']) ? $item['raw'] : - (is_object($item['raw']) ? (array)$item['raw'] : NULL); + // we have to process it differently. If it's neither, we can't use it. + $raw = $item['raw']; } - if (isset($raw) && isset($raw[$id]) && is_scalar($raw[$id])) { - $tokens['{{ ' . $this->options['id'] . '-' . $id . ' }}'] = CoreXss::filterAdmin($raw[$id]); + if ($raw && is_array($raw)) { + if ($raw !== NULL && isset($raw[$id]) && is_scalar($raw[$id])) { + $tokens['{{ ' . $this->options['id'] . '__' . $id . ' }}'] = CoreXss::filterAdmin($raw[$id]); + } + else { + // Make sure that empty values are replaced as well. + $tokens['{{ ' . $this->options['id'] . '__' . $id . ' }}'] = ''; + } } - else { - // Make sure that empty values are replaced as well. - $tokens['{{ ' . $this->options['id'] . '-' . $id . ' }}'] = ''; + else if($raw && is_object($raw)) { + if ($raw !== NULL && isset($raw->$id) && is_scalar($raw->$id)) { + $tokens['{{ ' . $this->options['id'] . '__' . $id . ' }}'] = CoreXss::filterAdmin($raw->$id); + } + else { + // Make sure that empty values are replaced as well. + $tokens['{{ ' . $this->options['id'] . '__' . $id . ' }}'] = ''; + } } } } diff --git a/core/modules/views/src/Plugin/views/field/FieldPluginBase.php b/core/modules/views/src/Plugin/views/field/FieldPluginBase.php index ee16994..0370bbf 100644 --- a/core/modules/views/src/Plugin/views/field/FieldPluginBase.php +++ b/core/modules/views/src/Plugin/views/field/FieldPluginBase.php @@ -1634,10 +1634,10 @@ protected function getTokenValuesRecursive(array $array, array $parent_keys = ar * fields as a list. For example, the field that displays all terms * on a node might have tokens for the tid and the term. * - * By convention, tokens should follow the format of {{ token-subtoken }} + * By convention, tokens should follow the format of {{ token__subtoken }} * where token is the field ID and subtoken is the field. If the - * field ID is terms, then the tokens might be {{ terms-tid }} and - * {{ terms-name }}. + * field ID is terms, then the tokens might be {{ terms__tid }} and + * {{ terms__name }}. */ protected function addSelfTokens(&$tokens, $item) { } diff --git a/core/modules/views/src/Tests/Plugin/PluginBaseTest.php b/core/modules/views/src/Tests/Plugin/PluginBaseTest.php new file mode 100644 index 0000000..faa736f --- /dev/null +++ b/core/modules/views/src/Tests/Plugin/PluginBaseTest.php @@ -0,0 +1,68 @@ +testPluginBase = new TestPluginBase(); + } + + /** + * Test that the token replacement in views works correctly. + */ + public function testViewsTokenReplace() { + $text = '{{ langcode__value }} means {{ langcode }}'; + $tokens = [ '{{ langcode }}' => SafeString::create('English'), '{{ langcode__value }}' => 'en']; + + + $result = \Drupal::service('renderer')->executeInRenderContext(new RenderContext(), function () use ($text, $tokens) { + return $this->testPluginBase->viewsTokenReplace($text, $tokens); + }); + + $this->assertIdentical($result, 'en means English'); + } + + } + +} + +namespace Drupal\views\Plugin\views { + + /** + * Helper class for using the PluginBase abstract class. + */ + class TestPluginBase extends PluginBase { + + public function __construct() { + parent::__construct([], '', []); + } + + public function viewsTokenReplace($text, $tokens) { + return parent::viewsTokenReplace($text, $tokens); + } + + } + +} diff --git a/core/modules/views/tests/modules/views_test_data/src/Plugin/views/field/FieldTest.php b/core/modules/views/tests/modules/views_test_data/src/Plugin/views/field/FieldTest.php index 74d5d3d..0f8feef 100644 --- a/core/modules/views/tests/modules/views_test_data/src/Plugin/views/field/FieldTest.php +++ b/core/modules/views/tests/modules/views_test_data/src/Plugin/views/field/FieldTest.php @@ -46,7 +46,7 @@ public function getTestValue() { * Overrides Drupal\views\Plugin\views\field\FieldPluginBase::addSelfTokens(). */ protected function addSelfTokens(&$tokens, $item) { - $tokens['[test-token]'] = $this->getTestValue(); + $tokens['[test__token]'] = $this->getTestValue(); } /**