Projet

Général

Profil

Paste
Télécharger (9,11 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / file_entity / file_entity.file.inc @ ca0757b9

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) {
99
    // If the file has changed dimensions or a new file has been uploaded,
100
    // update any image field reference to this file and flush image style
101
    // derivatives.
102
    $file->metadata += array('width' => NULL, 'height' => NULL);
103
    $file->original->metadata += array('width' => NULL, 'height' => NULL);
104
    if ($file->filesize != $file->original->filesize || $file->metadata['width'] != $file->original->metadata['width'] || $file->metadata['height'] != $file->original->metadata['height']) {
105
      _file_entity_update_image_field_dimensions($file);
106
    }
107

    
108
    // Flush image style derivatives whenever an image is updated.
109
    image_path_flush($file->uri);
110
  }
111

    
112
  // Clear any related field caches.
113
  file_entity_invalidate_field_caches($file);
114
}
115

    
116
/**
117
 * Implements hook_file_delete().
118
 */
119
function file_entity_file_delete($file) {
120
  field_attach_delete('file', $file);
121

    
122
  // This is safe to call since the file's records from the usage table have
123
  // not yet been deleted.
124
  file_entity_invalidate_field_caches($file);
125

    
126
  // Remove file metadata.
127
  db_delete('file_metadata')->condition('fid', $file->fid)->execute();
128

    
129
  // Remove this file from the search index if needed.
130
  // This code is implemented in file entity module rather than in search
131
  // module because file entity is implementing search module's API, not the
132
  // other way around.
133
  if (module_exists('search')) {
134
    search_reindex($file->fid, 'file');
135
  }
136
}
137

    
138
/**
139
 * Implements hook_file_mimetype_mapping_alter().
140
 */
141
function file_entity_file_mimetype_mapping_alter(&$mapping) {
142
  // Add support for mka and mkv.
143
  // @todo Remove when http://drupal.org/node/1443070 is fixed in core.
144
  $new_mappings['mka'] = 'audio/x-matroska';
145
  $new_mappings['mkv'] = 'video/x-matroska';
146

    
147
  // Add support for weba, webm, and webp.
148
  // @todo Remove when http://drupal.org/node/1443070 is fixed in core.
149
  $new_mappings['weba'] = 'audio/webm';
150
  $new_mappings['webm'] = 'video/webm';
151
  $new_mappings['webp'] = 'image/webp';
152

    
153
  foreach ($new_mappings as $extension => $mime_type) {
154
    if (!in_array($mime_type, $mapping['mimetypes'])) {
155
      // If the mime type does not already exist, add it.
156
      $mapping['mimetypes'][] = $mime_type;
157
    }
158

    
159
    // Get the index of the mime type and assign the extension to that key.
160
    $index = array_search($mime_type, $mapping['mimetypes']);
161
    $mapping['extensions'][$extension] = $index;
162
  }
163
}
164

    
165
/**
166
 * Implements hook_file_load().
167
 */
168
function file_entity_file_load($files) {
169
  $alt = variable_get('file_entity_alt', '[file:field_file_image_alt_text]');
170
  $title = variable_get('file_entity_title', '[file:field_file_image_title_text]');
171

    
172
  $replace_options = array(
173
    'clear' => TRUE,
174
    'sanitize' => FALSE,
175
  );
176

    
177
  foreach ($files as $file) {
178
    $file->metadata = array();
179

    
180
    // Load alt and title text from fields.
181
    if (!empty($alt)) {
182
      $file->alt = token_replace($alt, array('file' => $file), $replace_options);
183
    }
184
    if (!empty($title)) {
185
      $file->title = token_replace($title, array('file' => $file), $replace_options);
186
    }
187
  }
188

    
189
  // Load and unserialize metadata.
190
  $results = db_query("SELECT * FROM {file_metadata} WHERE fid IN (:fids)", array(':fids' => array_keys($files)));
191
  foreach ($results as $result) {
192
    $files[$result->fid]->metadata[$result->name] = unserialize($result->value);
193
  }
194
}
195

    
196
/**
197
 * Fetch the dimensions of an image and store them in the file metadata array.
198
 */
