name.tokens.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. /**
  3. * @file
  4. * Builds placeholder replacement tokens for country-related data.
  5. */
  6. /**
  7. * Implements hook_token_info().
  8. */
  9. function name_token_info() {
  10. // Start depreciated tokens. These only work if given a name object.
  11. $types['name'] = array(
  12. 'name' => t('Name (depreciated)'),
  13. 'description' => t('Raw name component tokens. (depreciated)'),
  14. 'needs-data' => 'name',
  15. );
  16. $old_tokens['default'] = array(
  17. 'name' => t('Name (depreciated)'),
  18. 'description' => t('Full default formatted name. Do not use, depreciated!'),
  19. );
  20. foreach (name_get_custom_format_options() as $machine_name => $formatter_title) {
  21. $old_tokens['formatter-' . $machine_name] = array(
  22. 'name' => t('Name: %formatter formatted (depreciated)', array('%formatter' => $formatter_title)),
  23. 'description' => t('Name using the %formatter formatter.', array('%formatter' => $formatter_title)),
  24. );
  25. }
  26. foreach (_name_translations() as $component => $component_label) {
  27. $old_tokens['component-' . $component] = array(
  28. 'name' => t('Name component: %component (depreciated)', array('%component' => $component_label)),
  29. 'description' => t('The %component component of the name.', array('%component' => $component_label)),
  30. );
  31. }
  32. // End depreciated tokens.
  33. //
  34. // The basic generic user real name token.
  35. $user_tokens['realname'] = array(
  36. 'name' => t('Real name'),
  37. 'description' => t('The real name of the user account, from the RealName or Name modules.'),
  38. );
  39. // New field integration.
  40. $types['name-field'] = array(
  41. 'name' => t('Name field values'),
  42. 'description' => t('Name field in the default format.'),
  43. 'needs-data' => 'name-field',
  44. );
  45. foreach (name_get_custom_format_options() as $machine_name => $formatter_title) {
  46. $tokens['formatter-' . $machine_name] = array(
  47. 'name' => t('Formatted name: %formatter', array('%formatter' => $formatter_title)),
  48. 'description' => t('Uses the %formatter format.', array('%formatter' => $formatter_title)),
  49. );
  50. }
  51. foreach (_name_translations() as $component => $component_label) {
  52. $tokens['component-' . $component] = array(
  53. 'name' => t('Component: %component', array('%component' => $component_label)),
  54. 'description' => t('The name component %component.', array('%component' => $component_label)),
  55. );
  56. }
  57. $token_info = array(
  58. 'types' => $types,
  59. 'tokens' => array(
  60. 'name' => $old_tokens,
  61. 'name-field' => $tokens,
  62. 'user' => $user_tokens,
  63. ),
  64. );
  65. foreach (name_token_types_chained(NULL, TRUE) as $token_type => $tokens) {
  66. $token_info['tokens'][$token_type] = $tokens;
  67. }
  68. return $token_info;
  69. }
  70. /**
  71. * Implements hook_tokens().
  72. */
  73. function name_tokens($type, $tokens, array $data = array(), array $options = array()) {
  74. $sanitize = !empty($options['sanitize']);
  75. $replacements = array();
  76. // This handles the field tokens.
  77. if (isset($data[$type]) && $known_tokens = name_token_types_chained($type)) {
  78. foreach ($tokens as $name => $original) {
  79. // The RealName module provides the 'name-raw' token.
  80. if ($name != 'name-raw' && strpos($name, 'name-') === 0) {
  81. /*
  82. * We handle a number of different combinations here.
  83. * token
  84. * token:[delta|all]
  85. * token:formatter-FORMAT_NAME
  86. * token:[delta|all]:formatter-FORMAT_NAME
  87. * token:component-TYPE
  88. * token:[delta|all]:component-TYPE
  89. */
  90. $parts = explode(':', $name);
  91. $field_name = array_shift($parts);
  92. $field_name = str_replace('-', '_', substr($field_name, 5));
  93. // Ensure that this is actually a real field token before replacing.
  94. // This will mimimise the chances of false matches like 'name-raw'.
  95. if (field_info_field($field_name)) {
  96. $entity_type = str_replace('-', '_', $type);
  97. // Fix for only known token type to entity type mismatched keys.
  98. if (in_array($entity_type, array('term', 'vocabulary'))) {
  99. $entity_type = 'taxonomy_' . $entity_type;
  100. }
  101. $items = field_get_items($entity_type, $data[$type], $field_name);
  102. if (empty($items)) {
  103. $replacements[$original] = '';
  104. continue;
  105. }
  106. // Find the delta value.
  107. $delta = NULL;
  108. $next = array_shift($parts);
  109. if (isset($next)) {
  110. if (is_numeric($next) && ((string) intval($next)) === (string) $next) {
  111. $delta = $next;
  112. }
  113. elseif ($next == 'all') {
  114. $delta = 'all';
  115. }
  116. else {
  117. // Return the value to the array for the next step.
  118. $delta = 0;
  119. array_unshift($parts, $next);
  120. }
  121. }
  122. else {
  123. $delta = 0;
  124. }
  125. if ($delta != 'all' && !isset($items[$delta])) {
  126. $replacements[$original] = '';
  127. continue;
  128. }
  129. // Find the token action and format / component.
  130. $action = NULL;
  131. $action_key = NULL;
  132. if ($next = array_shift($parts)) {
  133. if (strpos($next, 'formatter-') === 0) {
  134. $action = 'formatter';
  135. $action_key = substr($next, 10);
  136. }
  137. elseif (strpos($next, 'component-') === 0) {
  138. $action = 'component';
  139. $action_key = substr($next, 10);
  140. }
  141. }
  142. else {
  143. $action_key = 'default';
  144. $action = 'formatter';
  145. }
  146. $names = array();
  147. if ($action == 'formatter') {
  148. $format = name_get_format_by_machine_name($action_key);
  149. if (empty($format)) {
  150. $format = name_get_format_by_machine_name('default');
  151. }
  152. if ($delta != 'all') {
  153. $items = array($items[$delta]);
  154. }
  155. foreach ($items as $item) {
  156. $names[] = name_format($item, $format);
  157. }
  158. }
  159. else {
  160. if ($delta != 'all') {
  161. $items = array($items[$delta]);
  162. }
  163. foreach ($items as $item) {
  164. if (isset($item[$action_key])) {
  165. $names[] = $item[$action_key];
  166. }
  167. }
  168. }
  169. $names = implode(', ', array_filter($names));
  170. $replacements[$original] = $sanitize ? check_plain($names) : $names;
  171. }
  172. }
  173. }
  174. }
  175. if ($type == 'user' && !empty($data['user'])) {
  176. $account = $data['user'];
  177. foreach ($tokens as $name => $original) {
  178. switch ($name) {
  179. case 'realname':
  180. $replacements[$original] = '';
  181. if (!empty($account->realname)) {
  182. $replacements[$original] = $sanitize ? check_plain($account->realname) : $account->realname;
  183. }
  184. break;
  185. }
  186. }
  187. }
  188. if ($type == 'name' && !empty($data['name'])) {
  189. $name = $data['name'];
  190. $name_components = array();
  191. foreach (_name_translations() as $key => $title) {
  192. if (!empty($name[$key])) {
  193. $name_components[$key] = $name[$key];
  194. }
  195. else {
  196. $name_components[$key] = '';
  197. }
  198. }
  199. foreach ($tokens as $key => $original) {
  200. if ($key == 'default') {
  201. // Full default formatted name.
  202. $default = name_format($name_components, name_settings('default_format'));
  203. $replacements[$original] = $sanitize ? check_plain($default) : $default;
  204. }
  205. elseif (strpos($key, 'formatter-') === 0) {
  206. list(, $machine_name) = explode('-', $key, 2);
  207. $format = name_get_format_by_machine_name($machine_name);
  208. $formated_name = name_format($name_components, $format);
  209. $replacements[$original] = $sanitize ? check_plain($formated_name) : $formated_name;
  210. }
  211. elseif (strpos($key, 'component-') === 0) {
  212. list(, $component) = explode('-', $key, 2);
  213. $replacements[$original] = $sanitize ? check_plain($name_components[$component]) : $name_components[$component];
  214. }
  215. }
  216. }
  217. return $replacements;
  218. }
  219. /**
  220. * Defines a list of token types that can be chained with the name field.
  221. *
  222. * @return
  223. * If an entity (token) type is given, returns the chained sub-list.
  224. */
  225. function name_token_types_chained($type = NULL, $reset = FALSE) {
  226. // This functions gets called rather often when replacing tokens.
  227. static $drupal_static_fast;
  228. if (!isset($drupal_static_fast)) {
  229. $drupal_static_fast['types'] = &drupal_static(__FUNCTION__);
  230. }
  231. $types = &$drupal_static_fast['types'];
  232. if (!isset($types) || $reset) {
  233. // Note that this hook contains translated strings, so each language is
  234. // cached separately.
  235. global $language;
  236. $langcode = $language->language;
  237. if (!$reset && $cache = cache_get("name_token_types_chained:$langcode", 'cache')) {
  238. $types = $cache->data;
  239. }
  240. if (!$types) {
  241. $types = array();
  242. foreach (field_info_field_map() as $field_name => $info) {
  243. if ($info['type'] == 'name') {
  244. foreach ($info['bundles'] as $entity_type => $bundles) {
  245. // Name field. Also known as Client.
  246. $labels = array();
  247. foreach ($bundles as $bundle) {
  248. $instance = field_info_instance($entity_type, $field_name, $bundle);
  249. $labels[$instance['label']] = $instance['label'];
  250. }
  251. $label = array_shift($labels);
  252. $clean = str_replace('_', '-', $field_name);
  253. if (empty($labels)) {
  254. $description = t('Name field in the default format. To specify a delta value, use "@token:0". Append the other chained options after the delta value like this, "@token:0:component-given". Replace the delta value with all to obtain all items in the field like this "@token:all".',
  255. array('@token' => $clean));
  256. }
  257. else {
  258. $description = t('Name field in the default format. Also known as %labels', array('%labels' => implode(', ', $labels)));
  259. }
  260. // Make sure we get the correct token type.
  261. $entity_info = entity_get_info($entity_type);
  262. $token_type = isset($entity_info['token type']) ? $entity_info['token type'] : $entity_type;
  263. $types[$token_type]['name-' . $clean] = array(
  264. 'name' => check_plain($label),
  265. 'description' => $description,
  266. 'type' => 'name-field',
  267. );
  268. }
  269. }
  270. }
  271. cache_set("name_token_types_chained:$langcode", $types);
  272. }
  273. }
  274. if (isset($type)) {
  275. return isset($types[$type]) ? $types[$type] : NULL;
  276. }
  277. return $types;
  278. }