field_collection.tokens.inc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * @file
  4. * Provides host entity tokens for field_collection.module
  5. */
  6. /**
  7. * Implements hook_token_info().
  8. */
  9. function field_collection_token_info() {
  10. $type = array(
  11. 'name' => t('Field collection host entity'),
  12. 'description' => t('Tokens related to field collection host entities.'),
  13. 'needs-data' => 'field_collection_item',
  14. );
  15. // Simple tokens.
  16. $host['type'] = array(
  17. 'name' => t('Host entity type'),
  18. 'description' => t('The entity type of the host. Common types are <em>node</em> and <em>user</em>.'),
  19. );
  20. $host['bundle'] = array(
  21. 'name' => t('Host entity bundle'),
  22. 'description' => t('For <em>node</em> entity types this is the content type, otherwise available as <code>[node:content-type:machine-name]</code>.'),
  23. );
  24. $host['id'] = array(
  25. 'name' => t('Host entity ID'),
  26. 'description' => t('The entity ID of the host. For nodes this is <code>nid</code>, for users <code>uid</code>.'),
  27. );
  28. // Chained tokens.
  29. foreach (field_collection_host_entity_types() as $entity_type => $entity_info) {
  30. $host[$entity_type] = array(
  31. 'name' => t('Entity: @entity_type', array('@entity_type' => $entity_info['label'])),
  32. 'description' => t('Host entity tokens when it is of type %entity_type', array('%entity_type' => $entity_info['label'])),
  33. 'type' => $entity_type,
  34. );
  35. }
  36. return array(
  37. 'types' => array('host' => $type),
  38. 'tokens' => array('host' => $host),
  39. );
  40. }
  41. /**
  42. * Implements hook_token_info_alter().
  43. *
  44. * Inject an additional 'host' token to the 'field_collection_item' token type.
  45. */
  46. function field_collection_token_info_alter(&$data) {
  47. $data['types']['field_collection_item'] = array(
  48. 'name' => t('Field collection'),
  49. 'description' => t('Tokens related to field collection.'),
  50. 'needs-data' => 'field_collection_item',
  51. );
  52. $data['tokens']['field_collection_item']['host'] = array(
  53. 'name' => t('Host entity'),
  54. 'description' => t('The host entity of this field collection item.'),
  55. 'type' => 'host',
  56. );
  57. }
  58. /**
  59. * Implements hook_tokens().
  60. */
  61. function field_collection_tokens($type, $tokens, array $data = array(), array $options = array()) {
  62. $replacements = array();
  63. // Provide a complete set of tokens for type == 'host', and a supplementary
  64. // token 'host' for type == 'field_collection_item'.
  65. if (($type == 'field_collection_item' or $type == 'host') and !empty($data['field_collection_item'])) {
  66. $collection = $data['field_collection_item'];
  67. // When saving revisions, only $collection->original has valid state about
  68. // its host entity.
  69. if (!empty($collection->original)) {
  70. $collection = $collection->original;
  71. }
  72. if ($type == 'field_collection_item') {
  73. if (!empty($tokens['host'])) {
  74. $replacements[$tokens['host']] = $collection->hostEntityId();
  75. }
  76. if ($host_tokens = token_find_with_prefix($tokens, 'host')) {
  77. $replacements += token_generate('host', $host_tokens, $data, $options);
  78. }
  79. }
  80. // $type == 'host'
  81. else {
  82. // Mapping between token and the FieldCollectionItemEntity method used to
  83. // retrieve the token with.
  84. $token_method_map = array(
  85. 'type' => 'hostEntityType',
  86. 'bundle' => 'hostEntityBundle',
  87. 'id' => 'hostEntityId',
  88. );
  89. $entity_types = field_collection_host_entity_types();
  90. foreach ($tokens as $name => $orig) {
  91. if (isset($token_method_map[$name])) {
  92. $replacements[$orig] = $collection->{$token_method_map[$name]}();
  93. }
  94. // This replaces e.g. [host:node] and [host:user] with their respective
  95. // nid and uid.
  96. if (!empty($entity_types[$name])) {
  97. $replacements[$orig] = $collection->hostEntityId();
  98. }
  99. }
  100. foreach ($entity_types as $entity_type => $entity_info) {
  101. if ($entity_tokens = token_find_with_prefix($tokens, $entity_type)) {
  102. $host = $collection->hostEntity();
  103. $replacements += token_generate($entity_type, $entity_tokens, array($entity_type => $host), $options);
  104. }
  105. }
  106. }
  107. }
  108. return $replacements;
  109. }
  110. /**
  111. * Entity types that serve as host for field collections.
  112. *
  113. * @return array
  114. * The list of entities as provided by entity_get_info(), filtered by field
  115. * collection usage.
  116. */
  117. function field_collection_host_entity_types() {
  118. $host_entity_types = &drupal_static(__FUNCTION__, FALSE);
  119. if ($host_entity_types === FALSE) {
  120. $host_entity_types = array();
  121. $entity_types = entity_get_info();
  122. // Look for all field instances, filter them by type == 'field_collection'
  123. // and map the entity type it's connected to to the returned list.
  124. foreach (field_info_field_map() as $field_instance) {
  125. if ($field_instance['type'] == 'field_collection') {
  126. foreach (array_keys($field_instance['bundles']) as $entity_type) {
  127. if (!isset($host_entity_types[$entity_type])) {
  128. // No need to test for existence in $entity_types. If it's not there
  129. // your site is broken.
  130. $host_entity_types[$entity_type] = $entity_types[$entity_type];
  131. }
  132. }
  133. }
  134. }
  135. }
  136. return $host_entity_types;
  137. }