199
function file_entity_metadata_fetch_image_dimensions($file) {
200
  // Prevent PHP notices when trying to read empty files.
201
  // @see http://drupal.org/node/681042
202
  if (!$file->filesize) {
203
    return;
204
  }
205

    
206
  // Do not bother proceeding if this file does not have an image mime type.
207
  if (file_entity_file_get_mimetype_type($file) != 'image') {
208
    return;
209
  }
210

    
211
  // We have a non-empty image file.
212
  $image_info = image_get_info($file->uri);
213
  if ($image_info) {
214
    $file->metadata['width'] = $image_info['width'];
215
    $file->metadata['height'] = $image_info['height'];
216
  }
217
  else {
218
    // Fallback to NULL values.
219
    $file->metadata['width'] = NULL;
220
    $file->metadata['height'] = NULL;
221
  }
222
}
223

    
224
/**
225
 * Update the image dimensions stored in any image fields for a file.
226
 *
227
 * @param object $file
228
 *   A file object that is an image.
229
 *
230
 * @see http://drupal.org/node/1448124
231
 */
232
function _file_entity_update_image_field_dimensions($file) {
233
  // Prevent PHP notices when trying to read empty files.
234
  // @see http://drupal.org/node/681042
235
  if (!$file->filesize) {
236
    return;
237
  }
238

    
239
  // Do not bother proceeding if this file does not have an image mime type.
240
  if (file_entity_file_get_mimetype_type($file) != 'image') {
241
    return;
242
  }
243

    
244
  // Find all image field enabled on the site.
245
  $image_fields = _file_entity_get_fields_by_type('image');
246

    
247
  foreach ($image_fields as $image_field) {
248
    $query = new EntityFieldQuery();
249
    $query->fieldCondition($image_field, 'fid', $file->fid);
250
    $results = $query->execute();
251

    
252
    foreach ($results as $entity_type => $entities) {
253
      $entities = entity_load($entity_type, array_keys($entities));
254
      foreach ($entities as $entity) {
255
        foreach ($entity->{$image_field} as $langcode => $items) {
256
          foreach ($items as $delta => $item) {
257
            if ($item['fid'] == $file->fid) {
258
              $entity->{$image_field}[$langcode][$delta]['width'] = $file->metadata['width'];
259
              $entity->{$image_field}[$langcode][$delta]['height'] = $file->metadata['height'];
260
            }
261
          }
262
        }
263

    
264
        // Save the updated field column values.
265
        _file_entity_entity_fields_update($entity_type, $entity);
266
      }
267
    }
268
  }
269
}
270

    
271
/**
272
 * Update an entity's field values without changing anything on the entity.
273
 */
274
function _file_entity_entity_fields_update($entity_type, $entity) {
275
  list($id) = entity_extract_ids($entity_type, $entity);
276
  if (empty($id)) {
277
    throw new Exception(t('Cannot call _file_entity_update_entity_fields() on a new entity.'));
278
  }
279

    
280
  // Some modules use the original property.
281
  if (!isset($entity->original)) {
282
    $entity->original = $entity;
283
  }
284

    
285
  // Ensure that file_field_update() will not trigger additional usage.
286
  unset($entity->revision);
287

    
288
  // Invoke the field presave and update hooks.
289
  field_attach_presave($entity_type, $entity);
290
  field_attach_update($entity_type, $entity);
291

    
292
  // Clear the cache for this entity now.
293
  entity_get_controller($entity_type)->resetCache(array($id));
294
}
295

    
296
/**
297
 * Implements hook_file_metadata_info().
298
 */
299
function file_entity_file_metadata_info() {
300
  $info['width'] = array('label' => t('Width'), 'type' => 'integer');
301
  $info['height'] = array('label' => t('Height'), 'type' => 'integer');
302
  return $info;
303
}