system.tokens.inc

Builds placeholder replacement tokens system-wide data.

This file handles tokens for the global 'site' token type, as well as 'date' and 'file' tokens.

File

drupal-7.x/modules/system/system.tokens.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Builds placeholder replacement tokens system-wide data.
  5. *
  6. * This file handles tokens for the global 'site' token type, as well as
  7. * 'date' and 'file' tokens.
  8. */
  9. /**
  10. * Implements hook_token_info().
  11. */
  12. function system_token_info() {
  13. $types['site'] = array(
  14. 'name' => t("Site information"),
  15. 'description' => t("Tokens for site-wide settings and other global information."),
  16. );
  17. $types['date'] = array(
  18. 'name' => t("Dates"),
  19. 'description' => t("Tokens related to times and dates."),
  20. );
  21. $types['file'] = array(
  22. 'name' => t("Files"),
  23. 'description' => t("Tokens related to uploaded files."),
  24. 'needs-data' => 'file',
  25. );
  26. // Site-wide global tokens.
  27. $site['name'] = array(
  28. 'name' => t("Name"),
  29. 'description' => t("The name of the site."),
  30. );
  31. $site['slogan'] = array(
  32. 'name' => t("Slogan"),
  33. 'description' => t("The slogan of the site."),
  34. );
  35. $site['mail'] = array(
  36. 'name' => t("Email"),
  37. 'description' => t("The administrative email address for the site."),
  38. );
  39. $site['url'] = array(
  40. 'name' => t("URL"),
  41. 'description' => t("The URL of the site's front page."),
  42. );
  43. $site['url-brief'] = array(
  44. 'name' => t("URL (brief)"),
  45. 'description' => t("The URL of the site's front page without the protocol."),
  46. );
  47. $site['login-url'] = array(
  48. 'name' => t("Login page"),
  49. 'description' => t("The URL of the site's login page."),
  50. );
  51. // Date related tokens.
  52. $date['short'] = array(
  53. 'name' => t("Short format"),
  54. 'description' => t("A date in 'short' format. (%date)", array('%date' => format_date(REQUEST_TIME, 'short'))),
  55. );
  56. $date['medium'] = array(
  57. 'name' => t("Medium format"),
  58. 'description' => t("A date in 'medium' format. (%date)", array('%date' => format_date(REQUEST_TIME, 'medium'))),
  59. );
  60. $date['long'] = array(
  61. 'name' => t("Long format"),
  62. 'description' => t("A date in 'long' format. (%date)", array('%date' => format_date(REQUEST_TIME, 'long'))),
  63. );
  64. $date['custom'] = array(
  65. 'name' => t("Custom format"),
  66. 'description' => t("A date in a custom format. See !php-date for details.", array('!php-date' => l(t('the PHP documentation'), 'http://php.net/manual/en/function.date.php'))),
  67. );
  68. $date['since'] = array(
  69. 'name' => t("Time-since"),
  70. 'description' => t("A date in 'time-since' format. (%date)", array('%date' => format_interval(REQUEST_TIME - 360, 2))),
  71. );
  72. $date['raw'] = array(
  73. 'name' => t("Raw timestamp"),
  74. 'description' => t("A date in UNIX timestamp format (%date)", array('%date' => REQUEST_TIME)),
  75. );
  76. // File related tokens.
  77. $file['fid'] = array(
  78. 'name' => t("File ID"),
  79. 'description' => t("The unique ID of the uploaded file."),
  80. );
  81. $file['name'] = array(
  82. 'name' => t("File name"),
  83. 'description' => t("The name of the file on disk."),
  84. );
  85. $file['path'] = array(
  86. 'name' => t("Path"),
  87. 'description' => t("The location of the file relative to Drupal root."),
  88. );
  89. $file['mime'] = array(
  90. 'name' => t("MIME type"),
  91. 'description' => t("The MIME type of the file."),
  92. );
  93. $file['size'] = array(
  94. 'name' => t("File size"),
  95. 'description' => t("The size of the file."),
  96. );
  97. $file['url'] = array(
  98. 'name' => t("URL"),
  99. 'description' => t("The web-accessible URL for the file."),
  100. );
  101. $file['timestamp'] = array(
  102. 'name' => t("Timestamp"),
  103. 'description' => t("The date the file was most recently changed."),
  104. 'type' => 'date',
  105. );
  106. $file['owner'] = array(
  107. 'name' => t("Owner"),
  108. 'description' => t("The user who originally uploaded the file."),
  109. 'type' => 'user',
  110. );
  111. return array(
  112. 'types' => $types,
  113. 'tokens' => array(
  114. 'site' => $site,
  115. 'date' => $date,
  116. 'file' => $file,
  117. ),
  118. );
  119. }
  120. /**
  121. * Implements hook_tokens().
  122. */
  123. function system_tokens($type, $tokens, array $data = array(), array $options = array()) {
  124. $url_options = array('absolute' => TRUE);
  125. if (isset($options['language'])) {
  126. $url_options['language'] = $options['language'];
  127. $language_code = $options['language']->language;
  128. }
  129. else {
  130. $language_code = NULL;
  131. }
  132. $sanitize = !empty($options['sanitize']);
  133. $replacements = array();
  134. if ($type == 'site') {
  135. foreach ($tokens as $name => $original) {
  136. switch ($name) {
  137. case 'name':
  138. $site_name = variable_get('site_name', 'Drupal');
  139. $replacements[$original] = $sanitize ? check_plain($site_name) : $site_name;
  140. break;
  141. case 'slogan':
  142. $slogan = variable_get('site_slogan', '');
  143. $replacements[$original] = $sanitize ? check_plain($slogan) : $slogan;
  144. break;
  145. case 'mail':
  146. $replacements[$original] = variable_get('site_mail', '');
  147. break;
  148. case 'url':
  149. $replacements[$original] = url('<front>', $url_options);
  150. break;
  151. case 'url-brief':
  152. $replacements[$original] = preg_replace(array('!^https?://!', '!/$!'), '', url('<front>', $url_options));
  153. break;
  154. case 'login-url':
  155. $replacements[$original] = url('user', $url_options);
  156. break;
  157. }
  158. }
  159. }
  160. elseif ($type == 'date') {
  161. if (empty($data['date'])) {
  162. $date = REQUEST_TIME;
  163. }
  164. else {
  165. $date = $data['date'];
  166. }
  167. foreach ($tokens as $name => $original) {
  168. switch ($name) {
  169. case 'short':
  170. $replacements[$original] = format_date($date, 'short', '', NULL, $language_code);
  171. break;
  172. case 'medium':
  173. $replacements[$original] = format_date($date, 'medium', '', NULL, $language_code);
  174. break;
  175. case 'long':
  176. $replacements[$original] = format_date($date, 'long', '', NULL, $language_code);
  177. break;
  178. case 'since':
  179. $replacements[$original] = format_interval((REQUEST_TIME - $date), 2, $language_code);
  180. break;
  181. case 'raw':
  182. $replacements[$original] = $sanitize ? check_plain($date) : $date;
  183. break;
  184. }
  185. }
  186. if ($created_tokens = token_find_with_prefix($tokens, 'custom')) {
  187. foreach ($created_tokens as $name => $original) {
  188. $replacements[$original] = format_date($date, 'custom', $name, NULL, $language_code);
  189. }
  190. }
  191. }
  192. elseif ($type == 'file' && !empty($data['file'])) {
  193. $file = $data['file'];
  194. foreach ($tokens as $name => $original) {
  195. switch ($name) {
  196. // Basic keys and values.
  197. case 'fid':
  198. $replacements[$original] = $file->fid;
  199. break;
  200. // Essential file data
  201. case 'name':
  202. $replacements[$original] = $sanitize ? check_plain($file->filename) : $file->filename;
  203. break;
  204. case 'path':
  205. $replacements[$original] = $sanitize ? check_plain($file->uri) : $file->uri;
  206. break;
  207. case 'mime':
  208. $replacements[$original] = $sanitize ? check_plain($file->filemime) : $file->filemime;
  209. break;
  210. case 'size':
  211. $replacements[$original] = format_size($file->filesize);
  212. break;
  213. case 'url':
  214. $replacements[$original] = $sanitize ? check_plain(file_create_url($file->uri)) : file_create_url($file->uri);
  215. break;
  216. // These tokens are default variations on the chained tokens handled below.
  217. case 'timestamp':
  218. $replacements[$original] = format_date($file->timestamp, 'medium', '', NULL, $language_code);
  219. break;
  220. case 'owner':
  221. $account = user_load($file->uid);
  222. $name = format_username($account);
  223. $replacements[$original] = $sanitize ? check_plain($name) : $name;
  224. break;
  225. }
  226. }
  227. if ($date_tokens = token_find_with_prefix($tokens, 'timestamp')) {
  228. $replacements += token_generate('date', $date_tokens, array('date' => $file->timestamp), $options);
  229. }
  230. if (($owner_tokens = token_find_with_prefix($tokens, 'owner')) && $account = user_load($file->uid)) {
  231. $replacements += token_generate('user', $owner_tokens, array('user' => $account), $options);
  232. }
  233. }
  234. return $replacements;
  235. }