diff --git a/core/modules/views/src/Plugin/views/style/StylePluginBase.php b/core/modules/views/src/Plugin/views/style/StylePluginBase.php index 542aaf3..8191fca 100644 --- a/core/modules/views/src/Plugin/views/style/StylePluginBase.php +++ b/core/modules/views/src/Plugin/views/style/StylePluginBase.php @@ -9,10 +9,12 @@ 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 @@ -48,6 +50,13 @@ protected $usesOptions = TRUE; /** + * The token utility. + * + * @var \Drupal\Core\Utility\Token + */ + protected $tokenUtility; + + /** * Store all available tokens row rows. */ protected $rowTokens = array(); @@ -113,6 +122,36 @@ 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 @@ -230,13 +269,13 @@ public function tokenizeValue($value, $row_index) { } /** - * Indicates whether the value has a Views argument token. + * Indicates whether the value has a potential Views argument token. * * @param string $value * The string to check * * @return bool - * TRUE if the string has a Views token. + * TRUE if the string has a potential Views argument token. */ public function hasArgToken($value) { // If the string definitely does not contain a token, return FALSE @@ -249,13 +288,13 @@ public function hasArgToken($value) { } /** - * Indicates whether the value has a Views row-level token. + * Indicates whether the value has a potential Views row-level token. * * @param string $value * The string to check * * @return bool - * TRUE if the string has a global token. + * TRUE if the string has a potential row token. */ public function hasRowToken($value) { // If the string definitely does not contain a token, return FALSE @@ -286,18 +325,18 @@ public function hasGlobalToken($value) { return FALSE; } // Otherwise, scan for valid token patterns. - $tokens = \Drupal::token()->scan($value); + $tokens = $this->tokenUtility->scan($value); return (!empty($tokens)); } /** - * Indicates whether the value has a special Views token or a global token. + * Indicates whether the value has a potential Views token or global token. * * @param string $value * The string to check * * @return bool - * TRUE if the string has any token. + * TRUE if the string has any token pattern. */ public function hasToken($value) { return ($this->hasArgToken($value) || $this->hasRowToken($value) || $this->hasGlobalToken($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 c16025a..56aab5d 100644 --- a/core/modules/views/tests/src/Unit/Plugin/style/StylePluginBaseTest.php +++ b/core/modules/views/tests/src/Unit/Plugin/style/StylePluginBaseTest.php @@ -1,6 +1,5 @@ stylePlugin = new TestStylePlugin(array(), 'default', array()); + $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); } /** @@ -94,9 +123,7 @@ public function providerHasRowToken() { * @see \Drupal\system\Tests\System\TokenScanTest */ public function testHasGlobalToken($value, $return) { - // @todo This needs to have Token available, and atm the whole bleeping - // Drupal class and service container as well. - //$this->assertEquals($this->stylePlugin->hasGlobalToken($value), $return); + $this->assertEquals($this->stylePlugin->hasGlobalToken($value), $return); } /** @@ -123,8 +150,42 @@ public function providerHasGlobalToken() { /** * @covers ::hasToken + * @dataProvider providerHasToken + */ + public function testHasToken($value, $return) { + $this->assertEquals($this->stylePlugin->hasToken($value), $return); + } + + /** + * Data provider for testHasToken(). + * + * @return array + * + * @see \Drupal\system\Tests\System\TokenScanTest */ - public function testHasToken() { + public function providerHasToken() { + return array( + array('%', FALSE), + array('!', FALSE), + array('!', FALSE), + array('% 1', FALSE), + array('! 1', FALSE), + array('!1', TRUE), + array('%1', TRUE), + array(' !12345 ', TRUE), + 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) + ); } }