Permalink
Browse files

Updating token.

  • Loading branch information...
1 parent d9772cb commit 0d3cdcccde8e61e70fb6342eaecf88a99e2e4ff8 @thomasturnbull committed Oct 6, 2011
Oops, something went wrong.
@@ -5,9 +5,9 @@ core = 7.x
files[] = token_test.module
hidden = TRUE
-; Information added by drupal.org packaging script on 2011-08-31
-version = "7.x-1.0-beta5"
+; Information added by drupal.org packaging script on 2011-09-30
+version = "7.x-1.0-beta6"
core = "7.x"
project = "token"
-datestamp = "1314804722"
+datestamp = "1317397307"
@@ -7,9 +7,9 @@ files[] = token.tokens.inc
files[] = token.pages.inc
files[] = token.test
-; Information added by drupal.org packaging script on 2011-08-31
-version = "7.x-1.0-beta5"
+; Information added by drupal.org packaging script on 2011-09-30
+version = "7.x-1.0-beta6"
core = "7.x"
project = "token"
-datestamp = "1314804722"
+datestamp = "1317397307"
@@ -1023,7 +1023,11 @@ function token_clean_token_name($name) {
* Do not use this function yet. Its API has not been finalized.
*/
function token_render_array(array $array, array $options = array()) {
- $rendered = array_map('render', $array);
+ $rendered = array();
+ foreach (element_children($array) as $key) {
+ $value = $array[$key];
+ $rendered[] = is_array($value) ? render($value) : (string) $value;
+ }
if (!empty($options['sanitize'])) {
$rendered = array_map('check_plain', $rendered);
}
@@ -1035,7 +1039,7 @@ function token_render_array(array $array, array $options = array()) {
* Do not use this function yet. Its API has not been finalized.
*/
function token_render_array_value($value, array $options = array()) {
- $rendered = render($value);
+ $rendered = is_array($value) ? render($value) : (string) $value;
if (!empty($options['sanitize'])) {
$rendered = check_plain($rendered);
}
@@ -163,7 +163,7 @@ function _token_token_tree_format_row($token, array $token_info, $is_group = FAL
* @see drupal_clean_css_identifier()
*/
function _token_clean_css_identifier($id) {
- static $replacements = array(' ' => '-', '_' => '-', '/' => '-', '[' => '-', ']' => '', ':' => '--');
+ static $replacements = array(' ' => '-', '_' => '-', '/' => '-', '[' => '-', ']' => '', ':' => '--', '?' => '');
return 'token-' . rtrim(strtr(trim($id, '[]'), $replacements), '-');
}
@@ -16,31 +16,41 @@ class TokenTestHelper extends DrupalWebTestCase {
}
function assertTokens($type, array $data, array $tokens, array $options = array()) {
- $token_input = drupal_map_assoc(array_keys($tokens));
- $values = token_generate($type, $token_input, $data, $options);
- foreach ($tokens as $token => $expected) {
+ $input = $this->mapTokenNames($type, array_keys($tokens));
+ $replacements = token_generate($type, $input, $data, $options);
+ foreach ($tokens as $name => $expected) {
+ $token = $input[$name];
if (!isset($expected)) {
- $this->assertTrue(!isset($values[$token]), t("Token value for [@type:@token] was not generated.", array('@type' => $type, '@token' => $token)));
+ $this->assertTrue(!isset($values[$token]), t("Token value for @token was not generated.", array('@type' => $type, '@token' => $token)));
}
- elseif (!isset($values[$token])) {
- $this->fail(t("Token value for [@type:@token] was not generated.", array('@type' => $type, '@token' => $token)));
+ elseif (!isset($replacements[$token])) {
+ $this->fail(t("Token value for @token was not generated.", array('@type' => $type, '@token' => $token)));
}
elseif (!empty($options['regex'])) {
- $this->assertTrue(preg_match('/^' . $expected . '$/', $values[$token]), t("Token value for [@type:@token] was '@actual', matching regular expression pattern '@expected'.", array('@type' => $type, '@token' => $token, '@actual' => $values[$token], '@expected' => $expected)));
+ $this->assertTrue(preg_match('/^' . $expected . '$/', $replacements[$token]), t("Token value for @token was '@actual', matching regular expression pattern '@expected'.", array('@type' => $type, '@token' => $token, '@actual' => $replacements[$token], '@expected' => $expected)));
}
else {
- $this->assertIdentical($values[$token], $expected, t("Token value for [@type:@token] was '@actual', expected value '@expected'.", array('@type' => $type, '@token' => $token, '@actual' => $values[$token], '@expected' => $expected)));
+ $this->assertIdentical($replacements[$token], $expected, t("Token value for @token was '@actual', expected value '@expected'.", array('@type' => $type, '@token' => $token, '@actual' => $replacements[$token], '@expected' => $expected)));
}
}
- return $values;
+ return $replacements;
}
- function assertNoTokens($type, array $data, array $tokens, array $options = array()) {
- $token_input = drupal_map_assoc($tokens);
- $values = token_generate($type, $token_input, $data, $options);
+ function mapTokenNames($type, array $tokens = array()) {
+ $return = array();
foreach ($tokens as $token) {
- $this->assertTrue(!isset($values[$token]), t("Token value for [@type:@token] was not generated.", array('@type' => $type, '@token' => $token)));
+ $return[$token] = "[$type:$token]";
+ }
+ return $return;
+ }
+
+ function assertNoTokens($type, array $data, array $tokens, array $options = array()) {
+ $input = $this->mapTokenNames($type, $tokens);
+ $replacements = token_generate($type, $input, $data, $options);
+ foreach ($tokens as $name) {
+ $token = $input[$name];
+ $this->assertTrue(!isset($replacements[$token]), t("Token value for @token was not generated.", array('@type' => $type, '@token' => $token)));
}
return $values;
}
@@ -551,17 +561,25 @@ class TokenUserTestCase extends TokenTestHelper {
'picture:fid' => $this->account->picture->fid,
'picture:size-raw' => 125,
'ip-address' => NULL,
+ 'roles' => implode(', ', $this->account->roles),
+ 'roles:keys' => implode(', ', array_keys($this->account->roles)),
);
$this->assertTokens('user', array('user' => $this->account), $user_tokens);
$edit = array('user_pictures' => FALSE);
$this->drupalPost('admin/config/people/accounts', $edit, 'Save configuration');
$this->assertText('The configuration options have been saved.');
+ // Remove the simpletest-created user role.
+ user_role_delete(end($this->account->roles));
+ $this->account = user_load($this->account->uid, TRUE);
+
$user_tokens = array(
'picture' => NULL,
'picture:fid' => NULL,
'ip-address' => NULL,
+ 'roles' => 'authenticated user',
+ 'roles:keys' => (string) DRUPAL_AUTHENTICATED_RID,
);
$this->assertTokens('user', array('user' => $this->account), $user_tokens);
@@ -570,6 +588,12 @@ class TokenUserTestCase extends TokenTestHelper {
'ip-address' => ip_address(),
);
$this->assertTokens('current-user', array(), $tokens);
+
+ $tokens = array(
+ 'roles' => 'anonymous user',
+ 'roles:keys' => (string) DRUPAL_ANONYMOUS_RID,
+ );
+ $this->assertTokens('user', array('user' => drupal_anonymous_user()), $tokens);
}
}
@@ -816,9 +840,8 @@ class TokenArrayTestCase extends TokenTestHelper {
}
function testArrayTokens() {
+ // Test a simple array.
$array = array(0 => 'a', 1 => 'b', 2 => 'c', 4 => 'd');
-
- // Test the profile token values.
$tokens = array(
'first' => 'a',
'last' => 'd',
@@ -836,6 +859,33 @@ class TokenArrayTestCase extends TokenTestHelper {
'join: ' => 'a b c d',
);
$this->assertTokens('array', array('array' => $array), $tokens);
+
+ // Test a mixed simple and render array.
+ // 2 => c, 0 => a, 4 => d, 1 => b
+ $array = array(
+ '#property' => 'value',
+ 0 => 'a',
+ 1 => array('#markup' => 'b', '#weight' => 0.01),
+ 2 => array('#markup' => 'c', '#weight' => -10),
+ 4 => array('#markup' => 'd', '#weight' => 0),
+ );
+ $tokens = array(
+ 'first' => 'c',
+ 'last' => 'b',
+ 'value:0' => 'a',
+ 'value:2' => 'c',
+ 'count' => 4,
+ 'keys' => '2, 0, 4, 1',
+ 'keys:value:3' => '1',
+ 'keys:join' => '2041',
+ 'reversed' => 'b, d, a, c',
+ 'reversed:keys' => '1, 4, 0, 2',
+ 'join:/' => 'c/a/d/b',
+ 'join' => 'cadb',
+ 'join:, ' => 'c, a, d, b',
+ 'join: ' => 'c a d b',
+ );
+ $this->assertTokens('array', array('array' => $array), $tokens);
}
}
@@ -30,17 +30,31 @@ function token_token_info_alter(&$info) {
unset($info['tokens']['node']['type']);
unset($info['tokens']['node']['type-name']);
- // Add [token:path] tokens for any URI-able entities.
+ // Support 'url' type tokens for core tokens.
+ if (isset($info['tokens']['comment']['url']) && module_exists('comment')) {
+ $info['tokens']['comment']['url']['type'] = 'url';
+ }
+ $info['tokens']['node']['url']['type'] = 'url';
+ if (isset($info['tokens']['term']['url']) && module_exists('taxonomy')) {
+ $info['tokens']['term']['url']['type'] = 'url';
+ }
+ $info['tokens']['user']['url']['type'] = 'url';
+
+ // Add [token:url] tokens for any URI-able entities.
$entities = entity_get_info();
foreach ($entities as $entity => $entity_info) {
if (!isset($entity_info['token type'])) {
continue;
}
+
$token_type = $entity_info['token type'];
+ if (!isset($info['types'][$token_type]) || !isset($info['tokens'][$token_type])) {
+ continue;
+ }
// Add [entity:url] tokens if they do not already exist.
// @todo Support entity:label
- if (!isset($info['tokens'][$token_type]['url']) && (!empty($entity_info['uri callback']) || !empty($entity_info['bundles'][$entity]['uri callback']))) {
+ if (!isset($info['tokens'][$token_type]['url']) && !empty($entity_info['uri callback'])) {
$info['tokens'][$token_type]['url'] = array(
'name' => t('URL'),
'description' => t('The URL of the @entity.', array('@entity' => drupal_strtolower($entity_info['label']))),
@@ -50,13 +64,6 @@ function token_token_info_alter(&$info) {
}
}
- $info['tokens']['comment']['url']['type'] = 'url';
- //$info['tokens']['file']['url']['type'] = 'url';
- $info['tokens']['node']['url']['type'] = 'url';
- $info['tokens']['term']['url']['type'] = 'url';
- $info['tokens']['user']['url']['type'] = 'url';
- // @todo Support other tokens not named 'url'
-
// Add support for custom date formats.
// @todo Remove when http://drupal.org/node/1173706 is fixed.
$date_format_types = system_get_date_types();
@@ -602,7 +609,8 @@ function token_tokens($type, $tokens, array $data = array(), array $options = ar
foreach ($tokens as $name => $original) {
switch ($name) {
case 'title':
- $replacements[$original] = drupal_get_title();
+ $title = drupal_get_title();
+ $replacements[$original] = $sanitize ? $title : decode_entities($title);
break;
case 'url':
$replacements[$original] = url($current_path, $url_options);
@@ -678,7 +686,7 @@ function token_tokens($type, $tokens, array $data = array(), array $options = ar
break;
case 'args':
$value = empty($url_options['alias']) ? drupal_get_path_alias($path, $language_code) : $path;
- $replacements[$original] = token_render_array(arg(NULL, $args), $options);
+ $replacements[$original] = token_render_array(arg(NULL, $value), $options);
break;
}
}
@@ -737,23 +745,25 @@ function token_tokens($type, $tokens, array $data = array(), array $options = ar
if ($type == 'array' && !empty($data['array']) && is_array($data['array'])) {
$array = $data['array'];
+ $sort = isset($options['array sort']) ? $options['array sort'] : TRUE;
+ $keys = element_children($array, $sort);
+
foreach ($tokens as $name => $original) {
switch ($name) {
case 'first':
- $first = reset($array);
- $replacements[$original] = token_render_array_value($first, $options);
- reset($array);
+ $value = $array[$keys[0]];
+ $value = is_array($value) ? render($value) : (string) $value;
+ $replacements[$original] = $sanitize ? check_plain($value) : $value;
break;
case 'last':
- $last = end($array);
- $replacements[$original] = token_render_array_value($last, $options);
- reset($array);
+ $value = $array[$keys[count($keys) - 1]];
+ $value = is_array($value) ? render($value) : (string) $value;
+ $replacements[$original] = $sanitize ? check_plain($value) : $value;
break;
case 'count':
- $replacements[$original] = count($array);
+ $replacements[$original] = count($keys);
break;
case 'keys':
- $keys = array_keys($array);
$replacements[$original] = token_render_array($keys, $options);
break;
case 'reversed':
@@ -769,7 +779,7 @@ function token_tokens($type, $tokens, array $data = array(), array $options = ar
// [array:value:*] dynamic tokens.
if ($value_tokens = token_find_with_prefix($tokens, 'value')) {
foreach ($value_tokens as $key => $original) {
- if (isset($array[$key])) {
+ if ($key[0] !== '#' && isset($array[$key])) {
$replacements[$original] = token_render_array_value($array[$key], $options);
}
}
@@ -782,12 +792,14 @@ function token_tokens($type, $tokens, array $data = array(), array $options = ar
}
}
- // Chained token relationships.
+ // [array:keys:*] chained tokens.
if ($key_tokens = token_find_with_prefix($tokens, 'keys')) {
- $replacements += token_generate('array', $key_tokens, array('array' => array_keys($array)), $options);
+ $replacements += token_generate('array', $key_tokens, array('array' => $keys), $options);
}
+
+ // [array:reversed:*] chained tokens.
if ($reversed_tokens = token_find_with_prefix($tokens, 'reversed')) {
- $replacements += token_generate('array', $reversed_tokens, array('array' => array_reverse($array, TRUE)), $options);
+ $replacements += token_generate('array', $reversed_tokens, array('array' => array_reverse($array, TRUE)), array('array sort' => FALSE) + $options);
}
// @todo Handle if the array values are not strings and could be chained.
@@ -1233,8 +1245,7 @@ function _token_field_info($field_name = NULL) {
}
$info[$key]['token types'][] = $token_type;
- $info[$key]['labels'] = array();
- $info[$key]['bundles'] = array();
+ $info[$key] += array('labels' => array(), 'bundles' => array());
// Find which label is most commonly used.
foreach ($field['bundles'][$entity] as $bundle) {
@@ -1249,20 +1260,24 @@ function _token_field_info($field_name = NULL) {
$info[$key]['labels'][] = $instances[$entity][$bundle][$key]['label'];
$info[$key]['bundles'][$token_type][$bundle] = $entity_info[$entity]['bundles'][$bundle]['label'];
}
- $labels = array_count_values($info[$key]['labels']);
- arsort($labels);
- $info[$key]['label'] = key($labels);
-
- // Generate a description for the token.
- $info[$key]['description'] = t('@type field.', array('@type' => $type_info[$field['type']]['label']));
- if ($also_known_as = array_unique(array_diff($info[$key]['labels'], array($info[$key]['label'])))) {
- $info[$key]['description'] .= ' ' . t('Also known as %labels.', array('%labels' => implode(', ', $also_known_as)));
- }
+ }
+ }
+
+ if (isset($info[$key])) {
+ $labels = array_count_values($info[$key]['labels']);
+ arsort($labels);
+ $info[$key]['label'] = key($labels);
+
+ // Generate a description for the token.
+ $info[$key]['description'] = t('@type field.', array('@type' => $type_info[$field['type']]['label']));
+ if ($also_known_as = array_unique(array_diff($info[$key]['labels'], array($info[$key]['label'])))) {
+ $info[$key]['description'] .= ' ' . t('Also known as %labels.', array('%labels' => implode(', ', $also_known_as)));
}
}
}
drupal_alter('token_field_info', $info);
+ cache_set('field:info', $info, 'cache_token');
}
}
@@ -1290,6 +1305,11 @@ function field_token_info_alter(&$info) {
continue;
}
+ // Ensure the tokens exist.
+ if (!isset($info['types'][$token_type]) || !isset($info['tokens'][$token_type])) {
+ continue;
+ }
+
if ($token_type == 'comment' && $field_name == 'comment_body') {
// Core provides the comment field as [comment:body].
continue;

0 comments on commit 0d3cdcc

Please sign in to comment.