diff --git a/core/modules/views/src/Plugin/views/field/FieldPluginBase.php b/core/modules/views/src/Plugin/views/field/FieldPluginBase.php index fab7664..660f935 100644 --- a/core/modules/views/src/Plugin/views/field/FieldPluginBase.php +++ b/core/modules/views/src/Plugin/views/field/FieldPluginBase.php @@ -337,7 +337,7 @@ public function elementClasses($row_index = NULL) { * tokens so they will all be available. */ public function tokenizeValue($value, $row_index = NULL) { - if ($this->view->getStyle()->hasRowToken($value) || $this->view->getStyle()->hasArgToken($value)) { + if ($this->view->getStyle()->hasTwigToken($value) || $this->view->getStyle()->hasArgToken($value)) { $fake_item = array( 'alter_text' => TRUE, 'text' => $value, diff --git a/core/modules/views/src/Plugin/views/style/StylePluginBase.php b/core/modules/views/src/Plugin/views/style/StylePluginBase.php index 8191fca..8d05286 100644 --- a/core/modules/views/src/Plugin/views/style/StylePluginBase.php +++ b/core/modules/views/src/Plugin/views/style/StylePluginBase.php @@ -9,12 +9,10 @@ use Drupal\Component\Utility\Html; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Utility\Token; use Drupal\views\Plugin\views\PluginBase; use Drupal\views\Plugin\views\display\DisplayPluginBase; use Drupal\views\Plugin\views\wizard\WizardInterface; use Drupal\views\ViewExecutable; -use Symfony\Component\DependencyInjection\ContainerInterface; /** * @defgroup views_style_plugins Views style plugins @@ -50,13 +48,6 @@ protected $usesOptions = TRUE; /** - * The token utility. - * - * @var \Drupal\Core\Utility\Token - */ - protected $tokenUtility; - - /** * Store all available tokens row rows. */ protected $rowTokens = array(); @@ -122,36 +113,6 @@ protected $defaultFieldLabels = FALSE; /** - * Constructs a StylePluginBase object. - * - * @param array $configuration - * A configuration array containing information about the plugin instance. - * @param string $plugin_id - * The plugin_id for the plugin instance. - * @param mixed $plugin_definition - * The plugin implementation definition. - * @param \Drupal\Core\Utility\Token $token - * The Token utility. - */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, Token $token_utility) { - parent::__construct($configuration, $plugin_id, $plugin_definition); - $this->tokenUtility = $token_utility; - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { - return new static( - $configuration, - $plugin_id, - $plugin_definition, - $container->get('token') - ); - } - - - /** * Overrides \Drupal\views\Plugin\views\PluginBase::init(). * * The style options might come externally as the style can be sourced from at @@ -253,7 +214,7 @@ public function getRowClass($row_index) { * Take a value and apply token replacement logic to it. */ public function tokenizeValue($value, $row_index) { - if ($this->hasArgToken($value) || $this->hasRowToken($value)) { + if ($this->hasArgToken($value) || $this->hasTwigToken($value)) { // Row tokens might be empty, for example for node row style. $tokens = isset($this->rowTokens[$row_index]) ? $this->rowTokens[$row_index] : array(); if (!empty($this->view->build_info['substitutions'])) { @@ -296,41 +257,19 @@ public function hasArgToken($value) { * @return bool * TRUE if the string has a potential row token. */ - public function hasRowToken($value) { - // If the string definitely does not contain a token, return FALSE - // immediately for performance. - if (strpos($value, '[') === FALSE) { - return FALSE; - } - // Otherwise, scan for valid token patterns. - // Match any non-empty string between [ and ] that does not contain [ or ]. - return preg_match('/\[[^\[\]]+\]/', $value); - } - - /** - * Indicates whether the value has a global token. - * - * @param string $value - * The string to check - * - * @return bool - * TRUE if the string has a global token. - * - * @see \Drupal\Core\Utility\Token - */ - public function hasGlobalToken($value) { + public function hasTwigToken($value) { // If the string definitely does not contain a token, return FALSE // immediately for performance. - if (strpos($value, '[') === FALSE) { + if (strpos($value, '{{') === FALSE) { return FALSE; } // Otherwise, scan for valid token patterns. - $tokens = $this->tokenUtility->scan($value); - return (!empty($tokens)); + // Match any non-empty string between {{ and }} except another {{. + return preg_match('/\{\{[^(\{\{)]+\}\}/', $value); } /** - * Indicates whether the value has a potential Views token or global token. + * Indicates whether the value has a potential Views token. * * @param string $value * The string to check @@ -339,7 +278,7 @@ public function hasGlobalToken($value) { * TRUE if the string has any token pattern. */ public function hasToken($value) { - return ($this->hasArgToken($value) || $this->hasRowToken($value) || $this->hasGlobalToken($value)); + return ($this->hasArgToken($value) || $this->hasTwigToken($value)); } /** diff --git a/core/modules/views/tests/src/Unit/Plugin/style/StylePluginBaseTest.php b/core/modules/views/tests/src/Unit/Plugin/style/StylePluginBaseTest.php index 56aab5d..445552c 100644 --- a/core/modules/views/tests/src/Unit/Plugin/style/StylePluginBaseTest.php +++ b/core/modules/views/tests/src/Unit/Plugin/style/StylePluginBaseTest.php @@ -9,7 +9,6 @@ use Drupal\Tests\UnitTestCase; use Drupal\views\Plugin\views\style\StylePluginBase; -use Drupal\Core\Utility\Token; /** * @coversDefaultClass \Drupal\views\Plugin\views\style\StylePluginBase. @@ -23,41 +22,12 @@ class StylePluginBaseTest extends UnitTestCase { protected $stylePlugin; /** - * @var \Drupal\Core\Utility\Token|\PHPUnit_Framework_MockObject_MockObject - */ - protected $tokenUtility; - - /** - * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject - */ - protected $cache; - - /** - * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject - */ - protected $languageManager; - - /** - * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject - */ - protected $moduleHandler; - - /** - * @var \Drupal\Core\Language\LanguageInterface|\PHPUnit_Framework_MockObject_MockObject - */ - protected $language; - - /** * {@inheritdoc} */ protected function setUp() { parent::setUp(); - $this->cache = $this->getMock('\Drupal\Core\Cache\CacheBackendInterface'); - $this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface'); - $this->moduleHandler = $this->getMock('\Drupal\Core\Extension\ModuleHandlerInterface'); - $this->tokenUtility = new Token($this->moduleHandler, $this->cache, $this->languageManager); - $this->stylePlugin = new TestStylePlugin(array(), 'default', array(), $this->tokenUtility); + $this->stylePlugin = new TestStylePlugin(array(), 'default', array()); } /** @@ -86,65 +56,37 @@ public function providerHasArgToken() { array(' %54321 ', TRUE), array('%elephant', FALSE), array('!elephant', FALSE), - array('[other_token]', FALSE), + array('[old_token]', FALSE), + array('[other:token]', FALSE), + array('{{twig_token}}', FALSE), ); } /** - * @covers ::hasRowToken - * @dataProvider providerHasRowToken + * @covers ::hasTwigToken + * @dataProvider providerHasTwigToken */ - public function testHasRowToken($value, $return) { - $this->assertEquals($this->stylePlugin->hasRowToken($value), $return); + public function testHasTwigToken($value, $return) { + $this->assertEquals($this->stylePlugin->hasTwigToken($value), $return); } /** - * Data provider for testHasRowToken(). + * Data provider for testHasTwigToken(). * * @return array */ - public function providerHasRowToken() { - return array( - array('%1', FALSE), - array('!1', FALSE), - array('[', FALSE), - array(']', FALSE), - array('[]', FALSE), - array('[[]', FALSE), - array('[elephant]', TRUE), - array(' [elephant_row_token_1] ', TRUE), - ); - } - - /** - * @covers ::hasGlobalToken - * @dataProvider providerHasGlobalToken - * - * @see \Drupal\system\Tests\System\TokenScanTest - */ - public function testHasGlobalToken($value, $return) { - $this->assertEquals($this->stylePlugin->hasGlobalToken($value), $return); - } - - /** - * Data provider for testHasGlobalToken(). - * - * @return array - * - * @see \Drupal\system\Tests\System\TokenScanTest - */ - public function providerHasGlobalToken() { + public function providerHasTwigToken() { return array( array('%1', FALSE), array('!1', FALSE), - array('[', FALSE), - array(']', FALSE), - array('[]', FALSE), - array('[[]', FALSE), - array('[elephant]', FALSE), - // We don't need to test too many valid patterns because the token tests - // cover that already. - array(' [some:token] ', TRUE) + array('{', FALSE), + array('{{', FALSE), + array('}', FALSE), + array('}}', FALSE), + array('{{}}', FALSE), + array('{{{{}}', FALSE), + array('{{ elephant }}', TRUE), + array(' {{elephant_twig_token_1}} ', TRUE), ); } @@ -176,15 +118,16 @@ public function providerHasToken() { array(' %54321 ', TRUE), array('%elephant', FALSE), array('!elephant', FALSE), - array('[', FALSE), - array(']', FALSE), - array('[]', FALSE), - array('[[]', FALSE), - array('[elephant]', TRUE), - array(' [elephant_row_token_1] ', TRUE), - // We don't need to test too many valid patterns for global tokens - // because the token tests cover that already. - array(' [some:token] ', TRUE) + array('{', FALSE), + array('{{', FALSE), + array('}', FALSE), + array('}}', FALSE), + array('{{}}', FALSE), + array('{{{{}}', FALSE), + array('{{ elephant }}', TRUE), + array(' {{elephant_twig_token_1}} ', TRUE), + array('[old_token]', FALSE), + array('[other:token]', FALSE), ); }