1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* File hooks implemented by the File entity module.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements hook_file_presave().
|
10
|
*/
|
11
|
function file_entity_file_presave($file) {
|
12
|
// Always ensure the filemime property is current.
|
13
|
if (!empty($file->original) || empty($file->filemime)) {
|
14
|
$file->filemime = file_get_mimetype($file->uri);
|
15
|
}
|
16
|
|
17
|
// The file type is used as a bundle key, and therefore, must not be NULL.
|
18
|
// It defaults to FILE_TYPE_NONE when loaded via file_load(), but in case
|
19
|
// file_save() is called on a new file object, default it here too.
|
20
|
if (!isset($file->type)) {
|
21
|
$file->type = FILE_TYPE_NONE;
|
22
|
}
|
23
|
|
24
|
// If the file isn't already assigned a real type, determine what type should
|
25
|
// be assigned to it.
|
26
|
if ($file->type === FILE_TYPE_NONE) {
|
27
|
$type = file_get_type($file);
|
28
|
if (isset($type)) {
|
29
|
$file->type = $type;
|
30
|
}
|
31
|
}
|
32
|
|
33
|
field_attach_presave('file', $file);
|
34
|
|
35
|
// Fetch image dimensions.
|
36
|
file_entity_metadata_fetch_image_dimensions($file);
|
37
|
}
|
38
|
|
39
|
/**
|
40
|
* Implements hook_file_type().
|
41
|
*/
|
42
|
function file_entity_file_type($file) {
|
43
|
$types = array();
|
44
|
foreach (file_type_get_enabled_types() as $type) {
|
45
|
if (file_entity_match_mimetypes($type->mimetypes, $file->filemime)) {
|
46
|
$types[] = $type->type;
|
47
|
}
|
48
|
}
|
49
|
|
50
|
return $types;
|
51
|
}
|
52
|
|
53
|
/**
|
54
|
* Implements hook_file_insert().
|
55
|
*/
|
56
|
function file_entity_file_insert($file) {
|
57
|
// Ensure field data is saved since file_save() does not in Drupal 7.
|
58
|
field_attach_insert('file', $file);
|
59
|
|
60
|
// Save file metadata.
|
61
|
if (!empty($file->metadata)) {
|
62
|
$query = db_insert('file_metadata')->fields(array('fid', 'name', 'value'));
|
63
|
foreach ($file->metadata as $name => $value) {
|
64
|
$query->values(array(
|
65
|
'fid' => $file->fid,
|
66
|
'name' => $name,
|
67
|
'value' => serialize($value),
|
68
|
));
|
69
|
}
|
70
|
$query->execute();
|
71
|
}
|
72
|
|
73
|
// Clear any related field caches.
|
74
|
file_entity_invalidate_field_caches($file);
|
75
|
}
|
76
|
|
77
|
/**
|
78
|
* Implements hook_file_update().
|
79
|
*/
|
80
|
function file_entity_file_update($file) {
|
81
|
// Ensure field data is saved since file_save() does not in Drupal 7.
|
82
|
field_attach_update('file', $file);
|
83
|
|
84
|
// Save file metadata.
|
85
|
db_delete('file_metadata')->condition('fid', $file->fid)->execute();
|
86
|
if (!empty($file->metadata)) {
|
87
|
$query = db_insert('file_metadata')->fields(array('fid', 'name', 'value'));
|
88
|
foreach ($file->metadata as $name => $value) {
|
89
|
$query->values(array(
|
90
|
'fid' => $file->fid,
|
91
|
'name' => $name,
|
92
|
'value' => serialize($value),
|
93
|
));
|
94
|
}
|
95
|
$query->execute();
|
96
|
}
|
97
|
|
98
|
if (module_exists('image') && file_entity_file_get_mimetype_type($file) == 'image' && $file->filesize && isset($file->original)) {
|
99
|
if (!isset($file->metadata)) {
|
100
|
$file->metadata = array();
|
101
|
}
|
102
|
|
103
|
if (!isset($file->original->metadata)) {
|
104
|
if (!is_object($file->original)) {
|
105
|
$file->original = new stdClass();
|
106
|
}
|
107
|
$file->original->metadata = array();
|
108
|
}
|
109
|
|
110
|
// If the file has changed dimensions or a new file has been uploaded,
|
111
|
// update any image field reference to this file and flush image style
|
112
|
// derivatives.
|
113
|
$file->metadata += array('width' => NULL, 'height' => NULL);
|
114
|
$file->original->metadata += array('width' => NULL, 'height' => NULL);
|
115
|
if ($file->filesize != $file->original->filesize || $file->metadata['width'] != $file->original->metadata['width'] || $file->metadata['height'] != $file->original->metadata['height']) {
|
116
|
_file_entity_update_image_field_dimensions($file);
|
117
|
}
|
118
|
|
119
|
// Flush image style derivatives whenever an image is replaced.
|
120
|
if (empty($file->file_entity_skip_image_flush) && file_entity_has_file_changed($file)) {
|
121
|
image_path_flush($file->uri);
|
122
|
}
|
123
|
}
|
124
|
|
125
|
// Clear any related field caches.
|
126
|
file_entity_invalidate_field_caches($file);
|
127
|
}
|
128
|
|
129
|
/**
|
130
|
* Returns whether the file has changed
|
131
|
*/
|
132
|
function file_entity_has_file_changed($file) {
|
133
|
return empty($file->is_new) || empty($file->original) || $file->filesize != $file->original->filesize || $file->uri != $file->original->uri;
|
134
|
}
|
135
|
|
136
|
|
137
|
/**
|
138
|
* Implements hook_file_delete().
|
139
|
*/
|
140
|
function file_entity_file_delete($file) {
|
141
|
field_attach_delete('file', $file);
|
142
|
|
143
|
// This is safe to call since the file's records from the usage table have
|
144
|
// not yet been deleted.
|
145
|
file_entity_invalidate_field_caches($file);
|
146
|
|
147
|
// Remove file metadata.
|
148
|
db_delete('file_metadata')->condition('fid', $file->fid)->execute();
|
149
|
|
150
|
// Remove this file from the search index if needed.
|
151
|
// This code is implemented in file entity module rather than in search
|
152
|
// module because file entity is implementing search module's API, not the
|
153
|
// other way around.
|
154
|
if (module_exists('search')) {
|
155
|
search_reindex($file->fid, 'file');
|
156
|
}
|
157
|
}
|
158
|
|
159
|
/**
|
160
|
* Implements hook_file_mimetype_mapping_alter().
|
161
|
*/
|
162
|
function file_entity_file_mimetype_mapping_alter(&$mapping) {
|
163
|
// For info on adding new mime-types to core: http://drupal.org/node/1443070.
|
164
|
|
165
|
// @todo. Remove after fixing this in core http://drupal.org/node/2912875.
|
166
|
// Add support for autocad drawings.
|
167
|
$new_mappings['dwg'] = 'application/acad';
|
168
|
// For info on adding new mime-types to file_entity: drupal.org/node/2900830.
|
169
|
|
170
|
foreach ($new_mappings as $extension => $mime_type) {
|
171
|
if (!in_array($mime_type, $mapping['mimetypes'])) {
|
172
|
// If the mime type does not already exist, add it.
|
173
|
$mapping['mimetypes'][] = $mime_type;
|
174
|
}
|
175
|
|
176
|
// Get the index of the mime type and assign the extension to that key.
|
177
|
$index = array_search($mime_type, $mapping['mimetypes']);
|
178
|
$mapping['extensions'][$extension] = $index;
|
179
|
}
|
180
|
}
|
181
|
|
182
|
/**
|
183
|
* Implements hook_file_load().
|
184
|
*/
|
185
|
function file_entity_file_load($files) {
|
186
|
// Add alt and title text to images.
|
187
|
file_entity_set_title_alt_properties($files);
|
188
|
|
189
|
// Add metadata to each file.
|
190
|
foreach ($files as $file) {
|
191
|
$file->metadata = array();
|
192
|
}
|
193
|
$results = db_query("SELECT * FROM {file_metadata} WHERE fid IN (:fids)", array(':fids' => array_keys($files)));
|
194
|
|
195
|
foreach ($results as $result) {
|
196
|
$name = $result->name;
|
197
|
|
198
|
// image.module required height and width to be properties of the file.
|
199
|
if ($name == 'height' || $name == 'width') {
|
200
|
$files[$result->fid]->$name = unserialize($result->value);
|
201
|
}
|
202
|
|
203
|
$files[$result->fid]->metadata[$name] = unserialize($result->value);
|
204
|
}
|
205
|
}
|
206
|
|
207
|
/**
|
208
|
* Implements hook_entitycache_ENTITY_TYPE_load().
|
209
|
*/
|
210
|
function file_entity_entitycache_file_load($files) {
|
211
|
// Integrates with entitycache - ensures the alt and title text on images is
|
212
|
// localized.
|
213
|
file_entity_set_title_alt_properties($files);
|
214
|
}
|
215
|
|
216
|
/**
|
217
|
* Implements hook_entity_load().
|
218
|
*/
|
219
|
function file_entity_entity_load($entities, $entity_type) {
|
220
|
file_entity_set_title_alt_properties_on_file_fields($entities, $entity_type);
|
221
|
}
|
222
|
|
223
|
/**
|
224
|
* Sets the title / alt properties on file fields attached to entities.
|
225
|
*
|
226
|
* Files attached to a file or image field can be stored in the field cache or
|
227
|
* entity cache for whichever entity that the field is attached to. Because
|
228
|
* $file->alt and $file->title are set in file_entity_file_load() based on the
|
229
|
* current page language, they will go into the cache with that language as
|
230
|
* well. To ensure that the correct language is used when the entity is later
|
231
|
* loaded and displayed in a different language, the alt and title properties
|
232
|
* can be set again using this function.
|
233
|
*
|
234
|
* @param array $entities
|
235
|
* An array of entity objects of the same type.
|
236
|
* @param string $entity_type
|
237
|
* The type of entity.
|
238
|
*
|
239
|
* @see file_entity_entity_load()
|
240
|
* @see file_entity_entitycache_load()
|
241
|
*/
|
242
|
function file_entity_set_title_alt_properties_on_file_fields($entities, $entity_type) {
|
243
|
foreach ($entities as $entity) {
|
244
|
list(, , $bundle) = entity_extract_ids($entity_type, $entity);
|
245
|
foreach (field_info_instances($entity_type, $bundle) as $instance) {
|
246
|
if (!empty($entity->{$instance['field_name']})) {
|
247
|
foreach ($entity->{$instance['field_name']} as &$items) {
|
248
|
foreach ($items as &$item) {
|
249
|
// We need to detect any field items that passed through
|
250
|
// file_field_load(), whether they are files, images, or something
|
251
|
// else. There is no direct way to do that, but checking for a few
|
252
|
// expected file properties on the field item should be sufficient.
|
253
|
if (is_array($item) && !empty($item['fid']) && isset($item['uri']) && isset($item['filename'])) {
|
254
|
$file = (object) $item;
|
255
|
file_entity_set_title_alt_properties(array($file));
|
256
|
$item = (array) $file;
|
257
|
}
|
258
|
}
|
259
|
}
|
260
|
}
|
261
|
}
|
262
|
}
|
263
|
}
|
264
|
|
265
|
/**
|
266
|
* Set the title / alt properties of file objects.
|
267
|
*
|
268
|
* @param array $files
|
269
|
* List of file entities.
|
270
|
* @param stdClass $language
|
271
|
* (optional) A language object to use for translating the title and alt
|
272
|
* properties. Defaults to the language of the current request.
|
273
|
*/
|
274
|
function file_entity_set_title_alt_properties($files, $language = NULL) {
|
275
|
if (!isset($language)) {
|
276
|
$language = $GLOBALS['language'];
|
277
|
}
|
278
|
|
279
|
$alt = variable_get('file_entity_alt', '[file:field_file_image_alt_text]');
|
280
|
$title = variable_get('file_entity_title', '[file:field_file_image_title_text]');
|
281
|
|
282
|
$replace_options = array(
|
283
|
'clear' => TRUE,
|
284
|
'sanitize' => FALSE,
|
285
|
'language' => $language,
|
286
|
);
|
287
|
|
288
|
foreach ($files as $file) {
|
289
|
// Load alt and title text from fields.
|
290
|
if (!empty($alt)) {
|
291
|
$output = token_replace($alt, array('file' => $file), $replace_options);
|
292
|
|
293
|
if (!empty($output)) {
|
294
|
// @todo Remove once https://www.drupal.org/node/1713164 is fixed.
|
295
|
// There is currently no way to get the raw alt text returned from the
|
296
|
// token so we revert the encoding done during tokenization.
|
297
|
$file->alt = decode_entities($output);
|
298
|
}
|
299
|
}
|
300
|
if (!empty($title)) {
|
301
|
$output = token_replace($title, array('file' => $file), $replace_options);
|
302
|
|
303
|
if (!empty($output)) {
|
304
|
// @todo Remove once https://www.drupal.org/node/1713164 is fixed.
|
305
|
// There is currently no way to get the raw title text returned from the
|
306
|
// token so we revert the encoding done during tokenization.
|
307
|
$file->title = decode_entities($output);
|
308
|
}
|
309
|
}
|
310
|
}
|
311
|
}
|
312
|
|
313
|
/**
|
314
|
* Fetch the dimensions of an image and store them in the file metadata array.
|
315
|
*/
|
316
|
function file_entity_metadata_fetch_image_dimensions($file) {
|
317
|
// Prevent PHP notices when trying to read empty files.
|
318
|
// @see http://drupal.org/node/681042
|
319
|
if (!$file->filesize) {
|
320
|
return;
|
321
|
}
|
322
|
|
323
|
// Do not bother proceeding if this file does not have an image mime type.
|
324
|
if (file_entity_file_get_mimetype_type($file) != 'image') {
|
325
|
return;
|
326
|
}
|
327
|
|
328
|
// We have a non-empty image file.
|
329
|
$image_info = image_get_info($file->uri);
|
330
|
if ($image_info) {
|
331
|
$file->metadata['width'] = $image_info['width'];
|
332
|
$file->metadata['height'] = $image_info['height'];
|
333
|
}
|
334
|
else {
|
335
|
// Fallback to NULL values.
|
336
|
$file->metadata['width'] = NULL;
|
337
|
$file->metadata['height'] = NULL;
|
338
|
}
|
339
|
}
|
340
|
|
341
|
/**
|
342
|
* Update the image dimensions stored in any image fields for a file.
|
343
|
*
|
344
|
* @param object $file
|
345
|
* A file object that is an image.
|
346
|
*
|
347
|
* @see http://drupal.org/node/1448124
|
348
|
*/
|
349
|
function _file_entity_update_image_field_dimensions($file) {
|
350
|
// Prevent PHP notices when trying to read empty files.
|
351
|
// @see http://drupal.org/node/681042
|
352
|
if (!$file->filesize) {
|
353
|
return;
|
354
|
}
|
355
|
|
356
|
// Do not bother proceeding if this file does not have an image mime type.
|
357
|
if (file_entity_file_get_mimetype_type($file) != 'image') {
|
358
|
return;
|
359
|
}
|
360
|
|
361
|
// Find all image field enabled on the site.
|
362
|
$image_fields = _file_entity_get_fields_by_type('image');
|
363
|
|
364
|
foreach ($image_fields as $image_field) {
|
365
|
$query = new EntityFieldQuery();
|
366
|
$query->fieldCondition($image_field, 'fid', $file->fid);
|
367
|
$results = $query->execute();
|
368
|
|
369
|
foreach ($results as $entity_type => $entities) {
|
370
|
$entities = entity_load($entity_type, array_keys($entities));
|
371
|
foreach ($entities as $entity) {
|
372
|
foreach ($entity->{$image_field} as $langcode => $items) {
|
373
|
foreach ($items as $delta => $item) {
|
374
|
if ($item['fid'] == $file->fid) {
|
375
|
$entity->{$image_field}[$langcode][$delta]['width'] = $file->metadata['width'];
|
376
|
$entity->{$image_field}[$langcode][$delta]['height'] = $file->metadata['height'];
|
377
|
}
|
378
|
}
|
379
|
}
|
380
|
|
381
|
// Save the updated field column values.
|
382
|
_file_entity_entity_fields_update($entity_type, $entity);
|
383
|
}
|
384
|
}
|
385
|
}
|
386
|
}
|
387
|
|
388
|
/**
|
389
|
* Update an entity's field values without changing anything on the entity.
|
390
|
*/
|
391
|
function _file_entity_entity_fields_update($entity_type, $entity) {
|
392
|
list($id) = entity_extract_ids($entity_type, $entity);
|
393
|
if (empty($id)) {
|
394
|
throw new Exception(t('Cannot call _file_entity_update_entity_fields() on a new entity.'));
|
395
|
}
|
396
|
|
397
|
// Some modules use the original property.
|
398
|
if (!isset($entity->original)) {
|
399
|
$entity->original = $entity;
|
400
|
}
|
401
|
|
402
|
// Ensure that file_field_update() will not trigger additional usage.
|
403
|
unset($entity->revision);
|
404
|
|
405
|
// Invoke the field presave and update hooks.
|
406
|
field_attach_presave($entity_type, $entity);
|
407
|
field_attach_update($entity_type, $entity);
|
408
|
|
409
|
// Clear the cache for this entity now.
|
410
|
entity_get_controller($entity_type)->resetCache(array($id));
|
411
|
}
|
412
|
|
413
|
/**
|
414
|
* Implements hook_file_metadata_info().
|
415
|
*/
|
416
|
function file_entity_file_metadata_info() {
|
417
|
$info['width'] = array('label' => t('Width'), 'type' => 'integer');
|
418
|
$info['height'] = array('label' => t('Height'), 'type' => 'integer');
|
419
|
return $info;
|
420
|
}
|