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