entity_token.inc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * @file
  4. * Entity tokens module integration.
  5. */
  6. /**
  7. * Implements hook_custom_formatters_token_tree_types_alter() on behalf of
  8. * entity_tokens.module.
  9. */
  10. function entity_token_custom_formatters_token_tree_types_alter(&$token_types, $field_type) {
  11. module_load_include('inc', 'entity_token', 'entity_token.tokens');
  12. $fields = array();
  13. foreach (field_info_fields() as $name => $field) {
  14. if ($field['type'] == $field_type) {
  15. $fields[] = str_replace('_', '-', $name);
  16. }
  17. }
  18. $info = module_exists('token') ? token_get_info() : token_info();
  19. foreach ($fields as $field) {
  20. foreach (element_children($info['tokens']) as $type) {
  21. if (isset($info['tokens'][$type][$field])) {
  22. $token_info = $info['tokens'][$type][$field];
  23. if (!empty($token_info['entity-token']) && isset($token_info['type']) && entity_token_types_chained($token_info['type'])) {
  24. $token_types[] = $token_info['type'];
  25. return;
  26. }
  27. }
  28. }
  29. }
  30. }
  31. /**
  32. * Implements hook_custom_formatters_token_data_alter() on behalf of
  33. * entity_token.module.
  34. */
  35. function entity_token_custom_formatters_token_data_alter(&$token_data, &$text, $field) {
  36. module_load_include('inc', 'entity_token', 'entity_token.tokens');
  37. $field_name = str_replace('_', '-', $field['field_name']);
  38. $info = module_exists('token') ? token_get_info() : token_info();
  39. foreach (element_children($info['tokens']) as $type) {
  40. if (isset($info['tokens'][$type][$field_name])) {
  41. $token_info = $info['tokens'][$type][$field_name];
  42. if (!empty($token_info['entity-token']) && isset($token_info['type']) && entity_token_types_chained($token_info['type'])) {
  43. $token_types = entity_token_types_chained();
  44. $item_token_type = entity_property_list_extract_type($token_info['type']);
  45. // List tokens.
  46. if (isset($token_types[$item_token_type])) {
  47. $entity_info = entity_get_info($token_types[$item_token_type]);
  48. $entity = entity_load($token_types[$item_token_type], array($token_data['item'][$entity_info['entity keys']['id']]));
  49. $token_data[$token_info['type']] = array($entity[$token_data['item'][$entity_info['entity keys']['id']]]);
  50. $text = str_replace('[list:', "[{$token_info['type']}:", $text);
  51. }
  52. // Structural tokens.
  53. elseif ('struct' == $token_info['type']) {
  54. $entity_info = entity_get_info($field['type']);
  55. if (isset($entity_info['entity keys'])) {
  56. $entity = entity_load($field['type'], array($token_data['item'][$entity_info['entity keys']['id']]));
  57. $token_data['struct'] = entity_metadata_wrapper($field['type'], $entity[$token_data['item'][$entity_info['entity keys']['id']]]);
  58. }
  59. }
  60. return;
  61. }
  62. }
  63. }
  64. }