Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
D
drupal
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Insights
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Locked Files
Drupal.org issue queue
Drupal.org issue queue
Security & Compliance
Security & Compliance
Dependency List
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
project
drupal
Commits
c014519c
Commit
c014519c
authored
Apr 18, 2013
by
alexpott
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue
#1969540
by Xano: Convert token.inc to a service.
parent
d5df67d4
Changes
31
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
31 changed files
with
475 additions
and
383 deletions
+475
-383
core/core.services.yml
core/core.services.yml
+3
-0
core/includes/common.inc
core/includes/common.inc
+0
-1
core/includes/token.inc
core/includes/token.inc
+0
-265
core/lib/Drupal.php
core/lib/Drupal.php
+10
-0
core/lib/Drupal/Core/Utility/Token.php
core/lib/Drupal/Core/Utility/Token.php
+309
-0
core/modules/action/action.module
core/modules/action/action.module
+8
-8
core/modules/comment/comment.tokens.inc
core/modules/comment/comment.tokens.inc
+12
-10
core/modules/comment/lib/Drupal/comment/Tests/CommentTokenReplaceTest.php
...ment/lib/Drupal/comment/Tests/CommentTokenReplaceTest.php
+4
-3
core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetBase.php
.../field/lib/Drupal/field/Plugin/Type/Widget/WidgetBase.php
+2
-2
core/modules/file/file.field.inc
core/modules/file/file.field.inc
+4
-3
core/modules/file/lib/Drupal/file/Tests/FileFieldPathTest.php
.../modules/file/lib/Drupal/file/Tests/FileFieldPathTest.php
+1
-1
core/modules/file/lib/Drupal/file/Tests/FileTokenReplaceTest.php
...dules/file/lib/Drupal/file/Tests/FileTokenReplaceTest.php
+3
-2
core/modules/link/link.module
core/modules/link/link.module
+1
-1
core/modules/node/lib/Drupal/node/Tests/NodeTokenReplaceTest.php
...dules/node/lib/Drupal/node/Tests/NodeTokenReplaceTest.php
+5
-4
core/modules/node/node.tokens.inc
core/modules/node/node.tokens.inc
+8
-6
core/modules/statistics/lib/Drupal/statistics/Tests/StatisticsTokenReplaceTest.php
...ib/Drupal/statistics/Tests/StatisticsTokenReplaceTest.php
+1
-1
core/modules/statistics/statistics.tokens.inc
core/modules/statistics/statistics.tokens.inc
+4
-2
core/modules/system/lib/Drupal/system/Tests/System/TokenReplaceTest.php
...ystem/lib/Drupal/system/Tests/System/TokenReplaceTest.php
+18
-13
core/modules/system/lib/Drupal/system/Tests/System/TokenScanTest.php
...s/system/lib/Drupal/system/Tests/System/TokenScanTest.php
+1
-1
core/modules/system/system.api.php
core/modules/system/system.api.php
+28
-22
core/modules/system/system.module
core/modules/system/system.module
+4
-2
core/modules/system/system.tokens.inc
core/modules/system/system.tokens.inc
+7
-5
core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TokenReplaceTest.php
...s/taxonomy/lib/Drupal/taxonomy/Tests/TokenReplaceTest.php
+6
-5
core/modules/taxonomy/taxonomy.tokens.inc
core/modules/taxonomy/taxonomy.tokens.inc
+6
-4
core/modules/user/lib/Drupal/user/Tests/UserTokenReplaceTest.php
...dules/user/lib/Drupal/user/Tests/UserTokenReplaceTest.php
+6
-5
core/modules/user/user.module
core/modules/user/user.module
+7
-5
core/modules/user/user.tokens.inc
core/modules/user/user.tokens.inc
+7
-5
core/modules/views/lib/Drupal/views/Plugin/views/PluginBase.php
...odules/views/lib/Drupal/views/Plugin/views/PluginBase.php
+3
-3
core/modules/views/lib/Drupal/views/Tests/Handler/AreaTest.php
...modules/views/lib/Drupal/views/Tests/Handler/AreaTest.php
+1
-1
core/modules/views/lib/Drupal/views/Tests/TokenReplaceTest.php
...modules/views/lib/Drupal/views/Tests/TokenReplaceTest.php
+2
-1
core/modules/views/views.tokens.inc
core/modules/views/views.tokens.inc
+4
-2
No files found.
core/core.services.yml
View file @
c014519c
...
...
@@ -390,3 +390,6 @@ services:
class
:
Drupal\system\Plugin\ImageToolkitInterface
factory_method
:
getDefaultToolkit
factory_service
:
image.toolkit.manager
token
:
class
:
Drupal\Core\Utility\Token
arguments
:
[
'
@module_handler'
]
core/includes/common.inc
View file @
c014519c
...
...
@@ -4569,7 +4569,6 @@ function _drupal_bootstrap_code() {
require_once
DRUPAL_ROOT
.
'/core/includes/form.inc'
;
require_once
DRUPAL_ROOT
.
'/core/includes/mail.inc'
;
require_once
DRUPAL_ROOT
.
'/core/includes/ajax.inc'
;
require_once
DRUPAL_ROOT
.
'/core/includes/token.inc'
;
require_once
DRUPAL_ROOT
.
'/core/includes/errors.inc'
;
require_once
DRUPAL_ROOT
.
'/core/includes/schema.inc'
;
require_once
DRUPAL_ROOT
.
'/core/includes/entity.inc'
;
...
...
core/includes/token.inc
deleted
100644 → 0
View file @
d5df67d4
<?php
/**
* @file
* Drupal placeholder/token replacement system.
*
* API functions for replacing placeholders in text with meaningful values.
*
* For example: When configuring automated emails, an administrator enters
* standard text for the email. Variables like the title of a node and the date
* the email was sent can be entered as placeholders like [node:title] and
* [date:short]. When a Drupal module prepares to send the email, it can call
* the token_replace() function, passing in the text. The token system will
* scan the text for placeholder tokens, give other modules an opportunity to
* replace them with meaningful text, then return the final product to the
* original module.
*
* Tokens follow the form: [$type:$name], where $type is a general class of
* tokens like 'node', 'user', or 'comment' and $name is the name of a given
* placeholder. For example, [node:title] or [node:created:since].
*
* In addition to raw text containing placeholders, modules may pass in an array
* of objects to be used when performing the replacement. The objects should be
* keyed by the token type they correspond to. For example:
*
* @code
* // Load a node and a user, then replace tokens in the text.
* $text = 'On [date:short], [user:name] read [node:title].';
* $node = node_load(1);
* $user = user_load(1);
*
* // [date:...] tokens use the current date automatically.
* $data = array('node' => $node, 'user' => $user);
* return token_replace($text, $data);
* @endcode
*
* Some tokens may be chained in the form of [$type:$pointer:$name], where $type
* is a normal token type, $pointer is a reference to another token type, and
* $name is the name of a given placeholder. For example, [node:author:mail]. In
* that example, 'author' is a pointer to the 'user' account that created the
* node, and 'mail' is a placeholder available for any 'user'.
*
* @see token_replace()
* @see hook_tokens()
* @see hook_token_info()
*/
/**
* Replaces all tokens in a given string with appropriate values.
*
* @param string $text
* A string potentially containing replaceable tokens.
* @param array $data
* (optional) An array of keyed objects. For simple replacement scenarios
* 'node', 'user', and others are common keys, with an accompanying node or
* user object being the value. Some token types, like 'site', do not require
* any explicit information from $data and can be replaced even if it is
* empty.
* @param array $options
* (optional) A keyed array of settings and flags to control the token
* replacement process. Supported options are:
* - langcode: A language code to be used when generating locale-sensitive
* tokens.
* - callback: A callback function that will be used to post-process the
* array of token replacements after they are generated. For example, a
* module using tokens in a text-only email might provide a callback to
* strip HTML entities from token values before they are inserted into the
* final text.
* - clear: A boolean flag indicating that tokens should be removed from the
* final text if no replacement value can be generated.
* - sanitize: A boolean flag indicating that tokens should be sanitized for
* display to a web browser. Defaults to TRUE. Developers who set this
* option to FALSE assume responsibility for running filter_xss(),
* check_plain() or other appropriate scrubbing functions before displaying
* data to users.
*
* @return string
* Text with tokens replaced.
*/
function
token_replace
(
$text
,
array
$data
=
array
(),
array
$options
=
array
())
{
$text_tokens
=
token_scan
(
$text
);
if
(
empty
(
$text_tokens
))
{
return
$text
;
}
$replacements
=
array
();
foreach
(
$text_tokens
as
$type
=>
$tokens
)
{
$replacements
+=
token_generate
(
$type
,
$tokens
,
$data
,
$options
);
if
(
!
empty
(
$options
[
'clear'
]))
{
$replacements
+=
array_fill_keys
(
$tokens
,
''
);
}
}
// Optionally alter the list of replacement values.
if
(
!
empty
(
$options
[
'callback'
]))
{
$function
=
$options
[
'callback'
];
$function
(
$replacements
,
$data
,
$options
);
}
$tokens
=
array_keys
(
$replacements
);
$values
=
array_values
(
$replacements
);
return
str_replace
(
$tokens
,
$values
,
$text
);
}
/**
* Builds a list of all token-like patterns that appear in the text.
*
* @param string $text
* The text to be scanned for possible tokens.
*
* @return array
* An associative array of discovered tokens, grouped by type.
*/
function
token_scan
(
$text
)
{
// Matches tokens with the following pattern: [$type:$name]
// $type and $name may not contain [ ] characters.
// $type may not contain : or whitespace characters, but $name may.
preg_match_all
(
'/
\[ # [ - pattern start
([^\s\[\]:]*) # match $type not containing whitespace : [ or ]
: # : - separator
([^\[\]]*) # match $name not containing [ or ]
\] # ] - pattern end
/x'
,
$text
,
$matches
);
$types
=
$matches
[
1
];
$tokens
=
$matches
[
2
];
// Iterate through the matches, building an associative array containing
// $tokens grouped by $types, pointing to the version of the token found in
// the source text. For example, $results['node']['title'] = '[node:title]';
$results
=
array
();
for
(
$i
=
0
;
$i
<
count
(
$tokens
);
$i
++
)
{
$results
[
$types
[
$i
]][
$tokens
[
$i
]]
=
$matches
[
0
][
$i
];
}
return
$results
;
}
/**
* Generates replacement values for a list of tokens.
*
* @param string $type
* The type of token being replaced. 'node', 'user', and 'date' are common.
* @param array $tokens
* An array of tokens to be replaced, keyed by the literal text of the token
* as it appeared in the source text.
* @param array $data
* (optional) An array of keyed objects. For simple replacement scenarios
* 'node', 'user', and others are common keys, with an accompanying node or
* user object being the value. Some token types, like 'site', do not require
* any explicit information from $data and can be replaced even if it is
* empty.
* @param array $options
* (optional) A keyed array of settings and flags to control the token
* replacement process. Supported options are:
* - langcode: A language code to be used when generating locale-sensitive
* tokens.
* - callback: A callback function that will be used to post-process the
* array of token replacements after they are generated. Can be used when
* modules require special formatting of token text, for example URL
* encoding or truncation to a specific length.
* - sanitize: A boolean flag indicating that tokens should be sanitized for
* display to a web browser. Developers who set this option to FALSE assume
* responsibility for running filter_xss(), check_plain() or other
* appropriate scrubbing functions before displaying data to users.
*
* @return array
* An associative array of replacement values, keyed by the original 'raw'
* tokens that were found in the source text. For example:
* $results['[node:title]'] = 'My new node';
*
* @see hook_tokens()
* @see hook_tokens_alter()
*/
function
token_generate
(
$type
,
array
$tokens
,
array
$data
=
array
(),
array
$options
=
array
())
{
$options
+=
array
(
'sanitize'
=>
TRUE
);
$replacements
=
module_invoke_all
(
'tokens'
,
$type
,
$tokens
,
$data
,
$options
);
// Allow other modules to alter the replacements.
$context
=
array
(
'type'
=>
$type
,
'tokens'
=>
$tokens
,
'data'
=>
$data
,
'options'
=>
$options
,
);
drupal_alter
(
'tokens'
,
$replacements
,
$context
);
return
$replacements
;
}
/**
* Returns a list of tokens that begin with a specific prefix.
*
* Used to extract a group of 'chained' tokens (such as [node:author:name])
* from the full list of tokens found in text. For example:
* @code
* $data = array(
* 'author:name' => '[node:author:name]',
* 'title' => '[node:title]',
* 'created' => '[node:created]',
* );
* $results = token_find_with_prefix($data, 'author');
* $results == array('name' => '[node:author:name]');
* @endcode
*
* @param array $tokens
* A keyed array of tokens, and their original raw form in the source text.
* @param string $prefix
* A textual string to be matched at the beginning of the token.
* @param string $delimiter
* (optional) A string containing the character that separates the prefix from
* the rest of the token. Defaults to ':'.
*
* @return array
* An associative array of discovered tokens, with the prefix and delimiter
* stripped from the key.
*/
function
token_find_with_prefix
(
array
$tokens
,
$prefix
,
$delimiter
=
':'
)
{
$results
=
array
();
foreach
(
$tokens
as
$token
=>
$raw
)
{
$parts
=
explode
(
$delimiter
,
$token
,
2
);
if
(
count
(
$parts
)
==
2
&&
$parts
[
0
]
==
$prefix
)
{
$results
[
$parts
[
1
]]
=
$raw
;
}
}
return
$results
;
}
/**
* Returns metadata describing supported tokens.
*
* The metadata array contains token type, name, and description data as well
* as an optional pointer indicating that the token chains to another set of
* tokens.
*
* For example:
* @code
* $data['types']['node'] = array(
* 'name' => t('Nodes'),
* 'description' => t('Tokens related to node objects.'),
* );
* $data['tokens']['node']['title'] = array(
* 'name' => t('Title'),
* 'description' => t('The title of the current node.'),
* );
* $data['tokens']['node']['author'] = array(
* 'name' => t('Author'),
* 'description' => t('The author of the current node.'),
* 'type' => 'user',
* );
* @endcode
*
* @return array
* An associative array of token information, grouped by token type.
*/
function
token_info
()
{
$data
=
&
drupal_static
(
__FUNCTION__
);
if
(
!
isset
(
$data
))
{
$data
=
module_invoke_all
(
'token_info'
);
drupal_alter
(
'token_info'
,
$data
);
}
return
$data
;
}
core/lib/Drupal.php
View file @
c014519c
...
...
@@ -341,4 +341,14 @@ public static function typedData() {
return
static
::
$container
->
get
(
'typed_data'
);
}
/**
* Returns the token service.
*
* @return \Drupal\Core\Utility\Token
* The token service.
*/
public
static
function
token
()
{
return
static
::
$container
->
get
(
'token'
);
}
}
core/lib/Drupal/Core/Utility/Token.php
0 → 100644
View file @
c014519c
This diff is collapsed.
Click to expand it.
core/modules/action/action.module
View file @
c014519c
...
...
@@ -564,11 +564,11 @@ function action_send_email_action_submit($form, $form_state) {
* @param array $context
* Array with the following elements:
* - 'recipient': E-mail message recipient. This will be passed through
*
token_
replace().
*
\Drupal\Core\Utility\Token::
replace().
* - 'subject': The subject of the message. This will be passed through
*
token_
replace().
*
\Drupal\Core\Utility\Token::
replace().
* - 'message': The message to send. This will be passed through
*
token_
replace().
*
\Drupal\Core\Utility\Token::
replace().
* - Other elements will be used as the data for token replacement.
*
* @ingroup actions
...
...
@@ -578,7 +578,7 @@ function action_send_email_action($entity, $context) {
$context
[
'node'
]
=
$entity
;
}
$recipient
=
token_
replace
(
$context
[
'recipient'
],
$context
);
$recipient
=
Drupal
::
token
()
->
replace
(
$context
[
'recipient'
],
$context
);
// If the recipient is a registered user with a language preference, use
// the recipient's preferred language. Otherwise, use the system default
...
...
@@ -633,7 +633,7 @@ function action_message_action_submit($form, $form_state) {
* @param array $context
* Array with the following elements:
* - 'message': The message to send. This will be passed through
*
token_
replace().
*
\Drupal\Core\Utility\Token::
replace().
* - Other elements will be used as the data for token replacement in
* the message.
*
...
...
@@ -644,7 +644,7 @@ function action_message_action(&$entity, $context = array()) {
$context
[
'node'
]
=
$entity
;
}
$context
[
'message'
]
=
token_
replace
(
filter_xss_admin
(
$context
[
'message'
]),
$context
);
$context
[
'message'
]
=
Drupal
::
token
()
->
replace
(
filter_xss_admin
(
$context
[
'message'
]),
$context
);
drupal_set_message
(
$context
[
'message'
]);
}
...
...
@@ -685,11 +685,11 @@ function action_goto_action_submit($form, $form_state) {
* @param array $context
* Array with the following elements:
* - 'url': URL to redirect to. This will be passed through
*
token_
replace().
*
\Drupal\Core\Utility\Token::
replace().
* - Other elements will be used as the data for token replacement.
*
* @ingroup actions.
*/
function
action_goto_action
(
$entity
,
$context
)
{
drupal_goto
(
token_
replace
(
$context
[
'url'
],
$context
));
drupal_goto
(
Drupal
::
token
()
->
replace
(
$context
[
'url'
],
$context
));
}
core/modules/comment/comment.tokens.inc
View file @
c014519c
...
...
@@ -103,6 +103,8 @@ function comment_token_info() {
* Implements hook_tokens().
*/
function
comment_tokens
(
$type
,
$tokens
,
array
$data
=
array
(),
array
$options
=
array
())
{
$token_service
=
Drupal
::
token
();
$url_options
=
array
(
'absolute'
=>
TRUE
);
if
(
isset
(
$options
[
'langcode'
]))
{
$url_options
[
'language'
]
=
language_load
(
$options
[
'langcode'
]);
...
...
@@ -198,25 +200,25 @@ function comment_tokens($type, $tokens, array $data = array(), array $options =
}
// Chained token relationships.
if
(
$node_tokens
=
token_find_with_p
refix
(
$tokens
,
'node'
))
{
if
(
$node_tokens
=
$token_service
->
findwithP
refix
(
$tokens
,
'node'
))
{
$node
=
$comment
->
nid
->
entity
;
$replacements
+=
token_
generate
(
'node'
,
$node_tokens
,
array
(
'node'
=>
$node
),
$options
);
$replacements
+=
$token_service
->
generate
(
'node'
,
$node_tokens
,
array
(
'node'
=>
$node
),
$options
);
}
if
(
$date_tokens
=
token_find_with_p
refix
(
$tokens
,
'created'
))
{
$replacements
+=
token_
generate
(
'date'
,
$date_tokens
,
array
(
'date'
=>
$comment
->
created
->
value
),
$options
);
if
(
$date_tokens
=
$token_service
->
findwithP
refix
(
$tokens
,
'created'
))
{
$replacements
+=
$token_service
->
generate
(
'date'
,
$date_tokens
,
array
(
'date'
=>
$comment
->
created
->
value
),
$options
);
}
if
(
$date_tokens
=
token_find_with_p
refix
(
$tokens
,
'changed'
))
{
$replacements
+=
token_
generate
(
'date'
,
$date_tokens
,
array
(
'date'
=>
$comment
->
changed
->
value
),
$options
);
if
(
$date_tokens
=
$token_service
->
findwithP
refix
(
$tokens
,
'changed'
))
{
$replacements
+=
$token_service
->
generate
(
'date'
,
$date_tokens
,
array
(
'date'
=>
$comment
->
changed
->
value
),
$options
);
}
if
((
$parent_tokens
=
token_find_with_p
refix
(
$tokens
,
'parent'
))
&&
$parent
=
$comment
->
pid
->
entity
)
{
$replacements
+=
token_
generate
(
'comment'
,
$parent_tokens
,
array
(
'comment'
=>
$parent
),
$options
);
if
((
$parent_tokens
=
$token_service
->
findwithP
refix
(
$tokens
,
'parent'
))
&&
$parent
=
$comment
->
pid
->
entity
)
{
$replacements
+=
$token_service
->
generate
(
'comment'
,
$parent_tokens
,
array
(
'comment'
=>
$parent
),
$options
);
}
if
((
$author_tokens
=
token_find_with_p
refix
(
$tokens
,
'author'
))
&&
$account
=
$comment
->
uid
->
entity
)
{
$replacements
+=
token_
generate
(
'user'
,
$author_tokens
,
array
(
'user'
=>
$account
),
$options
);
if
((
$author_tokens
=
$token_service
->
findwithP
refix
(
$tokens
,
'author'
))
&&
$account
=
$comment
->
uid
->
entity
)
{
$replacements
+=
$token_service
->
generate
(
'user'
,
$author_tokens
,
array
(
'user'
=>
$account
),
$options
);
}
}
elseif
(
$type
==
'node'
&
!
empty
(
$data
[
'node'
]))
{
...
...
core/modules/comment/lib/Drupal/comment/Tests/CommentTokenReplaceTest.php
View file @
c014519c
...
...
@@ -23,6 +23,7 @@ public static function getInfo() {
* Creates a comment, then tests the tokens generated from it.
*/
function
testCommentTokenReplacement
()
{
$token_service
=
\Drupal
::
token
();
$language_interface
=
language
(
LANGUAGE_TYPE_INTERFACE
);
$url_options
=
array
(
'absolute'
=>
TRUE
,
...
...
@@ -72,7 +73,7 @@ function testCommentTokenReplacement() {
$this
->
assertFalse
(
in_array
(
0
,
array_map
(
'strlen'
,
$tests
)),
'No empty tokens generated.'
);
foreach
(
$tests
as
$input
=>
$expected
)
{
$output
=
token_
replace
(
$input
,
array
(
'comment'
=>
$comment
),
array
(
'langcode'
=>
$language_interface
->
langcode
));
$output
=
$token_service
->
replace
(
$input
,
array
(
'comment'
=>
$comment
),
array
(
'langcode'
=>
$language_interface
->
langcode
));
$this
->
assertEqual
(
$output
,
$expected
,
format_string
(
'Sanitized comment token %token replaced.'
,
array
(
'%token'
=>
$input
)));
}
...
...
@@ -88,7 +89,7 @@ function testCommentTokenReplacement() {
$tests
[
'[comment:author:name]'
]
=
$this
->
admin_user
->
name
;
foreach
(
$tests
as
$input
=>
$expected
)
{
$output
=
token_
replace
(
$input
,
array
(
'comment'
=>
$comment
),
array
(
'langcode'
=>
$language_interface
->
langcode
,
'sanitize'
=>
FALSE
));
$output
=
$token_service
->
replace
(
$input
,
array
(
'comment'
=>
$comment
),
array
(
'langcode'
=>
$language_interface
->
langcode
,
'sanitize'
=>
FALSE
));
$this
->
assertEqual
(
$output
,
$expected
,
format_string
(
'Unsanitized comment token %token replaced.'
,
array
(
'%token'
=>
$input
)));
}
...
...
@@ -101,7 +102,7 @@ function testCommentTokenReplacement() {
$tests
[
'[node:comment-count-new]'
]
=
2
;
foreach
(
$tests
as
$input
=>
$expected
)
{
$output
=
token_
replace
(
$input
,
array
(
'node'
=>
$node
),
array
(
'langcode'
=>
$language_interface
->
langcode
));
$output
=
$token_service
->
replace
(
$input
,
array
(
'node'
=>
$node
),
array
(
'langcode'
=>
$language_interface
->
langcode
));
$this
->
assertEqual
(
$output
,
$expected
,
format_string
(
'Node comment token %token replaced.'
,
array
(
'%token'
=>
$input
)));
}
}
...
...
core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetBase.php
View file @
c014519c
...
...
@@ -105,7 +105,7 @@ public function form(EntityInterface $entity, $langcode, array $items, array &$f
$delta
=
isset
(
$get_delta
)
?
$get_delta
:
0
;
$element
=
array
(
'#title'
=>
check_plain
(
$instance
[
'label'
]),
'#description'
=>
field_filter_xss
(
token_
replace
(
$instance
[
'description'
])),
'#description'
=>
field_filter_xss
(
\Drupal
::
token
()
->
replace
(
$instance
[
'description'
])),
);
$element
=
$this
->
formSingleElement
(
$entity
,
$items
,
$delta
,
$langcode
,
$element
,
$form
,
$form_state
);
...
...
@@ -196,7 +196,7 @@ protected function formMultipleElements(EntityInterface $entity, array $items, $
$wrapper_id
=
drupal_html_id
(
$id_prefix
.
'-add-more-wrapper'
);
$title
=
check_plain
(
$instance
[
'label'
]);
$description
=
field_filter_xss
(
token_
replace
(
$instance
[
'description'
]));
$description
=
field_filter_xss
(
\Drupal
::
token
()
->
replace
(
$instance
[
'description'
]));
$elements
=
array
();
...
...
core/modules/file/file.field.inc
View file @
c014519c
...
...
@@ -352,18 +352,19 @@ function file_field_widget_upload_validators($field, $instance) {
* @param $instance
* A field instance array.
* @param $data
* An array of token objects to pass to token_replace().
* An array of token objects to pass to
* \Drupal\Core\Utility\Token::replace().
*
* @return
* A file directory URI with tokens replaced.
*
* @see
token_
replace()
* @see
\Drupal\Core\Utility\Token::
replace()
*/
function
file_field_widget_uri
(
$field
,
$instance
,
$data
=
array
())
{
$destination
=
trim
(
$instance
[
'settings'
][
'file_directory'
],
'/'
);
// Replace tokens.
$destination
=
token_
replace
(
$destination
,
$data
);
$destination
=
Drupal
::
token
()
->
replace
(
$destination
,
$data
);
return
$field
[
'settings'
][
'uri_scheme'
]
.
'://'
.
$destination
;
}
...
...
core/modules/file/lib/Drupal/file/Tests/FileFieldPathTest.php
View file @
c014519c
...
...
@@ -60,7 +60,7 @@ function testUploadPath() {
// Do token replacement using the same user which uploaded the file, not
// the user running the test case.
$data
=
array
(
'user'
=>
$this
->
admin_user
);
$subdirectory
=
token_
replace
(
'[user:uid]/[user:name]'
,
$data
);
$subdirectory
=
\Drupal
::
token
()
->
replace
(
'[user:uid]/[user:name]'
,
$data
);
$this
->
assertPathMatch
(
'public://'
.
$subdirectory
.
'/'
.
$test_file
->
filename
,
$node_file
->
uri
,
t
(
'The file %file was uploaded to the correct path with token replacements.'
,
array
(
'%file'
=>
$node_file
->
uri
)));
}
...
...
core/modules/file/lib/Drupal/file/Tests/FileTokenReplaceTest.php
View file @
c014519c
...
...
@@ -23,6 +23,7 @@ public static function getInfo() {
* Creates a file, then tests the tokens generated from it.
*/
function
testFileTokenReplacement
()
{
$token_service
=
\Drupal
::
token
();
$language_interface
=
language
(
LANGUAGE_TYPE_INTERFACE
);
$url_options
=
array
(
'absolute'
=>
TRUE
,
...
...
@@ -65,7 +66,7 @@ function testFileTokenReplacement() {
$this
->
assertFalse
(
in_array
(
0
,
array_map
(
'strlen'
,
$tests
)),
t
(
'No empty tokens generated.'
));
foreach
(
$tests
as
$input
=>
$expected
)
{
$output
=
token_
replace
(
$input
,
array
(
'file'
=>
$file
),
array
(
'langcode'
=>
$language_interface
->
langcode
));
$output
=
$token_service
->
replace
(
$input
,
array
(
'file'
=>
$file
),
array
(
'langcode'
=>
$language_interface
->
langcode
));
$this
->
assertEqual
(
$output
,
$expected
,
t
(
'Sanitized file token %token replaced.'
,
array
(
'%token'
=>
$input
)));
}
...
...
@@ -76,7 +77,7 @@ function testFileTokenReplacement() {
$tests
[
'[file:size]'
]
=
format_size
(
$file
->
filesize
);
foreach
(
$tests
as
$input
=>
$expected
)
{
$output
=
token_
replace
(
$input
,
array
(
'file'
=>
$file
),
array
(
'langcode'
=>
$language_interface
->
langcode
,
'sanitize'
=>
FALSE
));
$output
=
$token_service
->
replace
(
$input
,
array
(
'file'
=>
$file
),
array
(
'langcode'
=>
$language_interface
->
langcode
,
'sanitize'
=>
FALSE
));
$this
->
assertEqual
(
$output
,
$expected
,
t
(
'Unsanitized file token %token replaced.'
,
array
(
'%token'
=>
$input
)));
}
}
...
...
core/modules/link/link.module
View file @
c014519c
...
...
@@ -362,7 +362,7 @@ function link_field_formatter_view(EntityInterface $entity, $field, $instance, $
if
(
empty
(
$settings
[
'url_only'
])
&&
!
empty
(
$item
[
'title'
]))
{
// Unsanitizied token replacement here because $options['html'] is FALSE
// by default in theme_link().
$link_title
=
token_
replace
(
$item
[
'title'
],
array
(
$entity
->
entityType
()
=>
$entity
),
array
(
'sanitize'
=>
FALSE
,
'clear'
=>
TRUE
));
$link_title
=
Drupal
::
token
()
->
replace
(
$item
[
'title'
],
array
(
$entity
->
entityType
()
=>
$entity
),
array
(
'sanitize'
=>
FALSE
,
'clear'
=>
TRUE
));
}
// Trim the link title to the desired length.
...
...
core/modules/node/lib/Drupal/node/Tests/NodeTokenReplaceTest.php
View file @
c014519c
...
...
@@ -23,6 +23,7 @@ public static function getInfo() {
* Creates a node, then tests the tokens generated from it.
*/
function
testNodeTokenReplacement
()
{
$token_service
=
\Drupal
::
token
();
$language_interface
=
language
(
LANGUAGE_TYPE_INTERFACE
);
$url_options
=
array
(
'absolute'
=>
TRUE
,
...
...
@@ -66,7 +67,7 @@ function testNodeTokenReplacement() {
$this
->
assertFalse
(
in_array
(
0
,
array_map
(
'strlen'
,
$tests
)),
'No empty tokens generated.'
);
foreach
(
$tests
as
$input
=>
$expected
)
{
$output
=
token_
replace
(
$input
,
array
(
'node'
=>
$node
),
array
(
'langcode'
=>
$language_interface
->
langcode
));
$output
=
$token_service
->
replace
(
$input
,
array
(
'node'
=>
$node
),
array
(
'langcode'
=>
$language_interface
->
langcode
));
$this
->
assertEqual
(
$output
,
$expected
,
format_string
(
'Sanitized node token %token replaced.'
,
array
(
'%token'
=>
$input
)));
}
...
...
@@ -78,7 +79,7 @@ function testNodeTokenReplacement() {
$tests
[
'[node:author:name]'
]
=
user_format_name
(
$account
);
foreach
(
$tests
as
$input
=>
$expected
)
{
$output
=
token_
replace
(
$input
,
array
(
'node'
=>
$node
),
array
(
'langcode'
=>
$language_interface
->
langcode
,
'sanitize'
=>
FALSE
));
$output
=
$token_service
->
replace
(
$input
,
array
(
'node'
=>
$node
),
array
(
'langcode'
=>
$language_interface
->
langcode
,
'sanitize'
=>
FALSE
));
$this
->
assertEqual
(
$output
,
$expected
,
format_string
(
'Unsanitized node token %token replaced.'
,
array
(
'%token'
=>
$input
)));
}
...
...
@@ -99,7 +100,7 @@ function testNodeTokenReplacement() {
$this
->
assertFalse
(
in_array
(
0
,
array_map
(
'strlen'
,
$tests
)),
'No empty tokens generated for node without a summary.'
);
foreach
(
$tests
as
$input
=>
$expected
)
{
$output
=
token_
replace
(
$input
,
array
(
'node'
=>
$node
),
array
(
'language'
=>
$language_interface
));
$output
=
$token_service
->
replace
(
$input
,
array
(
'node'
=>
$node
),
array
(
'language'
=>
$language_interface
));
$this
->
assertEqual
(
$output
,
$expected
,
format_string
(
'Sanitized node token %token replaced for node without a summary.'
,
array
(
'%token'
=>
$input
)));
}
...
...
@@ -107,7 +108,7 @@ function testNodeTokenReplacement() {
$tests
[
'[node:summary]'
]
=
$node
->
body
[
$node
->
langcode
][
0
][
'value'
];
foreach
(
$tests
as
$input
=>
$expected
)
{
$output
=
token_
replace
(
$input
,
array
(
'node'
=>
$node
),
array
(
'language'
=>
$language_interface
,
'sanitize'
=>
FALSE
));
$output
=
$token_service
->
replace
(
$input
,
array
(
'node'
=>
$node
),
array
(
'language'
=>
$language_interface
,
'sanitize'
=>
FALSE
));
$this
->
assertEqual
(
$output
,
$expected
,
format_string
(
'Unsanitized node token %token replaced for node without a summary.'
,
array
(
'%token'
=>
$input
)));
}
}
...
...
core/modules/node/node.tokens.inc
View file @
c014519c
...
...
@@ -90,6 +90,8 @@ function node_token_info() {
* Implements hook_tokens().
*/
function
node_tokens
(
$type
,
$tokens
,
array
$data
=
array
(),
array
$options
=
array
())
{
$token_service
=
Drupal
::
token
();
$url_options
=
array
(
'absolute'
=>
TRUE
);
if
(
isset
(
$options
[
'langcode'
]))
{
$url_options
[
'language'
]
=
language_load
(
$options
[
'langcode'
]);
...
...
@@ -197,17 +199,17 @@ function node_tokens($type, $tokens, array $data = array(), array $options = arr
}
}
if
(
$author_tokens
=
token_find_with_p
refix
(
$tokens
,
'author'
))
{
if
(
$author_tokens
=
$token_service
->
findWithP
refix
(
$tokens
,
'author'
))
{
$author
=
user_load
(
$node
->
uid
);
$replacements
+=
token_
generate
(
'user'
,
$author_tokens
,
array
(
'user'
=>
$author
),
$options
);
$replacements
+=
$token_service
->
generate
(
'user'
,
$author_tokens
,
array
(
'user'
=>
$author
),
$options
);
}
if
(
$created_tokens
=
token_find_with_p
refix
(
$tokens
,
'created'
))
{
$replacements
+=
token_
generate
(
'date'
,
$created_tokens
,
array
(
'date'
=>
$node
->
created
),
$options
);
if
(
$created_tokens
=
$token_service
->
findWithP
refix
(
$tokens
,
'created'
))
{
$replacements
+=
$token_service
->
generate
(
'date'
,
$created_tokens
,
array
(
'date'
=>
$node
->
created
),
$options
);
}
if
(
$changed_tokens
=
token_find_with_p
refix
(
$tokens
,
'changed'
))
{
$replacements
+=
token_
generate
(
'date'
,
$changed_tokens
,
array
(
'date'
=>
$node
->
changed
),
$options
);
if
(
$changed_tokens
=
$token_service
->
findWithP
refix
(
$tokens
,
'changed'
))
{
$replacements
+=
$token_service
->
generate
(
'date'
,
$changed_tokens
,
array
(
'date'
=>
$node
->
changed
),
$options
);
}
}
...
...
core/modules/statistics/lib/Drupal/statistics/Tests/StatisticsTokenReplaceTest.php
View file @
c014519c
...
...
@@ -54,7 +54,7 @@ function testStatisticsTokenReplacement() {
$this
->
assertFalse
(
in_array
(
0
,
array_map
(
'strlen'
,
$tests
)),
'No empty tokens generated.'
);
foreach
(
$tests
as
$input
=>
$expected
)
{
$output
=
token_
replace
(
$input
,
array
(
'node'
=>
$node
),
array
(
'langcode'
=>
$language_interface
->
langcode
));
$output
=
\Drupal
::
token
()
->
replace
(
$input
,
array
(
'node'
=>
$node
),
array
(
'langcode'
=>
$language_interface
->
langcode
));
$this
->
assertEqual
(
$output
,
$expected
,
format_string
(
'Statistics token %token replaced.'
,
array
(
'%token'
=>
$input
)));
}
}
...
...
core/modules/statistics/statistics.tokens.inc
View file @
c014519c
...
...
@@ -32,6 +32,8 @@ function statistics_token_info() {
* Implements hook_tokens().
*/
function
statistics_tokens
(
$type
,
$tokens
,
array
$data
=
array
(),
array
$options
=
array
())
{
$token_service
=
Drupal
::
token
();
$url_options
=
array
(
'absolute'
=>
TRUE
);
$replacements
=
array
();
...
...
@@ -53,9 +55,9 @@ function statistics_tokens($type, $tokens, array $data = array(), array $options
}
}
if
(
$created_tokens
=
token_find_with_p
refix
(
$tokens
,
'last-view'
))
{
if
(
$created_tokens
=
$token_service
->
findWithP
refix
(
$tokens
,
'last-view'
))
{
$statistics
=
statistics_get
(
$node
->
nid
);
$replacements
+=
token_
generate
(
'date'
,
$created_tokens
,
array
(
'date'
=>
$statistics
[
'timestamp'
]),
$options
);
$replacements
+=
$token_service
->
generate
(
'date'
,
$created_tokens
,
array
(
'date'
=>
$statistics
[
'timestamp'
]),
$options
);
}
}
...
...
core/modules/system/lib/Drupal/system/Tests/System/TokenReplaceTest.php
View file @
c014519c
...
...
@@ -25,6 +25,8 @@ public static function getInfo() {
* Creates a user and a node, then tests the tokens generated from them.
*/
function
testTokenReplacement
()
{
$token_service
=
\Drupal
::
token
();
// Create the initial objects.
$account
=
$this
->
drupalCreateUser
();
$node
=
$this
->
drupalCreateNode
(
array
(
'uid'
=>
$account
->
uid
));
...
...
@@ -47,34 +49,35 @@ function testTokenReplacement() {
$target
.=
format_date
(
REQUEST_TIME
,
'short'
,
''
,
NULL
,
$language_interface
->
langcode
);
// Test that the clear parameter cleans out non-existent tokens.
$result
=
token_
replace
(
$source
,
array
(
'node'
=>
$node
),
array
(
'langcode'
=>
$language_interface
->
langcode
,
'clear'
=>
TRUE
));
$result
=
$token_service
->
replace
(
$source
,
array
(
'node'
=>
$node
),
array
(
'langcode'
=>
$language_interface
->
langcode
,
'clear'
=>
TRUE
));
$result
=
$this
->
assertEqual
(
$target
,
$result
,
'Valid tokens replaced while invalid tokens cleared out.'
);
// Test without using the clear parameter (non-existent token untouched).
$target
.=
'[user:name]'
;
$target
.=
'[bogus:token]'
;
$result
=
token_
replace
(
$source
,
array
(
'node'
=>
$node
),
array
(
'langcode'
=>
$language_interface
->
langcode
));
$result
=
$token_service
->
replace
(
$source
,
array
(
'node'
=>
$node
),
array
(
'langcode'
=>
$language_interface
->
langcode
));
$this
->
assertEqual
(
$target
,
$result
,
'Valid tokens replaced while invalid tokens ignored.'
);
// Check that the results of
token_generate are sanitized properly. This does NOT
//
test the cleanliness of every token -- just that the $sanitize flag is being
//
passed properly through the call stack and being handled correctly by a 'known'
// token, [node:title].
// Check that the results of
Token::generate are sanitized properly. This
//
does NOT test the cleanliness of every token -- just that the $sanitize
//
flag is being passed properly through the call stack and being handled
//
correctly by a 'known'
token, [node:title].
$raw_tokens
=
array
(
'title'
=>
'[node:title]'
);
$generated
=
token_
generate
(
'node'
,
$raw_tokens
,
array
(
'node'
=>
$node
));
$generated
=
$token_service
->
generate
(
'node'
,
$raw_tokens
,
array
(
'node'
=>
$node
));
$this
->
assertEqual
(
$generated
[
'[node:title]'
],
check_plain
(
$node
->
title
),
'Token sanitized.'
);
$generated
=
token_
generate
(
'node'
,
$raw_tokens
,
array
(
'node'
=>
$node
),
array
(
'sanitize'
=>
FALSE
));
$generated
=
$token_service
->
generate
(
'node'
,
$raw_tokens
,
array
(
'node'
=>
$node
),
array
(
'sanitize'
=>
FALSE
));
$this
->
assertEqual
(
$generated
[
'[node:title]'
],
$node
->
title
,
'Unsanitized token generated properly.'
);
// Test token replacement when the string contains no tokens.
$this
->
assertEqual
(
token_
replace
(
'No tokens here.'
),
'No tokens here.'
);
$this
->
assertEqual
(
$token_service
->
replace
(
'No tokens here.'
),
'No tokens here.'
);
}
/**
* Test whether token-replacement works in various contexts.
*/
function
testSystemTokenRecognition
()
{
$token_service
=
\Drupal
::
token
();
$language_interface
=
language
(
LANGUAGE_TYPE_INTERFACE
);
// Generate prefixes and suffixes for the token context.
...
...
@@ -95,7 +98,7 @@ function testSystemTokenRecognition() {
foreach
(
$tests
as
$test
)
{
$input
=
$test
[
'prefix'
]
.
'[site:name]'
.
$test
[
'suffix'
];
$expected
=
$test
[
'prefix'
]
.
'Drupal'
.
$test
[
'suffix'
];
$output
=
token_
replace
(
$input
,
array
(),
array
(
'langcode'
=>
$language_interface
->
langcode
));
$output
=
$token_service
->
replace
(
$input
,
array
(),
array
(
'langcode'
=>
$language_interface
->
langcode
));
$this
->
assertTrue
(
$output
==
$expected
,
format_string
(
'Token recognized in string %string'
,
array
(
'%string'
=>
$input
)));
}
}
...
...
@@ -104,6 +107,7 @@ function testSystemTokenRecognition() {
* Tests the generation of all system site information tokens.
*/
function
testSystemSiteTokenReplacement
()
{
$token_service
=
\Drupal
::
token
();
$language_interface
=
language
(
LANGUAGE_TYPE_INTERFACE
);
$url_options
=
array
(
'absolute'
=>
TRUE
,
...
...
@@ -129,7 +133,7 @@ function testSystemSiteTokenReplacement() {