forked from md-systems/file_entity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_entity.file.inc
211 lines (184 loc) · 6.3 KB
/
file_entity.file.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/**
* @file
* File hooks implemented by the File entity module.
*/
use Drupal\file\FileInterface;
use Drupal\file_entity\Entity\FileType;
/**
* Implements hook_file_type().
*/
function file_entity_file_type(FileInterface $file) {
$types = array();
foreach (FileType::loadEnabled() as $type) {
if (file_entity_match_mimetypes($type->getMimeTypes(), $file->getMimeType())) {
$types[] = $type->id();
}
}
return $types;
}
/**
* Implements hook_file_insert().
*/
function file_entity_file_insert(FileInterface $file) {
// Save file metadata.
if (!empty($file->metadata)) {
$query = db_insert('file_metadata')->fields(array('fid', 'name', 'value'));
foreach ($file->metadata as $name => $value) {
$query->values(array(
'fid' => $file->id(),
'name' => $name,
'value' => serialize($value),
));
}
$query->execute();
}
// Clear any related field caches.
// @todo: No longer needed?
//file_entity_invalidate_field_caches($file);
}
/**
* Implements hook_file_update().
*/
function file_entity_file_update(FileInterface $file) {
// Save file metadata.
db_delete('file_metadata')->condition('fid', $file->id())->execute();
if (!empty($file->metadata)) {
$query = db_insert('file_metadata')->fields(array('fid', 'name', 'value'));
foreach ($file->metadata as $name => $value) {
$query->values(array(
'fid' => $file->id(),
'name' => $name,
'value' => serialize($value),
));
}
$query->execute();
}
if (\Drupal::moduleHandler()->moduleExists('image') && file_entity_file_get_mimetype_type($file) == 'image' && $file->getFileSize) {
// If the image dimensions have changed, update any image field references
// to this file and flush image style derivatives.
if ($file->metadata['width'] != $file->metadata['width'] || $file->metadata['height'] != $file->metadata['height']) {
_file_entity_update_image_field_dimensions($file);
}
// Flush image style derivatives whenever an image is updated.
image_path_flush($file->getFileUri());
}
// Clear any related field caches.
// @todo: No longer needed?
//file_entity_invalidate_field_caches($file);
}
/**
* Implements hook_file_delete().
*/
function file_entity_file_delete($file) {
// This is safe to call since the file's records from the usage table have
// not yet been deleted.
// @todo No longer needed?
//file_entity_invalidate_field_caches($file);
// Remove file metadata.
db_delete('file_metadata')->condition('fid', $file->id())->execute();
// Remove this file from the search index if needed.
// This code is implemented in file entity module rather than in search
// module because file entity is implementing search module's API, not the
// other way around.
if (\Drupal::moduleHandler()->moduleExists('search')) {
//search_reindex($file->id(), 'file');
}
}
/**
* Implements hook_file_mimetype_mapping_alter().
*/
function file_entity_file_mimetype_mapping_alter(&$mapping) {
// Add support for mka and mkv.
// @todo Remove when http://drupal.org/node/1443070 is fixed in core.
$new_mappings['mka'] = 'audio/x-matroska';
$new_mappings['mkv'] = 'video/x-matroska';
// Add support for weba, webm, and webp.
// @todo Remove when http://drupal.org/node/1443070 is fixed in core.
$new_mappings['weba'] = 'audio/webm';
$new_mappings['webm'] = 'video/webm';
$new_mappings['webp'] = 'image/webp';
foreach ($new_mappings as $extension => $mime_type) {
if (!in_array($mime_type, $mapping['mimetypes'])) {
// If the mime type does not already exist, add it.
$mapping['mimetypes'][] = $mime_type;
}
// Get the index of the mime type and assign the extension to that key.
$index = array_search($mime_type, $mapping['mimetypes']);
$mapping['extensions'][$extension] = $index;
}
}
/**
* Fetch the dimensions of an image and store them in the file metadata array.
*/
function file_entity_metadata_fetch_image_dimensions(FileInterface $file) {
// Prevent PHP notices when trying to read empty files.
// @see http://drupal.org/node/681042
if (!$file->getSize()) {
return;
}
// Do not bother proceeding if this file does not have an image mime type.
if (file_entity_file_get_mimetype_type($file) != 'image') {
return;
}
// We have a non-empty image file.
$image = Drupal::service('image.factory')->get($file->getFileUri());
if ($image) {
$file->metadata['width'] = $image->getWidth();
$file->metadata['height'] = $image->getHeight();
}
else {
// Fallback to NULL values.
$file->metadata['width'] = NULL;
$file->metadata['height'] = NULL;
}
}
/**
* Update the image dimensions stored in any image fields for a file.
*
* @param object $file
* A file object that is an image.
*
* @see http://drupal.org/node/1448124
*/
function _file_entity_update_image_field_dimensions($file) {
// Prevent PHP notices when trying to read empty files.
// @see http://drupal.org/node/681042
if (!$file->filesize) {
return;
}
// Do not bother proceeding if this file does not have an image mime type.
if (file_entity_file_get_mimetype_type($file) != 'image') {
return;
}
// Find all image field enabled on the site.
$image_fields = _file_entity_get_fields_by_type('image');
foreach ($image_fields as $image_field) {
$query = new EntityFieldQuery();
$query->fieldCondition($image_field, 'fid', $file->fid);
$results = $query->execute();
foreach ($results as $entity_type => $entities) {
$entities = entity_load($entity_type, array_keys($entities));
foreach ($entities as $entity) {
foreach ($entity->{$image_field} as $langcode => $items) {
foreach ($items as $delta => $item) {
if ($item['fid'] == $file->fid) {
$entity->{$image_field}[$langcode][$delta]['width'] = $file->metadata['width'];
$entity->{$image_field}[$langcode][$delta]['height'] = $file->metadata['height'];
}
}
}
// Save the updated field column values.
$entity->save();
}
}
}
}
/**
* Implements hook_file_metadata_info().
*/
function file_entity_file_metadata_info() {
$info['width'] = array('label' => t('Width'), 'type' => 'integer');
$info['height'] = array('label' => t('Height'), 'type' => 'integer');
return $info;
}