From ff0f9a93a74d67660f43c8a8d9c802f6eab20613 Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Mon, 17 Jun 2024 09:32:09 +0100 Subject: [PATCH] Issue #2684251 by pooja_sharma, sheldonreed3, smustgrave, jehon, Lendude: Global Token Replacements is not working correctly in href (cherry picked from commit ca4535dd9a3d7a8bcbb5164b69e2be17a46f3ea4) --- .../views/area/TokenizeAreaPluginBase.php | 7 +- .../src/Kernel/Handler/AreaTextTokenTest.php | 69 +++++++++++++++++++ 2 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 core/modules/views/tests/src/Kernel/Handler/AreaTextTokenTest.php diff --git a/core/modules/views/src/Plugin/views/area/TokenizeAreaPluginBase.php b/core/modules/views/src/Plugin/views/area/TokenizeAreaPluginBase.php index 02533c7198b6..b563ebefaab1 100644 --- a/core/modules/views/src/Plugin/views/area/TokenizeAreaPluginBase.php +++ b/core/modules/views/src/Plugin/views/area/TokenizeAreaPluginBase.php @@ -107,11 +107,14 @@ public function tokenForm(&$form, FormStateInterface $form_state) { * will be replaced. */ public function tokenizeValue($value) { + // As we add the globalTokenForm() we also should replace the token here. + $value = $this->globalTokenReplace($value); + if ($this->options['tokenize']) { $value = $this->view->getStyle()->tokenizeValue($value, 0); } - // As we add the globalTokenForm() we also should replace the token here. - return $this->globalTokenReplace($value); + + return $value; } } diff --git a/core/modules/views/tests/src/Kernel/Handler/AreaTextTokenTest.php b/core/modules/views/tests/src/Kernel/Handler/AreaTextTokenTest.php new file mode 100644 index 000000000000..04fce742be68 --- /dev/null +++ b/core/modules/views/tests/src/Kernel/Handler/AreaTextTokenTest.php @@ -0,0 +1,69 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\Tests\views\Kernel\Handler; + +use Drupal\Tests\views\Kernel\ViewsKernelTestBase; +use Drupal\views\Views; + +/** + * Tests the token in text area handler. + * + * @group views + * @see \Drupal\views\Plugin\views\area\Text + */ +class AreaTextTokenTest extends ViewsKernelTestBase { + + protected static $modules = ['system', 'user', 'filter']; + + /** + * Views used by this test. + * + * @var array + */ + public static $testViews = ['test_view']; + + /** + * {@inheritdoc} + */ + protected function setUp($import_test_views = TRUE): void { + parent::setUp(); + + $this->installConfig(['system', 'filter']); + $this->installEntitySchema('user'); + } + + /** + * Tests the token into text area plugin within header. + */ + public function testAreaTextToken(): void { + /** @var \Drupal\Core\Render\RendererInterface $renderer */ + $renderer = $this->container->get('renderer'); + $view = Views::getView('test_view'); + $view->setDisplay(); + + // Enable checkbox 'token replacement', add token into href in text header. + $string = '<a href="[site:url]">Added Site URL token in href</a>'; + $view->displayHandlers->get('default')->overrideOption('header', [ + 'area' => [ + 'id' => 'area', + 'table' => 'views', + 'field' => 'area', + 'content' => [ + 'value' => $string, + ], + 'tokenize' => TRUE, + ], + ]); + + // Execute the view. + $this->executeView($view); + + $build = $view->display_handler->handlers['header']['area']->render(); + $replaced_token = \Drupal::token()->replace('[site:url]'); + $desired_output = str_replace('[site:url]', $replaced_token, $string); + $this->assertEquals(check_markup($desired_output), $renderer->renderRoot($build), 'Global token assessed in href'); + } + +} -- GitLab