Commit d0f5482d authored by webchick's avatar webchick

Issue #1818450 by damiankloip, fastangel: Added Introduce core token support in PluginBase class.

parent a2ed1518
......@@ -227,4 +227,91 @@ public function usesOptions() {
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'),
),
);
}
}
......@@ -141,6 +141,8 @@ public function tokenForm(&$form, &$form_state) {
}
}
}
$this->globalTokenForm($form, $form_state);
}
/**
......
......@@ -65,7 +65,7 @@ function render_textarea($value, $format) {
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);
}
}
......
......@@ -60,7 +60,7 @@ function render_textarea($value) {
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');
}
}
......
......@@ -39,8 +39,11 @@ public function buildOptionsForm(&$form, &$form_state) {
'#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 @@ public function buildOptionsForm(&$form, &$form_state) {
*/
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 '';
......
......@@ -109,6 +109,43 @@ public function testRenderArea() {
$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.
*/
......
......@@ -31,12 +31,20 @@ public function defineOptions() {
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']);
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment