Issue #1818450 by damiankloip, fastangel: Added Introduce core token support in PluginBase class.
parent
a2ed151844
commit
d0f5482d6d
|
@ -227,4 +227,91 @@ abstract class PluginBase extends ComponentPluginBase {
|
||||||
return $this->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 @@ abstract class AreaPluginBase extends HandlerBase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->globalTokenForm($form, $form_state);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -65,7 +65,7 @@ class Text extends AreaPluginBase {
|
||||||
if ($this->options['tokenize']) {
|
if ($this->options['tokenize']) {
|
||||||
$value = $this->view->style_plugin->tokenize_value($value, 0);
|
$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 @@ class TextCustom extends AreaPluginBase {
|
||||||
if ($this->options['tokenize']) {
|
if ($this->options['tokenize']) {
|
||||||
$value = $this->view->style_plugin->tokenize_value($value, 0);
|
$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 @@ class Title extends AreaPluginBase {
|
||||||
'#type' => 'textfield',
|
'#type' => 'textfield',
|
||||||
'#title' => t('Overridden title'),
|
'#title' => t('Overridden title'),
|
||||||
'#default_value' => $this->options['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) {
|
function render($empty = FALSE) {
|
||||||
if (!empty($this->options['title'])) {
|
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 '';
|
return '';
|
||||||
|
|
|
@ -109,6 +109,43 @@ class AreaTest extends HandlerTestBase {
|
||||||
$this->assertTrue(strpos($output, $empty_string) !== FALSE);
|
$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.
|
* Tests overriding the view title using the area title handler.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -31,12 +31,20 @@ class TestExample extends AreaPluginBase {
|
||||||
return $options;
|
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().
|
* Overrides Drupal\views\Plugin\views\area\AreaPluginBase::render().
|
||||||
*/
|
*/
|
||||||
public function render($empty = FALSE) {
|
public function render($empty = FALSE) {
|
||||||
if (!$empty || !empty($this->options['empty'])) {
|
if (!$empty || !empty($this->options['empty'])) {
|
||||||
return $this->options['string'];
|
return $this->globalTokenReplace($this->options['string']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue