- Patch #1346166 by Dave Reid: Improve performance of token_replace() if there are no tokens to replace.

8.0.x
Dries 2012-01-24 20:18:24 -05:00
parent 68fc0fad1a
commit b55fc01277
2 changed files with 9 additions and 1 deletions

View File

@ -77,8 +77,13 @@
* 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 (token_scan($text) as $type => $tokens) {
foreach ($text_tokens as $type => $tokens) {
$replacements += token_generate($type, $tokens, $data, $options);
if (!empty($options['clear'])) {
$replacements += array_fill_keys($tokens, '');

View File

@ -1852,6 +1852,9 @@ class TokenReplaceTestCase extends DrupalWebTestCase {
$generated = token_generate('node', $raw_tokens, array('node' => $node), array('sanitize' => FALSE));
$this->assertEqual($generated['[node:title]'], $node->title, t('Unsanitized token generated properly.'));
// Test token replacement when the string contains no tokens.
$this->assertEqual(token_replace('No tokens here.'), 'No tokens here.');
}
/**