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
|
foreach ($file->metadata as $name => $value) {
|
63
|
db_merge('file_metadata')
|
64
|
->fields(array(
|
65
|
'value' => serialize($value),
|
66
|
))
|
67
|
->key(array(
|
68
|
'fid' => $file->fid,
|
69
|
'name' => $name,
|
70
|
))
|
71
|
->execute();
|
72
|
}
|
73
|
}
|
74
|
|
75
|
// Clear any related field caches.
|
76
|
file_entity_invalidate_field_caches($file);
|
77
|
}
|
78
|
|
79
|
/**
|
80
|
* Implements hook_file_update().
|
81
|
*/
|
82
|
function file_entity_file_update($file) {
|
83
|
// Ensure field data is saved since file_save() does not in Drupal 7.
|
84
|
field_attach_update('file', $file);
|
85
|
|
86
|
// Save file metadata.
|
87
|
if (!empty($file->metadata)) {
|
88
|
foreach ($file->metadata as $name => $value) {
|
89
|
db_merge('file_metadata')
|
90
|
->fields(array(
|
91
|
'value' => serialize($value),
|
92
|
))
|
93
|
->key(array(
|
94
|
'fid' => $file->fid,
|
95
|
'name' => $name,
|
96
|
))
|
97
|
->execute();
|
98
|
}
|
99
|
}
|
100
|
|
101
|
// Save file metadata.
|
102
|
db_delete('file_metadata')->condition('fid', $file->fid);
|
103
|
if (!empty($file->metadata)) {
|
104
|
foreach ($file->metadata as $name => $value) {
|
105
|
db_merge('file_metadata')
|
106
|
->fields(array(
|
107
|
'value' => serialize($value),
|
108
|
))
|
109
|
->key(array(
|
110
|
'fid' => $file->fid,
|
111
|
'name' => $name,
|
112
|
))
|
113
|
->execute();
|
114
|
}
|
115
|
}
|
116
|
|
117
|
if (file_entity_file_get_mimetype_type($file) == 'image' && module_exists('image')) {
|
118
|
// If the image dimensions have changed, update any image field references
|
119
|
// to this file and flush image style derivatives.
|
120
|
if ($file->metadata['width'] != $file->metadata['width'] || $file->metadata['height'] != $file->metadata['height']) {
|
121
|
_file_entity_update_image_field_dimensions($file);
|
122
|
}
|
123
|
|
124
|
// Flush image style derivatives whenever an image is updated.
|
125
|
image_path_flush($file->uri);
|
126
|
}
|
127
|
|
128
|
// Clear any related field caches.
|
129
|
file_entity_invalidate_field_caches($file);
|
130
|
}
|
131
|
|
132
|
/**
|
133
|
* Implements hook_file_delete().
|
134
|
*/
|
135
|
function file_entity_file_delete($file) {
|
136
|
field_attach_delete('file', $file);
|
137
|
|
138
|
// This is safe to call since the file's records from the usage table have
|
139
|
// not yet been deleted.
|
140
|
file_entity_invalidate_field_caches($file);
|
141
|
|
142
|
// Remove file metadata.
|
143
|
db_delete('file_metadata')->condition('fid', $file->fid)->execute();
|
144
|
|
145
|
// Remove this file from the search index if needed.
|
146
|
// This code is implemented in file entity module rather than in search
|
147
|
// module because file entity is implementing search module's API, not the
|
148
|
// other way around.
|
149
|
if (module_exists('search')) {
|
150
|
search_reindex($file->fid, 'file');
|
151
|
}
|
152
|
}
|
153
|
|
154
|
/**
|
155
|
* Implements hook_file_mimetype_mapping_alter().
|
156
|
*/
|
157
|
function file_entity_file_mimetype_mapping_alter(&$mapping) {
|
158
|
// Add support for mka and mkv.
|
159
|
// @todo Remove when http://drupal.org/node/1443070 is fixed in core.
|
160
|
$new_mappings['mka'] = 'audio/x-matroska';
|
161
|
$new_mappings['mkv'] = 'video/x-matroska';
|
162
|
|
163
|
// Add support for weba, webm, and webp.
|
164
|
// @todo Remove when http://drupal.org/node/1443070 is fixed in core.
|
165
|
$new_mappings['weba'] = 'audio/webm';
|
166
|
$new_mappings['webm'] = 'video/webm';
|
167
|
$new_mappings['webp'] = 'image/webp';
|
168
|
|
169
|
foreach ($new_mappings as $extension => $mime_type) {
|
170
|
if (!in_array($mime_type, $mapping['mimetypes'])) {
|
171
|
// If the mime type does not already exist, add it.
|
172
|
$mapping['mimetypes'][] = $mime_type;
|
173
|
}
|
174
|
|
175
|
// Get the index of the mime type and assign the extension to that key.
|
176
|
$index = array_search($mime_type, $mapping['mimetypes']);
|
177
|
$mapping['extensions'][$extension] = $index;
|
178
|
}
|
179
|
}
|
180
|
|
181
|
/**
|
182
|
* Implements hook_file_load().
|
183
|
*/
|
184
|
function file_entity_file_load($files) {
|
185
|
$alt = variable_get('file_entity_alt', '[file:field_file_image_alt_text]');
|
186
|
$title = variable_get('file_entity_title', '[file:field_file_image_title_text]');
|
187
|
|
188
|
$replace_options = array(
|
189
|
'clear' => TRUE,
|
190
|
'sanitize' => FALSE,
|
191
|
);
|
192
|
|
193
|
foreach ($files as $file) {
|
194
|
$file->metadata = array();
|
195
|
|
196
|
// Load alt and title text from fields.
|
197
|
if (!empty($alt)) {
|
198
|
$file->alt = token_replace($alt, array('file' => $file), $replace_options);
|
199
|
}
|
200
|
if (!empty($title)) {
|
201
|
$file->title = token_replace($title, array('file' => $file), $replace_options);
|
202
|
}
|
203
|
}
|
204
|
|
205
|
// Load and unserialize metadata.
|
206
|
$results = db_query("SELECT * FROM {file_metadata} WHERE fid IN (:fids)", array(':fids' => array_keys($files)));
|
207
|
foreach ($results as $result) {
|
208
|
$files[$result->fid]->metadata[$result->name] = unserialize($result->value);
|
209
|
}
|
210
|
}
|
211
|
|
212
|
/**
|
213
|
* Fetch the dimensions of an image and store them in the file metadata array.
|
214
|
*/
|
215
|
function file_entity_metadata_fetch_image_dimensions($file) {
|
216
|
// Prevent PHP notices when trying to read empty files.
|
217
|
// @see http://drupal.org/node/681042
|
218
|
if (!$file->filesize) {
|
219
|
return;
|
220
|
}
|
221
|
|
222
|
// Do not bother proceeding if this file does not have an image mime type.
|
223
|
if (file_entity_file_get_mimetype_type($file) != 'image') {
|
224
|
return;
|
225
|
}
|
226
|
|
227
|
// We have a non-empty image file.
|
228
|
$image_info = image_get_info($file->uri);
|
229
|
if ($image_info) {
|
230
|
$file->metadata['width'] = $image_info['width'];
|
231
|
$file->metadata['height'] = $image_info['height'];
|
232
|
}
|
233
|
else {
|
234
|
// Fallback to NULL values.
|
235
|
$file->metadata['width'] = NULL;
|
236
|
$file->metadata['height'] = NULL;
|
237
|
}
|
238
|
}
|
239
|
|
240
|
/**
|
241
|
* Update the image dimensions stored in any image fields for a file.
|
242
|
*
|
243
|
* @param object $file
|
244
|
* A file object that is an image.
|
245
|
*
|
246
|
* @see http://drupal.org/node/1448124
|
247
|
*/
|
248
|
function _file_entity_update_image_field_dimensions($file) {
|
249
|
// Do not bother proceeding if this file does not have an image mime type.
|
250
|
if (file_entity_file_get_mimetype_type($file) != 'image') {
|
251
|
return;
|
252
|
}
|
253
|
|
254
|
// Find all image field enabled on the site.
|
255
|
$image_fields = _file_entity_get_fields_by_type('image');
|
256
|
|
257
|
foreach ($image_fields as $image_field) {
|
258
|
$query = new EntityFieldQuery();
|
259
|
$query->fieldCondition($image_field, 'fid', $file->fid);
|
260
|
$results = $query->execute();
|
261
|
|
262
|
foreach ($results as $entity_type => $entities) {
|
263
|
$entities = entity_load($entity_type, array_keys($entities));
|
264
|
foreach ($entities as $entity) {
|
265
|
foreach ($entity->{$image_field} as $langcode => $items) {
|
266
|
foreach ($items as $delta => $item) {
|
267
|
if ($item['fid'] == $file->fid) {
|
268
|
$entity->{$image_field}[$langcode][$delta]['width'] = $file->metadata['width'];
|
269
|
$entity->{$image_field}[$langcode][$delta]['height'] = $file->metadata['height'];
|
270
|
}
|
271
|
}
|
272
|
}
|
273
|
|
274
|
// Save the updated field column values.
|
275
|
_file_entity_entity_fields_update($entity_type, $entity);
|
276
|
}
|
277
|
}
|
278
|
}
|
279
|
}
|
280
|
|
281
|
/**
|
282
|
* Update an entity's field values without changing anything on the entity.
|
283
|
*/
|
284
|
function _file_entity_entity_fields_update($entity_type, $entity) {
|
285
|
list($id) = entity_extract_ids($entity_type, $entity);
|
286
|
if (empty($id)) {
|
287
|
throw new Exception(t('Cannot call _file_entity_update_entity_fields() on a new entity.'));
|
288
|
}
|
289
|
|
290
|
// Some modules use the original property.
|
291
|
if (!isset($entity->original)) {
|
292
|
$entity->original = $entity;
|
293
|
}
|
294
|
|
295
|
// Ensure that file_field_update() will not trigger additional usage.
|
296
|
unset($entity->revision);
|
297
|
|
298
|
// Invoke the field presave and update hooks.
|
299
|
field_attach_presave($entity_type, $entity);
|
300
|
field_attach_update($entity_type, $entity);
|
301
|
|
302
|
// Clear the cache for this entity now.
|
303
|
entity_get_controller($entity_type)->resetCache(array($id));
|
304
|
}
|
305
|
|
306
|
/**
|
307
|
* Implements hook_file_metadata_info().
|
308
|
*/
|
309
|
function file_entity_file_metadata_info() {
|
310
|
$info['width'] = array('label' => t('Width'), 'type' => 'integer');
|
311
|
$info['height'] = array('label' => t('Height'), 'type' => 'integer');
|
312
|
return $info;
|
313
|
}
|