entity_token.tokens.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. /**
  3. * @file
  4. * Provides tokens for entity properties which have no token yet.
  5. */
  6. /**
  7. * Defines the types of properties to be added as token.
  8. *
  9. * @return
  10. * An array mapping token types to the usual (entity) type names.
  11. */
  12. function entity_token_types() {
  13. $return = entity_token_types_chained();
  14. return $return + drupal_map_assoc(array('text', 'integer', 'decimal', 'duration', 'boolean', 'uri'));
  15. }
  16. /**
  17. * Defines a list of token types that need to be chained.
  18. *
  19. * @return
  20. * If a (token) type is given, whether the given type needs to be chained.
  21. * Else a full list of token types to be chained as returned by
  22. * entity_token_token_types().
  23. */
  24. function entity_token_types_chained($type = NULL) {
  25. // This functions gets called rather often when replacing tokens, thus
  26. // we statically cache $types using the advanced drupal static pattern.
  27. static $drupal_static_fast;
  28. if (!isset($drupal_static_fast)) {
  29. $drupal_static_fast['types'] = &drupal_static(__FUNCTION__, array());
  30. }
  31. $types = &$drupal_static_fast['types'];
  32. if (!$types) {
  33. // Add entities.
  34. foreach (entity_get_info() as $entity_type => $info) {
  35. if ($token_type = isset($info['token type']) ? $info['token type'] : $entity_type) {
  36. $types[$token_type] = $entity_type;
  37. }
  38. }
  39. // Add 'date' and 'site' tokens.
  40. $types['date'] = 'date';
  41. $types['site'] = 'site';
  42. // Add a 'struct' type.
  43. $types['struct'] = 'struct';
  44. }
  45. if (isset($type)) {
  46. return isset($types[$type]) || entity_property_list_extract_type($type);
  47. }
  48. return $types;
  49. }
  50. /**
  51. * Gets the right token type for a given property info array.
  52. */
  53. function _entity_token_map_to_token_type($property_info) {
  54. $lookup = &drupal_static(__FUNCTION__);
  55. if (!$lookup) {
  56. // Initialize a lookup array mapping property types to token types.
  57. $lookup = array_flip(entity_token_types());
  58. }
  59. $type = isset($property_info['type']) ? $property_info['type'] : 'text';
  60. // Just use the type 'struct' for all structures.
  61. if (!empty($property_info['property info'])) {
  62. $type = 'struct';
  63. }
  64. if ($item_type = entity_property_list_extract_type($type)) {
  65. return isset($lookup[$item_type]) ? "list<$lookup[$item_type]>" : FALSE;
  66. }
  67. return isset($lookup[$type]) ? $lookup[$type] : FALSE;
  68. }
  69. /**
  70. * Implements hook_token_info_alter().
  71. */
  72. function entity_token_token_info_alter(&$info) {
  73. $entity_info = entity_get_info();
  74. $token_types = entity_token_types_chained();
  75. // Loop over all chain-able token types, as those may contain further tokens,
  76. // e.g. entity types or 'site'.
  77. foreach ($token_types as $token_type => $type) {
  78. // Just add all properties regardless whether it's in a bundle, but only if
  79. // there is no token of the property yet.
  80. foreach (entity_get_all_property_info($type) as $name => $property) {
  81. $name = str_replace('_', '-', $name);
  82. $property += array('type' => 'text', 'description' => $property['label']);
  83. $property_token_type = _entity_token_map_to_token_type($property);
  84. if (!isset($info['tokens'][$token_type][$name]) && $property_token_type) {
  85. $info['tokens'][$token_type][$name] = array(
  86. 'name' => $property['label'],
  87. 'description' => $property['description'],
  88. 'type' => $property_token_type,
  89. // Mark the token so we know we have to provide the value afterwards.
  90. 'entity-token' => TRUE,
  91. );
  92. }
  93. if ($property_token_type == 'struct' && !empty($property['property info'])) {
  94. $info['tokens'][$token_type][$name]['dynamic'] = TRUE;
  95. $help = array();
  96. foreach ($property['property info'] as $key => $property_info) {
  97. $help[] = $key . ' (' . $property_info['label'] . ')';
  98. }
  99. $info['tokens'][$token_type][$name]['description'] .= ' ' . t('The following properties may be appended to the token: @keys',
  100. array('@keys' => implode(', ', $help))
  101. );
  102. }
  103. }
  104. }
  105. // Make sure all chain-able token types we support are registered.
  106. foreach ($token_types as $token_type => $type) {
  107. if (!empty($info['tokens'][$token_type]) && !isset($info['types'][$token_type])) {
  108. if (isset($entity_info[$type])) {
  109. $info['types'][$token_type] = array(
  110. 'name' => $entity_info[$type]['label'],
  111. 'description' => t('Tokens related to the "@name" entities.', array('@name' => $entity_info[$type]['label'])),
  112. 'needs-data' => $token_type,
  113. );
  114. }
  115. else {
  116. $info['types'][$token_type] = array(
  117. 'name' => drupal_strtoupper($token_type),
  118. 'description' => t('@name tokens.', array('@name' => drupal_strtoupper($token_type))),
  119. 'needs-data' => $token_type,
  120. );
  121. }
  122. }
  123. if (!empty($info['tokens'][$token_type]) && !isset($info['types']["list<$token_type>"]) && $token_type != 'site') {
  124. if (isset($entity_info[$type])) {
  125. $info['types']["list<$token_type>"] = array(
  126. 'name' => t('List of @entities', array('@entities' => isset($entity_info[$type]['plural label']) ? $entity_info[$type]['plural label'] : $entity_info[$type]['label'] . 's')),
  127. 'description' => t('Tokens related to the "@name" entities.', array('@name' => $entity_info[$type]['label'])),
  128. 'needs-data' => "list<$token_type>",
  129. );
  130. }
  131. else {
  132. $info['types']["list<$token_type>"] = array(
  133. 'name' => t('List of @type values', array('@type' => $token_type)),
  134. 'description' => t('Tokens for lists of @type values.', array('@type' => $token_type)),
  135. 'needs-data' => "list<$token_type>",
  136. );
  137. }
  138. // Also add some basic token replacements for lists...
  139. for ($i = 0; $i < 4; $i++) {
  140. $info['tokens']["list<$token_type>"][$i] = array(
  141. 'name' => t('@type with delta @delta', array('@delta' => $i, '@type' => $info['types'][$token_type]['name'])),
  142. 'description' => t('The list item with delta @delta. Delta values start from 0 and are incremented by one per list item.', array('@delta' => $i)),
  143. 'type' => $token_type,
  144. );
  145. }
  146. }
  147. }
  148. }
  149. /**
  150. * Implements hook_tokens().
  151. */
  152. function entity_token_tokens($type, $tokens, array $data = array(), array $options = array()) {
  153. $token_types = entity_token_types_chained();
  154. $replacements = array();
  155. if (isset($token_types[$type]) && (!empty($data[$type]) || $type == 'site')) {
  156. $data += array($type => FALSE);
  157. // Make use of token module's token cache if available.
  158. $info = module_exists('token') ? token_get_info() : token_info();
  159. foreach ($tokens as $name => $original) {
  160. // Provide the token for all properties marked to stem from us.
  161. if (!empty($info['tokens'][$type][$name]['entity-token']) || $type == 'struct') {
  162. $wrapper = !isset($wrapper) ? _entity_token_wrap_data($type, $token_types[$type], $data[$type], $options) : $wrapper;
  163. $property_name = str_replace('-', '_', $name);
  164. try {
  165. if (isset($wrapper->$property_name)) {
  166. $replacement = _entity_token_get_token($wrapper->$property_name, $options);
  167. if (isset($replacement)) {
  168. $replacements[$original] = $replacement;
  169. }
  170. }
  171. }
  172. catch (EntityMetadataWrapperException $e) {
  173. // If tokens for not existing values are requested, just do nothing.
  174. }
  175. }
  176. }
  177. // Properly chain everything of a type marked as needs chaining.
  178. $info['tokens'] += array($type => array());
  179. foreach ($info['tokens'][$type] as $name => $token_info) {
  180. if (!empty($token_info['entity-token']) && isset($token_info['type']) && entity_token_types_chained($token_info['type'])) {
  181. if ($chained_tokens = token_find_with_prefix($tokens, $name)) {
  182. $wrapper = !isset($wrapper) ? _entity_token_wrap_data($type, $token_types[$type], $data[$type], $options) : $wrapper;
  183. $property_name = str_replace('-', '_', $name);
  184. try {
  185. // Pass on 'struct' properties wrapped, else un-wrap the data.
  186. $value = ($token_info['type'] == 'struct') ? $wrapper->$property_name : $wrapper->$property_name->value();
  187. $replacements += token_generate($token_info['type'], $chained_tokens, array($token_info['type'] => $value), $options);
  188. }
  189. catch (EntityMetadataWrapperException $e) {
  190. // If tokens for not existing values are requested, just do nothing.
  191. }
  192. }
  193. }
  194. }
  195. }
  196. // Add support for evaluating tokens for "list<type"> types.
  197. elseif ($item_token_type = entity_property_list_extract_type($type)) {
  198. foreach ($tokens as $name => $original) {
  199. // Care about getting entries of a list.
  200. if (is_numeric($name)) {
  201. $wrapper = !isset($wrapper) ? _entity_token_wrap_data($type, "list<$token_types[$item_token_type]>", $data[$type], $options) : $wrapper;
  202. try {
  203. $replacement = _entity_token_get_token($wrapper->get($name), $options);
  204. if (isset($replacement)) {
  205. $replacements[$original] = $replacement;
  206. }
  207. }
  208. catch (EntityMetadataWrapperException $e) {
  209. // If tokens for not existing values are requested, just do nothing.
  210. }
  211. }
  212. // Care about generating chained tokens for list-items.
  213. else {
  214. $parts = explode(':', $name, 2);
  215. $delta = $parts[0];
  216. if (is_numeric($delta) && $chained_tokens = token_find_with_prefix($tokens, $delta)) {
  217. $wrapper = !isset($wrapper) ? _entity_token_wrap_data($type, "list<$token_types[$item_token_type]>", $data[$type], $options) : $wrapper;
  218. try {
  219. $replacements += token_generate($item_token_type, $chained_tokens, array($item_token_type => $wrapper->get($delta)->value()), $options);
  220. }
  221. catch (EntityMetadataWrapperException $e) {
  222. // If tokens for not existing values are requested, just do nothing.
  223. }
  224. }
  225. }
  226. }
  227. }
  228. // Add support for chaining struct data. As struct data has no registered
  229. // tokens, we have to chain based upon wrapper property info.
  230. if ($type == 'struct') {
  231. $wrapper = $data[$type];
  232. foreach ($wrapper as $name => $property) {
  233. $token_type = _entity_token_map_to_token_type($property->info());
  234. if (entity_token_types_chained($token_type) && $chained_tokens = token_find_with_prefix($tokens, $name)) {
  235. try {
  236. // Pass on 'struct' properties wrapped, else un-wrap the data.
  237. $value = ($token_type == 'struct') ? $property : $property->value();
  238. $replacements += token_generate($token_type, $chained_tokens, array($token_type => $value), $options);
  239. }
  240. catch (EntityMetadataWrapperException $e) {
  241. // If tokens for not existing values are requested, just do nothing.
  242. }
  243. }
  244. }
  245. }
  246. return $replacements;
  247. }
  248. /**
  249. * Wraps the given data by correctly obeying the options.
  250. */
  251. function _entity_token_wrap_data($token_type, $type, $data, $options) {
  252. if ($type == 'site') {
  253. $wrapper = entity_metadata_site_wrapper();
  254. }
  255. elseif ($type == 'struct') {
  256. // 'struct' data items are passed on wrapped.
  257. $wrapper = $data;
  258. }
  259. else {
  260. $wrapper = entity_metadata_wrapper($type, $data);
  261. }
  262. if (isset($options['language']) && $wrapper instanceof EntityStructureWrapper) {
  263. $wrapper->language($options['language']->language);
  264. }
  265. return $wrapper;
  266. }
  267. /**
  268. * Gets the token replacement by correctly obeying the options.
  269. */
  270. function _entity_token_get_token($wrapper, $options) {
  271. if (!$wrapper || $wrapper->value() === NULL) {
  272. // Do not provide a replacement if there is no value.
  273. return NULL;
  274. }
  275. if (empty($options['sanitize'])) {
  276. // When we don't need sanitized tokens decode already sanitizied texts.
  277. $options['decode'] = TRUE;
  278. }
  279. $langcode = isset($options['language']) ? $options['language']->language : NULL;
  280. // If there is a label for a property, e.g. defined by an options list or an
  281. // entity label, make use of it.
  282. if ($label = $wrapper->label()) {
  283. return empty($options['sanitize']) ? $label : check_plain($label);
  284. }
  285. switch ($wrapper->type()) {
  286. case 'integer':
  287. return $wrapper->value();
  288. case 'decimal':
  289. return number_format($wrapper->value(), 2);
  290. case 'date':
  291. return format_date($wrapper->value(), 'medium', '', NULL, $langcode);
  292. case 'duration':
  293. return format_interval($wrapper->value(), 2, $langcode);
  294. case 'boolean':
  295. return $wrapper->value() ? t('true') : t('false');
  296. case 'uri':
  297. case 'text':
  298. return $wrapper->value($options);
  299. }
  300. // Care for outputing list values.
  301. if ($wrapper instanceof EntityListWrapper) {
  302. $output = array();
  303. foreach ($wrapper as $item) {
  304. $output[] = _entity_token_get_token($item, $options);
  305. }
  306. return implode(', ', $output);
  307. }
  308. // Else we do not have a good string to output, e.g. for struct values. Just
  309. // output the string representation of the wrapper.
  310. return (string) $wrapper;
  311. }