entity_token.tokens.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. $replacement = _entity_token_get_token($wrapper->$property_name, $options);
  166. if (isset($replacement)) {
  167. $replacements[$original] = $replacement;
  168. }
  169. }
  170. catch (EntityMetadataWrapperException $e) {
  171. // If tokens for not existing values are requested, just do nothing.
  172. }
  173. }
  174. }
  175. // Properly chain everything of a type marked as needs chaining.
  176. $info['tokens'] += array($type => array());
  177. foreach ($info['tokens'][$type] as $name => $token_info) {
  178. if (!empty($token_info['entity-token']) && isset($token_info['type']) && entity_token_types_chained($token_info['type'])) {
  179. if ($chained_tokens = token_find_with_prefix($tokens, $name)) {
  180. $wrapper = !isset($wrapper) ? _entity_token_wrap_data($type, $token_types[$type], $data[$type], $options) : $wrapper;
  181. $property_name = str_replace('-', '_', $name);
  182. try {
  183. // Pass on 'struct' properties wrapped, else un-wrap the data.
  184. $value = ($token_info['type'] == 'struct') ? $wrapper->$property_name : $wrapper->$property_name->value();
  185. $replacements += token_generate($token_info['type'], $chained_tokens, array($token_info['type'] => $value), $options);
  186. }
  187. catch (EntityMetadataWrapperException $e) {
  188. // If tokens for not existing values are requested, just do nothing.
  189. }
  190. }
  191. }
  192. }
  193. }
  194. // Add support for evaluating tokens for "list<type"> types.
  195. elseif ($item_token_type = entity_property_list_extract_type($type)) {
  196. foreach ($tokens as $name => $original) {
  197. // Care about getting entries of a list.
  198. if (is_numeric($name)) {
  199. $wrapper = !isset($wrapper) ? _entity_token_wrap_data($type, "list<$token_types[$item_token_type]>", $data[$type], $options) : $wrapper;
  200. try {
  201. $replacement = _entity_token_get_token($wrapper->get($name), $options);
  202. if (isset($replacement)) {
  203. $replacements[$original] = $replacement;
  204. }
  205. }
  206. catch (EntityMetadataWrapperException $e) {
  207. // If tokens for not existing values are requested, just do nothing.
  208. }
  209. }
  210. // Care about generating chained tokens for list-items.
  211. else {
  212. $parts = explode(':', $name, 2);
  213. $delta = $parts[0];
  214. if (is_numeric($delta) && $chained_tokens = token_find_with_prefix($tokens, $delta)) {
  215. $wrapper = !isset($wrapper) ? _entity_token_wrap_data($type, "list<$token_types[$item_token_type]>", $data[$type], $options) : $wrapper;
  216. try {
  217. $replacements += token_generate($item_token_type, $chained_tokens, array($item_token_type => $wrapper->get($delta)->value()), $options);
  218. }
  219. catch (EntityMetadataWrapperException $e) {
  220. // If tokens for not existing values are requested, just do nothing.
  221. }
  222. }
  223. }
  224. }
  225. }
  226. // Add support for chaining struct data. As struct data has no registered
  227. // tokens, we have to chain based upon wrapper property info.
  228. if ($type == 'struct') {
  229. $wrapper = $data[$type];
  230. foreach ($wrapper as $name => $property) {
  231. $token_type = _entity_token_map_to_token_type($property->info());
  232. if (entity_token_types_chained($token_type) && $chained_tokens = token_find_with_prefix($tokens, $name)) {
  233. try {
  234. // Pass on 'struct' properties wrapped, else un-wrap the data.
  235. $value = ($token_type == 'struct') ? $property : $property->value();
  236. $replacements += token_generate($token_type, $chained_tokens, array($token_type => $value), $options);
  237. }
  238. catch (EntityMetadataWrapperException $e) {
  239. // If tokens for not existing values are requested, just do nothing.
  240. }
  241. }
  242. }
  243. }
  244. return $replacements;
  245. }
  246. /**
  247. * Wraps the given data by correctly obeying the options.
  248. */
  249. function _entity_token_wrap_data($token_type, $type, $data, $options) {
  250. if ($type == 'site') {
  251. $wrapper = entity_metadata_site_wrapper();
  252. }
  253. elseif ($type == 'struct') {
  254. // 'struct' data items are passed on wrapped.
  255. $wrapper = $data;
  256. }
  257. else {
  258. $wrapper = entity_metadata_wrapper($type, $data);
  259. }
  260. if (isset($options['language']) && $wrapper instanceof EntityStructureWrapper) {
  261. $wrapper->language($options['language']->language);
  262. }
  263. return $wrapper;
  264. }
  265. /**
  266. * Gets the token replacement by correctly obeying the options.
  267. */
  268. function _entity_token_get_token($wrapper, $options) {
  269. if ($wrapper->value() === NULL) {
  270. // Do not provide a replacement if there is no value.
  271. return NULL;
  272. }
  273. if (empty($options['sanitize'])) {
  274. // When we don't need sanitized tokens decode already sanitizied texts.
  275. $options['decode'] = TRUE;
  276. }
  277. $langcode = isset($options['language']) ? $options['language']->language : NULL;
  278. // If there is a label for a property, e.g. defined by an options list or an
  279. // entity label, make use of it.
  280. if ($label = $wrapper->label()) {
  281. return empty($options['sanitize']) ? $label : check_plain($label);
  282. }
  283. switch ($wrapper->type()) {
  284. case 'integer':
  285. return $wrapper->value();
  286. case 'decimal':
  287. return number_format($wrapper->value(), 2);
  288. case 'date':
  289. return format_date($wrapper->value(), 'medium', '', NULL, $langcode);
  290. case 'duration':
  291. return format_interval($wrapper->value(), 2, $langcode);
  292. case 'boolean':
  293. return $wrapper->value() ? t('true') : t('false');
  294. case 'uri':
  295. case 'text':
  296. return $wrapper->value($options);
  297. }
  298. // Care for outputing list values.
  299. if ($wrapper instanceof EntityListWrapper) {
  300. $output = array();
  301. foreach ($wrapper as $item) {
  302. $output[] = _entity_token_get_token($item, $options);
  303. }
  304. return implode(', ', $output);
  305. }
  306. // Else we do not have a good string to output, e.g. for struct values. Just
  307. // output the string representation of the wrapper.
  308. return (string) $wrapper;
  309. }