Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
D
drupal
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Custom Issue Tracker
Custom Issue Tracker
Labels
Merge Requests
288
Merge Requests
288
Requirements
Requirements
List
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Analytics
Analytics
Code Review
Insights
Issue
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
project
drupal
Commits
d0f5482d
Commit
d0f5482d
authored
Nov 29, 2012
by
webchick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue
#1818450
by damiankloip, fastangel: Added Introduce core token support in PluginBase class.
parent
a2ed1518
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
143 additions
and
5 deletions
+143
-5
core/modules/views/lib/Drupal/views/Plugin/views/PluginBase.php
...odules/views/lib/Drupal/views/Plugin/views/PluginBase.php
+87
-0
core/modules/views/lib/Drupal/views/Plugin/views/area/AreaPluginBase.php
...ews/lib/Drupal/views/Plugin/views/area/AreaPluginBase.php
+2
-0
core/modules/views/lib/Drupal/views/Plugin/views/area/Text.php
...modules/views/lib/Drupal/views/Plugin/views/area/Text.php
+1
-1
core/modules/views/lib/Drupal/views/Plugin/views/area/TextCustom.php
...s/views/lib/Drupal/views/Plugin/views/area/TextCustom.php
+1
-1
core/modules/views/lib/Drupal/views/Plugin/views/area/Title.php
...odules/views/lib/Drupal/views/Plugin/views/area/Title.php
+6
-2
core/modules/views/lib/Drupal/views/Tests/Handler/AreaTest.php
...modules/views/lib/Drupal/views/Tests/Handler/AreaTest.php
+37
-0
core/modules/views/tests/views_test_data/lib/Drupal/views_test_data/Plugin/views/area/TestExample.php
.../Drupal/views_test_data/Plugin/views/area/TestExample.php
+9
-1
No files found.
core/modules/views/lib/Drupal/views/Plugin/views/PluginBase.php
View file @
d0f5482d
...
...
@@ -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'
),
),
);
}
}
core/modules/views/lib/Drupal/views/Plugin/views/area/AreaPluginBase.php
View file @
d0f5482d
...
...
@@ -141,6 +141,8 @@ public function tokenForm(&$form, &$form_state) {
}
}
}
$this
->
globalTokenForm
(
$form
,
$form_state
);
}
/**
...
...
core/modules/views/lib/Drupal/views/Plugin/views/area/Text.php
View file @
d0f5482d
...
...
@@ -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
);
}
}
...
...
core/modules/views/lib/Drupal/views/Plugin/views/area/TextCustom.php
View file @
d0f5482d
...
...
@@ -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'
);
}
}
...
...
core/modules/views/lib/Drupal/views/Plugin/views/area/Title.php
View file @
d0f5482d
...
...
@@ -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
''
;
...
...
core/modules/views/lib/Drupal/views/Tests/Handler/AreaTest.php
View file @
d0f5482d
...
...
@@ -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.
*/
...
...
core/modules/views/tests/views_test_data/lib/Drupal/views_test_data/Plugin/views/area/TestExample.php
View file @
d0f5482d
...
...
@@ -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'
])
;
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment