1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Token integration for the file_entity module.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
|
|
* Implements hook_token_info().
|
10 |
|
|
*/
|
11 |
|
|
function file_entity_token_info() {
|
12 |
|
|
// File type tokens.
|
13 |
|
|
$info['types']['file-type'] = array(
|
14 |
|
|
'name' => t('File type'),
|
15 |
|
|
'description' => t('Tokens associated with file types.'),
|
16 |
|
|
'needs-data' => 'file_type',
|
17 |
|
|
);
|
18 |
|
|
$info['tokens']['file-type']['name'] = array(
|
19 |
|
|
'name' => t('Name'),
|
20 |
|
|
'description' => t('The name of the file type.'),
|
21 |
|
|
);
|
22 |
|
|
$info['tokens']['file-type']['machine-name'] = array(
|
23 |
|
|
'name' => t('Machine-readable name'),
|
24 |
|
|
'description' => t('The unique machine-readable name of the file type.'),
|
25 |
|
|
);
|
26 |
|
|
$info['tokens']['file-type']['count'] = array(
|
27 |
|
|
'name' => t('File count'),
|
28 |
|
|
'description' => t('The number of files belonging to the file type.'),
|
29 |
|
|
);
|
30 |
|
|
$info['tokens']['file-type']['edit-url'] = array(
|
31 |
|
|
'name' => t('Edit URL'),
|
32 |
|
|
'description' => t("The URL of the file type's edit page."),
|
33 |
|
|
);
|
34 |
|
|
|
35 |
|
|
// File tokens.
|
36 |
|
|
$info['tokens']['file']['type'] = array(
|
37 |
|
|
'name' => t('File type'),
|
38 |
|
|
'description' => t('The file type of the file.'),
|
39 |
|
|
'type' => 'file-type',
|
40 |
|
|
);
|
41 |
|
|
$info['tokens']['file']['download-url'] = array(
|
42 |
|
|
'name' => t('Download URL'),
|
43 |
|
|
'description' => t('The URL to download the file directly.'),
|
44 |
|
|
'type' => 'url',
|
45 |
|
|
);
|
46 |
|
|
|
47 |
082b75eb
|
Assos Assos
|
if (module_exists('token')) {
|
48 |
|
|
$info['types']['file_field'] = array(
|
49 |
|
|
'name' => t('Media'),
|
50 |
|
|
'description' => t('Tokens related to a file_entity field.'),
|
51 |
|
|
'hidden' => TRUE,
|
52 |
|
|
);
|
53 |
|
|
|
54 |
|
|
$default_text = ' ' . t('Defaults to first value.');
|
55 |
|
|
|
56 |
|
|
$info['tokens']['file_field'] = array(
|
57 |
|
|
'field' => array(
|
58 |
|
|
'name' => t('Field token value'),
|
59 |
|
|
'description' => t('Default: The value returned by the token field formatter.') . $default_text,
|
60 |
|
|
),
|
61 |
|
|
'url' => array(
|
62 |
|
|
'name' => t('URL'),
|
63 |
|
|
'description' => t('URL of the file_entity resource.') . $default_text,
|
64 |
|
|
'type' => 'array',
|
65 |
|
|
),
|
66 |
|
|
'filename' => array(
|
67 |
|
|
'name' => t('Filename'),
|
68 |
|
|
'description' => t('Filename the file_entity resource.') . $default_text,
|
69 |
|
|
'type' => 'array',
|
70 |
|
|
),
|
71 |
|
|
'filemime' => array(
|
72 |
|
|
'name' => t('MIME type'),
|
73 |
|
|
'description' => t('MIME type of the file_entity resource.') . $default_text,
|
74 |
|
|
'type' => 'array',
|
75 |
|
|
),
|
76 |
|
|
'type' => array(
|
77 |
|
|
'name' => t('File type'),
|
78 |
|
|
'description' => t('File type of the file_entity resource.') . $default_text,
|
79 |
|
|
'type' => 'array',
|
80 |
|
|
),
|
81 |
|
|
'image' => array(
|
82 |
|
|
'name' => t('Image'),
|
83 |
|
|
'description' => t('URL of a representative image for the file_entity resource, e.g. a video thumbnail.') . $default_text,
|
84 |
|
|
'type' => 'array',
|
85 |
|
|
),
|
86 |
|
|
'height' => array(
|
87 |
|
|
'name' => t('Height'),
|
88 |
|
|
'description' => t('Height of the file_entity resource, for videos or images.') . $default_text,
|
89 |
|
|
'type' => 'array',
|
90 |
|
|
),
|
91 |
|
|
'width' => array(
|
92 |
|
|
'name' => t('Width'),
|
93 |
|
|
'description' => t('Width of the file_entity resource, for videos or images.') . $default_text,
|
94 |
|
|
'type' => 'array',
|
95 |
|
|
),
|
96 |
|
|
'https-url' => array(
|
97 |
|
|
'name' => t('Secure URL'),
|
98 |
|
|
'description' => t('URL of the file_entity resource using HTTPS.') . $default_text,
|
99 |
|
|
'type' => 'array',
|
100 |
|
|
),
|
101 |
|
|
'https-image' => array(
|
102 |
|
|
'name' => t('Secure image'),
|
103 |
|
|
'description' => t('URL of a representative image for the file_entity resource using HTTPS, usually for videos.') . $default_text,
|
104 |
|
|
'type' => 'array',
|
105 |
|
|
),
|
106 |
|
|
);
|
107 |
|
|
|
108 |
|
|
$all_fields = field_info_field_map();
|
109 |
|
|
foreach ($all_fields as $field_name => $field) {
|
110 |
|
|
if ($field['type'] == 'file') {
|
111 |
|
|
$field_info = _token_field_info($field_name);
|
112 |
|
|
foreach (array_keys($field['bundles']) as $entity_type) {
|
113 |
f419e8de
|
Assos Assos
|
if ($entity_type == 'taxonomy_term') {
|
114 |
|
|
$entity_type = 'term';
|
115 |
|
|
}
|
116 |
082b75eb
|
Assos Assos
|
$info['tokens'][$entity_type][$field_name] = array(
|
117 |
|
|
'name' => $field_info['label'],
|
118 |
|
|
'description' => $field_info['description'],
|
119 |
|
|
'type' => 'file_field',
|
120 |
|
|
'module' => 'file_entity',
|
121 |
|
|
);
|
122 |
|
|
}
|
123 |
|
|
}
|
124 |
|
|
}
|
125 |
|
|
}
|
126 |
|
|
|
127 |
85ad3d82
|
Assos Assos
|
return $info;
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
/**
|
131 |
|
|
* Implements hook_token_info_alter().
|
132 |
|
|
*/
|
133 |
|
|
function file_entity_token_info_alter(&$info) {
|
134 |
|
|
$info['tokens']['file']['name']['description'] = t('The name of the file.');
|
135 |
|
|
}
|
136 |
|
|
|
137 |
|
|
/**
|
138 |
082b75eb
|
Assos Assos
|
* Provide replacement values for placeholder tokens.
|
139 |
85ad3d82
|
Assos Assos
|
*/
|
140 |
|
|
function file_entity_tokens($type, $tokens, array $data = array(), array $options = array()) {
|
141 |
|
|
$replacements = array();
|
142 |
|
|
|
143 |
082b75eb
|
Assos Assos
|
// Check that this token call contains the data we need
|
144 |
|
|
if ($type == 'entity' && !empty($data['entity_type']) && !empty($data['entity']) &&
|
145 |
|
|
!empty($data['token_type']) && module_exists('token')) {
|
146 |
|
|
|
147 |
|
|
foreach ($tokens as $name => $original) {
|
148 |
|
|
|
149 |
|
|
// Split out the token into its parts
|
150 |
|
|
$parts = explode(':', $name, 3);
|
151 |
|
|
|
152 |
|
|
$field_name = $parts[0];
|
153 |
|
|
$property = (isset($parts[1])) ? $parts[1] : '';
|
154 |
|
|
$array_handler = (isset($parts[2])) ? $parts[2] : '';
|
155 |
|
|
|
156 |
|
|
// Check that the field has content and that we should handle it
|
157 |
|
|
if (!empty($data['entity']->$field_name) && _token_module($data['token_type'], $field_name) == 'file_entity') {
|
158 |
|
|
|
159 |
|
|
// Get basic information
|
160 |
|
|
$entity_type = $data['entity_type'];
|
161 |
f419e8de
|
Assos Assos
|
if ($entity_type == 'taxonomy_term') {
|
162 |
|
|
$entity_type = 'term';
|
163 |
|
|
}
|
164 |
082b75eb
|
Assos Assos
|
$langcode = isset($options['language']) ? $options['language']->language : NULL;
|
165 |
|
|
$entity = clone $data['entity'];
|
166 |
|
|
|
167 |
|
|
// If we are looking for the field output, let field module handle it
|
168 |
|
|
if (empty($property) || $property == 'field') {
|
169 |
|
|
unset($entity->_field_view_prepared);
|
170 |
|
|
$field_output = field_view_field($entity_type, $entity, $field_name, 'token', $langcode);
|
171 |
|
|
$field_output['#token_options'] = $options;
|
172 |
|
|
$field_output['#prerender'][] = 'token_pre_render_field_token';
|
173 |
|
|
$replacements[$original] = drupal_render($field_output);
|
174 |
|
|
}
|
175 |
|
|
else {
|
176 |
|
|
$items = field_get_items($entity_type, $entity, $field_name);
|
177 |
|
|
$return = _file_entity_tokens_get_property($items, $property, $array_handler);
|
178 |
|
|
|
179 |
|
|
// We may get a single value or an array.
|
180 |
|
|
// Handle array with the array function from token module.
|
181 |
|
|
if (is_array($return)) {
|
182 |
|
|
$search_tokens = token_find_with_prefix($tokens, $field_name);
|
183 |
|
|
if ($array_tokens = token_find_with_prefix($search_tokens, $property)) {
|
184 |
|
|
$replacements += token_generate('array', $array_tokens, array('array' => $return), $options);
|
185 |
|
|
}
|
186 |
|
|
}
|
187 |
|
|
else {
|
188 |
a8cee257
|
Assos Assos
|
if (!is_null($return)) {
|
189 |
|
|
$replacements[$original] = $return;
|
190 |
|
|
}
|
191 |
082b75eb
|
Assos Assos
|
}
|
192 |
|
|
}
|
193 |
|
|
|
194 |
|
|
// Unset clone of entity
|
195 |
|
|
unset($entity);
|
196 |
|
|
}
|
197 |
|
|
}
|
198 |
|
|
}
|
199 |
|
|
|
200 |
85ad3d82
|
Assos Assos
|
$url_options = array('absolute' => TRUE);
|
201 |
|
|
if (isset($options['language'])) {
|
202 |
|
|
$url_options['language'] = $options['language'];
|
203 |
|
|
$language_code = $options['language']->language;
|
204 |
|
|
}
|
205 |
|
|
else {
|
206 |
|
|
$language_code = NULL;
|
207 |
|
|
}
|
208 |
|
|
|
209 |
|
|
$sanitize = !empty($options['sanitize']);
|
210 |
|
|
|
211 |
|
|
// File tokens.
|
212 |
|
|
if ($type == 'file' && !empty($data['file'])) {
|
213 |
|
|
$file = $data['file'];
|
214 |
|
|
|
215 |
|
|
foreach ($tokens as $name => $original) {
|
216 |
|
|
switch ($name) {
|
217 |
|
|
case 'type':
|
218 |
|
|
if ($file_type = file_type_load($file->type)) {
|
219 |
|
|
$replacements[$original] = $sanitize ? check_plain($file_type->label) : $file_type->label;
|
220 |
|
|
}
|
221 |
|
|
break;
|
222 |
|
|
|
223 |
|
|
case 'download-url':
|
224 |
|
|
$uri = file_entity_download_uri($file);
|
225 |
|
|
$replacements[$original] = url($uri['path'], $uri['options'] + $url_options);
|
226 |
|
|
break;
|
227 |
|
|
}
|
228 |
|
|
}
|
229 |
|
|
|
230 |
|
|
// Chained token relationships.
|
231 |
|
|
if (($file_type_tokens = token_find_with_prefix($tokens, 'type')) && $file_type = file_type_load($file->type)) {
|
232 |
|
|
$replacements += token_generate('file-type', $file_type_tokens, array('file_type' => $file_type), $options);
|
233 |
|
|
}
|
234 |
|
|
if ($download_url_tokens = token_find_with_prefix($tokens, 'download-url')) {
|
235 |
|
|
$replacements += token_generate('url', $download_url_tokens, file_entity_download_uri($file), $options);
|
236 |
|
|
}
|
237 |
|
|
}
|
238 |
|
|
|
239 |
|
|
// File type tokens.
|
240 |
|
|
if ($type == 'file-type' && !empty($data['file_type'])) {
|
241 |
|
|
$file_type = $data['file_type'];
|
242 |
|
|
|
243 |
|
|
foreach ($tokens as $name => $original) {
|
244 |
|
|
switch ($name) {
|
245 |
|
|
case 'name':
|
246 |
|
|
$replacements[$original] = $sanitize ? check_plain($file_type->label) : $file_type->label;
|
247 |
|
|
break;
|
248 |
|
|
|
249 |
|
|
case 'machine-name':
|
250 |
|
|
// This is a machine name so does not ever need to be sanitized.
|
251 |
|
|
$replacements[$original] = $file_type->type;
|
252 |
|
|
break;
|
253 |
|
|
|
254 |
|
|
case 'count':
|
255 |
|
|
$query = db_select('file_managed');
|
256 |
|
|
$query->condition('type', $file_type->type);
|
257 |
|
|
$query->addTag('file_type_file_count');
|
258 |
|
|
$count = $query->countQuery()->execute()->fetchField();
|
259 |
|
|
$replacements[$original] = (int) $count;
|
260 |
|
|
break;
|
261 |
|
|
|
262 |
|
|
case 'edit-url':
|
263 |
|
|
$replacements[$original] = url('admin/structure/file-types/manage/' . $file_type->type . '/fields', $url_options);
|
264 |
|
|
break;
|
265 |
|
|
}
|
266 |
|
|
}
|
267 |
|
|
}
|
268 |
|
|
|
269 |
|
|
return $replacements;
|
270 |
|
|
}
|
271 |
082b75eb
|
Assos Assos
|
|
272 |
|
|
/**
|
273 |
|
|
* This helper function gets file properties for token replacement.
|
274 |
|
|
*
|
275 |
|
|
* @param array $files
|
276 |
|
|
* An array of files that are values for the field.
|
277 |
|
|
*
|
278 |
|
|
* @param string $property
|
279 |
|
|
* The property to retrieve from the file info. See file_entity_token_info() for
|
280 |
|
|
* a list of properties.
|
281 |
|
|
*
|
282 |
|
|
* @param string $array_handler
|
283 |
|
|
* The optional array modifier, e.g. "count" or "join:,".
|
284 |
|
|
*
|
285 |
|
|
* @return mixed
|
286 |
|
|
* Either a single value, the first of the array, or an array of values.
|
287 |
|
|
*/
|
288 |
|
|
function _file_entity_tokens_get_property($files, $property, $array_handler = 'first') {
|
289 |
211d6cee
|
Assos Assos
|
$value = NULL;
|
290 |
082b75eb
|
Assos Assos
|
// If we only need the first variable
|
291 |
|
|
$return_first = ($array_handler == 'first' || empty($array_handler) || $array_handler == 'value:0');
|
292 |
|
|
|
293 |
|
|
// This static variable stores image info
|
294 |
|
|
$info = &drupal_static(__FUNCTION__);
|
295 |
|
|
|
296 |
|
|
foreach ($files as $file) {
|
297 |
|
|
$file['url'] = file_create_url($file['uri']);
|
298 |
|
|
$file['https-url'] = str_replace('http://', 'https://', $file['url']);
|
299 |
|
|
|
300 |
|
|
// If values are: filename, filemime, type, url, https-url
|
301 |
|
|
if (isset($file[$property])) {
|
302 |
|
|
$value = $file[$property];
|
303 |
|
|
}
|
304 |
|
|
|
305 |
|
|
// If values are: image, height, width, https-image
|
306 |
|
|
elseif (!empty($info[$file['fid']])) {
|
307 |
211d6cee
|
Assos Assos
|
if (isset($info[$file['fid']][$property])) {
|
308 |
|
|
$value = $info[$file['fid']][$property];
|
309 |
|
|
} else {
|
310 |
|
|
$value = NULL;
|
311 |
|
|
}
|
312 |
082b75eb
|
Assos Assos
|
}
|
313 |
|
|
// If values are files types
|
314 |
|
|
else {
|
315 |
|
|
|
316 |
|
|
// If file type is image
|
317 |
|
|
if ($file['type'] == 'image') {
|
318 |
|
|
$imageuri = $file['uri'];
|
319 |
|
|
}
|
320 |
|
|
|
321 |
|
|
// If file type is video
|
322 |
a8cee257
|
Assos Assos
|
elseif ($file['type'] == 'video' && strpos($file['uri'], '://v')) {
|
323 |
|
|
list($provider, $filename) = preg_split('!://v.*/!', $file['uri']);
|
324 |
082b75eb
|
Assos Assos
|
$imageuri = "public://file_entity-$provider/$filename.jpg";
|
325 |
|
|
}
|
326 |
|
|
|
327 |
|
|
// Do nothing for other file types
|
328 |
|
|
// @todo: Other file types may need handling
|
329 |
|
|
else {
|
330 |
|
|
$imageuri = FALSE;
|
331 |
|
|
}
|
332 |
|
|
|
333 |
|
|
if ($info[$file['fid']] = image_get_info($imageuri)) {
|
334 |
|
|
$info[$file['fid']]['image'] = file_create_url($imageuri);
|
335 |
|
|
$info[$file['fid']]['https-image'] = str_replace('http://', 'https://', $info[$file['fid']]['image']);
|
336 |
|
|
}
|
337 |
|
|
|
338 |
211d6cee
|
Assos Assos
|
if (isset($info[$file['fid']][$property])) {
|
339 |
|
|
$value = $info[$file['fid']][$property];
|
340 |
|
|
} else {
|
341 |
|
|
$value = NULL;
|
342 |
|
|
}
|
343 |
082b75eb
|
Assos Assos
|
}
|
344 |
|
|
|
345 |
|
|
if ($return_first) {
|
346 |
|
|
return $value;
|
347 |
|
|
}
|
348 |
|
|
$values[] = $value;
|
349 |
|
|
}
|
350 |
|
|
|
351 |
|
|
return $values;
|
352 |
|
|
} |