From d0f5482d6d3a536be6b93578d3bfc959f95e90be Mon Sep 17 00:00:00 2001 From: webchick Date: Thu, 29 Nov 2012 00:25:05 -0800 Subject: [PATCH] Issue #1818450 by damiankloip, fastangel: Added Introduce core token support in PluginBase class. --- .../Drupal/views/Plugin/views/PluginBase.php | 87 +++++++++++++++++++ .../Plugin/views/area/AreaPluginBase.php | 2 + .../Drupal/views/Plugin/views/area/Text.php | 2 +- .../views/Plugin/views/area/TextCustom.php | 2 +- .../Drupal/views/Plugin/views/area/Title.php | 8 +- .../Drupal/views/Tests/Handler/AreaTest.php | 37 ++++++++ .../Plugin/views/area/TestExample.php | 10 ++- 7 files changed, 143 insertions(+), 5 deletions(-) diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/PluginBase.php b/core/modules/views/lib/Drupal/views/Plugin/views/PluginBase.php index 118eb1c2d27..c045b7d470f 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/PluginBase.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/PluginBase.php @@ -227,4 +227,91 @@ abstract class PluginBase extends ComponentPluginBase { return $this->usesOptions; } + /** + * Returns a string with any core tokens replaced. + * + * @param string $string + * The string to preform the token replacement on. + * @param array $options + * An array of options, as passed to token_replace. + * + * @return string + * The tokenized string. + */ + public function globalTokenReplace($string = '', array $options = array()) { + return token_replace($string, array('view' => $this->view), $options); + } + + /** + * Returns an array of available token replacements. + * + * @param bool $prepared + * Whether to return the raw token info for each token or an array of + * prepared tokens for each type. E.g. "[view:name]". + * @param array $types + * An array of additional token types to return, defaults to 'site' and + * 'view'. + * + * @return array + * An array of available token replacement info or tokens, grouped by type. + */ + public function getAvailableGlobalTokens($prepared = FALSE, array $types = array()) { + $info = token_info(); + // Site and view tokens should always be available. + $types += array('site', 'view'); + $available = array_intersect_key($info['tokens'], array_flip($types)); + + // Construct the token string for each token. + if ($prepared) { + $prepared = array(); + foreach ($available as $type => $tokens) { + foreach (array_keys($tokens) as $token) { + $prepared[$type][] = "[$type:$token]"; + } + } + + return $prepared; + } + + return $available; + } + + /** + * Adds elements for available core tokens to a form. + * + * @param array $form + * The form array to alter, passed by reference. + * @param array $form_state + * The form state array to alter, passed by reference. + */ + public function globalTokenForm(&$form, &$form_state) { + $token_items = array(); + + foreach ($this->getAvailableGlobalTokens() as $type => $tokens) { + $item = array( + '#markup' => $type, + 'children' => array(), + ); + foreach ($tokens as $name => $info) { + $item['children'][$name] = "[$type:$name]" . ' - ' . $info['name'] . ': ' . $info['description']; + } + + $token_items[$type] = $item; + } + + $form['global_tokens'] = array( + '#type' => 'fieldset', + '#title' => t('Available global token replacements'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + ); + $form['global_tokens']['list'] = array( + '#theme' => 'item_list', + '#items' => $token_items, + '#attributes' => array( + 'class' => array('global-tokens'), + ), + ); + } + } diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/area/AreaPluginBase.php b/core/modules/views/lib/Drupal/views/Plugin/views/area/AreaPluginBase.php index a757bcf5730..f1381b5d9ad 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/area/AreaPluginBase.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/area/AreaPluginBase.php @@ -141,6 +141,8 @@ abstract class AreaPluginBase extends HandlerBase { } } } + + $this->globalTokenForm($form, $form_state); } /** diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/area/Text.php b/core/modules/views/lib/Drupal/views/Plugin/views/area/Text.php index 0671ec4f8eb..d96c86e3be7 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/area/Text.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/area/Text.php @@ -65,7 +65,7 @@ class Text extends AreaPluginBase { if ($this->options['tokenize']) { $value = $this->view->style_plugin->tokenize_value($value, 0); } - return check_markup($value, $format, '', FALSE); + return check_markup($this->globalTokenReplace($value), $format, '', FALSE); } } diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/area/TextCustom.php b/core/modules/views/lib/Drupal/views/Plugin/views/area/TextCustom.php index a2700065cbd..b744e2567c6 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/area/TextCustom.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/area/TextCustom.php @@ -60,7 +60,7 @@ class TextCustom extends AreaPluginBase { if ($this->options['tokenize']) { $value = $this->view->style_plugin->tokenize_value($value, 0); } - return $this->sanitizeValue($value, 'xss_admin'); + return $this->sanitizeValue($this->globalTokenReplace($value), 'xss_admin'); } } diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/area/Title.php b/core/modules/views/lib/Drupal/views/Plugin/views/area/Title.php index b0402c4cabc..d59c6139deb 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/area/Title.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/area/Title.php @@ -39,8 +39,11 @@ class Title extends AreaPluginBase { '#type' => 'textfield', '#title' => t('Overridden title'), '#default_value' => $this->options['title'], - '#description' => t('Override the title of this view when it is empty.'), + '#description' => t('Override the title of this view when it is empty. The available global tokens below can be used here.'), ); + + // Don't use the AreaPluginBase tokenForm method, we don't want row tokens. + $this->globalTokenForm($form, $form_state); } /** @@ -48,7 +51,8 @@ class Title extends AreaPluginBase { */ function render($empty = FALSE) { if (!empty($this->options['title'])) { - $this->view->setTitle($this->sanitizeValue($this->options['title'], 'xss_admin'), PASS_THROUGH); + $value = $this->globalTokenReplace($this->options['title']); + $this->view->setTitle($this->sanitizeValue($value, 'xss_admin'), PASS_THROUGH); } return ''; diff --git a/core/modules/views/lib/Drupal/views/Tests/Handler/AreaTest.php b/core/modules/views/lib/Drupal/views/Tests/Handler/AreaTest.php index ff50f64cfc5..68ee28dbfa8 100644 --- a/core/modules/views/lib/Drupal/views/Tests/Handler/AreaTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/Handler/AreaTest.php @@ -109,6 +109,43 @@ class AreaTest extends HandlerTestBase { $this->assertTrue(strpos($output, $empty_string) !== FALSE); } + /** + * Tests global tokens. + */ + public function testRenderAreaToken() { + $admin_user = $this->drupalCreateUser(array('administer views', 'administer site configuration')); + $this->drupalLogin($admin_user); + + $view = views_get_view('test_example_area'); + $view->initHandlers(); + + $this->drupalGet('admin/structure/views/nojs/config-item/test_example_area/default/empty/test_example'); + + // Test that the list is token present. + $element = $this->xpath('//ul[@class="global-tokens"]'); + $this->assertTrue($element, 'Token list found on the options form.'); + + $empty_handler = &$view->empty['test_example']; + + // Test the list of available tokens. + $available = $empty_handler->getAvailableGlobalTokens(); + foreach (array('site', 'view') as $type) { + $this->assertTrue(!empty($available[$type]) && is_array($available[$type])); + // Test that each item exists in the list. + foreach ($available[$type] as $token => $info) { + $this->assertText("[$type:$token]"); + } + } + + // Test the rendered output of a token. + $empty_handler->options['string'] = '[site:name]'; + + // Test we have the site:name token in the output. + $output = $view->preview(); + $expected = token_replace('[site:name]'); + $this->assertTrue(strpos($output, $expected) !== FALSE); + } + /** * Tests overriding the view title using the area title handler. */ diff --git a/core/modules/views/tests/views_test_data/lib/Drupal/views_test_data/Plugin/views/area/TestExample.php b/core/modules/views/tests/views_test_data/lib/Drupal/views_test_data/Plugin/views/area/TestExample.php index 78efe6490cb..952bcd360e8 100644 --- a/core/modules/views/tests/views_test_data/lib/Drupal/views_test_data/Plugin/views/area/TestExample.php +++ b/core/modules/views/tests/views_test_data/lib/Drupal/views_test_data/Plugin/views/area/TestExample.php @@ -31,12 +31,20 @@ class TestExample extends AreaPluginBase { return $options; } + /** + * Overrides Drupal\views\Plugin\views\area\AreaPluginBase::buildOptionsForm() + */ + public function buildOptionsForm(&$form, &$form_state) { + parent::buildOptionsForm($form, $form_state); + $this->globalTokenForm($form, $form_state); + } + /** * Overrides Drupal\views\Plugin\views\area\AreaPluginBase::render(). */ public function render($empty = FALSE) { if (!$empty || !empty($this->options['empty'])) { - return $this->options['string']; + return $this->globalTokenReplace($this->options['string']); } }