file_entity.file.inc

  1. cis7 sites/all/modules/ulmus/file_entity/file_entity.file.inc
  2. cle7 sites/all/modules/ulmus/file_entity/file_entity.file.inc
  3. ecd7 sites/all/modules/ulmus/file_entity/file_entity.file.inc
  4. elmsmedia7 sites/all/modules/ulmus/file_entity/file_entity.file.inc
  5. harmony7 sites/all/modules/ulmus/file_entity/file_entity.file.inc
  6. icor7 sites/all/modules/ulmus/file_entity/file_entity.file.inc
  7. meedjum_blog7 sites/all/modules/ulmus/file_entity/file_entity.file.inc
  8. mooc7 sites/all/modules/ulmus/file_entity/file_entity.file.inc

File hooks implemented by the File entity module.

Functions

Namesort descending Description
file_entity_file_delete Implements hook_file_delete().
file_entity_file_insert Implements hook_file_insert().
file_entity_file_load Implements hook_file_load().
file_entity_file_metadata_info Implements hook_file_metadata_info().
file_entity_file_mimetype_mapping_alter Implements hook_file_mimetype_mapping_alter().
file_entity_file_presave Implements hook_file_presave().
file_entity_file_type Implements hook_file_type().
file_entity_file_update Implements hook_file_update().
file_entity_metadata_fetch_image_dimensions Fetch the dimensions of an image and store them in the file metadata array.
_file_entity_entity_fields_update Update an entity's field values without changing anything on the entity.
_file_entity_update_image_field_dimensions Update the image dimensions stored in any image fields for a file.

File

sites/all/modules/ulmus/file_entity/file_entity.file.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * File hooks implemented by the File entity module.
  5. */
  6. /**
  7. * Implements hook_file_presave().
  8. */
  9. function file_entity_file_presave($file) {
  10. // Always ensure the filemime property is current.
  11. if (!empty($file->original) || empty($file->filemime)) {
  12. $file->filemime = file_get_mimetype($file->uri);
  13. }
  14. // The file type is used as a bundle key, and therefore, must not be NULL.
  15. // It defaults to FILE_TYPE_NONE when loaded via file_load(), but in case
  16. // file_save() is called on a new file object, default it here too.
  17. if (!isset($file->type)) {
  18. $file->type = FILE_TYPE_NONE;
  19. }
  20. // If the file isn't already assigned a real type, determine what type should
  21. // be assigned to it.
  22. if ($file->type === FILE_TYPE_NONE) {
  23. $type = file_get_type($file);
  24. if (isset($type)) {
  25. $file->type = $type;
  26. }
  27. }
  28. field_attach_presave('file', $file);
  29. // Fetch image dimensions.
  30. file_entity_metadata_fetch_image_dimensions($file);
  31. }
  32. /**
  33. * Implements hook_file_type().
  34. */
  35. function file_entity_file_type($file) {
  36. $types = array();
  37. foreach (file_type_get_enabled_types() as $type) {
  38. if (file_entity_match_mimetypes($type->mimetypes, $file->filemime)) {
  39. $types[] = $type->type;
  40. }
  41. }
  42. return $types;
  43. }
  44. /**
  45. * Implements hook_file_insert().
  46. */
  47. function file_entity_file_insert($file) {
  48. // Ensure field data is saved since file_save() does not in Drupal 7.
  49. field_attach_insert('file', $file);
  50. // Save file metadata.
  51. if (!empty($file->metadata)) {
  52. foreach ($file->metadata as $name => $value) {
  53. db_merge('file_metadata')
  54. ->fields(array(
  55. 'value' => serialize($value),
  56. ))
  57. ->key(array(
  58. 'fid' => $file->fid,
  59. 'name' => $name,
  60. ))
  61. ->execute();
  62. }
  63. }
  64. // Clear any related field caches.
  65. file_entity_invalidate_field_caches($file);
  66. }
  67. /**
  68. * Implements hook_file_update().
  69. */
  70. function file_entity_file_update($file) {
  71. // Ensure field data is saved since file_save() does not in Drupal 7.
  72. field_attach_update('file', $file);
  73. // Save file metadata.
  74. if (!empty($file->metadata)) {
  75. foreach ($file->metadata as $name => $value) {
  76. db_merge('file_metadata')
  77. ->fields(array(
  78. 'value' => serialize($value),
  79. ))
  80. ->key(array(
  81. 'fid' => $file->fid,
  82. 'name' => $name,
  83. ))
  84. ->execute();
  85. }
  86. }
  87. // Save file metadata.
  88. db_delete('file_metadata')->condition('fid', $file->fid);
  89. if (!empty($file->metadata)) {
  90. foreach ($file->metadata as $name => $value) {
  91. db_merge('file_metadata')
  92. ->fields(array(
  93. 'value' => serialize($value),
  94. ))
  95. ->key(array(
  96. 'fid' => $file->fid,
  97. 'name' => $name,
  98. ))
  99. ->execute();
  100. }
  101. }
  102. if (module_exists('image') && file_entity_file_get_mimetype_type($file) == 'image' && $file->filesize) {
  103. // If the image dimensions have changed, update any image field references
  104. // to this file and flush image style derivatives.
  105. if ($file->metadata['width'] != $file->metadata['width'] || $file->metadata['height'] != $file->metadata['height']) {
  106. _file_entity_update_image_field_dimensions($file);
  107. }
  108. // Flush image style derivatives whenever an image is updated.
  109. image_path_flush($file->uri);
  110. }
  111. // Clear any related field caches.
  112. file_entity_invalidate_field_caches($file);
  113. }
  114. /**
  115. * Implements hook_file_delete().
  116. */
  117. function file_entity_file_delete($file) {
  118. field_attach_delete('file', $file);
  119. // This is safe to call since the file's records from the usage table have
  120. // not yet been deleted.
  121. file_entity_invalidate_field_caches($file);
  122. // Remove file metadata.
  123. db_delete('file_metadata')->condition('fid', $file->fid)->execute();
  124. // Remove this file from the search index if needed.
  125. // This code is implemented in file entity module rather than in search
  126. // module because file entity is implementing search module's API, not the
  127. // other way around.
  128. if (module_exists('search')) {
  129. search_reindex($file->fid, 'file');
  130. }
  131. }
  132. /**
  133. * Implements hook_file_mimetype_mapping_alter().
  134. */
  135. function file_entity_file_mimetype_mapping_alter(&$mapping) {
  136. // Add support for mka and mkv.
  137. // @todo Remove when http://drupal.org/node/1443070 is fixed in core.
  138. $new_mappings['mka'] = 'audio/x-matroska';
  139. $new_mappings['mkv'] = 'video/x-matroska';
  140. // Add support for weba, webm, and webp.
  141. // @todo Remove when http://drupal.org/node/1443070 is fixed in core.
  142. $new_mappings['weba'] = 'audio/webm';
  143. $new_mappings['webm'] = 'video/webm';
  144. $new_mappings['webp'] = 'image/webp';
  145. foreach ($new_mappings as $extension => $mime_type) {
  146. if (!in_array($mime_type, $mapping['mimetypes'])) {
  147. // If the mime type does not already exist, add it.
  148. $mapping['mimetypes'][] = $mime_type;
  149. }
  150. // Get the index of the mime type and assign the extension to that key.
  151. $index = array_search($mime_type, $mapping['mimetypes']);
  152. $mapping['extensions'][$extension] = $index;
  153. }
  154. }
  155. /**
  156. * Implements hook_file_load().
  157. */
  158. function file_entity_file_load($files) {
  159. $alt = variable_get('file_entity_alt', '[file:field_file_image_alt_text]');
  160. $title = variable_get('file_entity_title', '[file:field_file_image_title_text]');
  161. $replace_options = array(
  162. 'clear' => TRUE,
  163. 'sanitize' => FALSE,
  164. );
  165. foreach ($files as $file) {
  166. $file->metadata = array();
  167. // Load alt and title text from fields.
  168. if (!empty($alt)) {
  169. $file->alt = token_replace($alt, array('file' => $file), $replace_options);
  170. }
  171. if (!empty($title)) {
  172. $file->title = token_replace($title, array('file' => $file), $replace_options);
  173. }
  174. }
  175. // Load and unserialize metadata.
  176. $results = db_query("SELECT * FROM {file_metadata} WHERE fid IN (:fids)", array(':fids' => array_keys($files)));
  177. foreach ($results as $result) {
  178. $files[$result->fid]->metadata[$result->name] = unserialize($result->value);
  179. }
  180. }
  181. /**
  182. * Fetch the dimensions of an image and store them in the file metadata array.
  183. */
  184. function file_entity_metadata_fetch_image_dimensions($file) {
  185. // Prevent PHP notices when trying to read empty files.
  186. // @see http://drupal.org/node/681042
  187. if (!$file->filesize) {
  188. return;
  189. }
  190. // Do not bother proceeding if this file does not have an image mime type.
  191. if (file_entity_file_get_mimetype_type($file) != 'image') {
  192. return;
  193. }
  194. // We have a non-empty image file.
  195. $image_info = image_get_info($file->uri);
  196. if ($image_info) {
  197. $file->metadata['width'] = $image_info['width'];
  198. $file->metadata['height'] = $image_info['height'];
  199. }
  200. else {
  201. // Fallback to NULL values.
  202. $file->metadata['width'] = NULL;
  203. $file->metadata['height'] = NULL;
  204. }
  205. }
  206. /**
  207. * Update the image dimensions stored in any image fields for a file.
  208. *
  209. * @param object $file
  210. * A file object that is an image.
  211. *
  212. * @see http://drupal.org/node/1448124
  213. */
  214. function _file_entity_update_image_field_dimensions($file) {
  215. // Prevent PHP notices when trying to read empty files.
  216. // @see http://drupal.org/node/681042
  217. if (!$file->filesize) {
  218. return;
  219. }
  220. // Do not bother proceeding if this file does not have an image mime type.
  221. if (file_entity_file_get_mimetype_type($file) != 'image') {
  222. return;
  223. }
  224. // Find all image field enabled on the site.
  225. $image_fields = _file_entity_get_fields_by_type('image');
  226. foreach ($image_fields as $image_field) {
  227. $query = new EntityFieldQuery();
  228. $query->fieldCondition($image_field, 'fid', $file->fid);
  229. $results = $query->execute();
  230. foreach ($results as $entity_type => $entities) {
  231. $entities = entity_load($entity_type, array_keys($entities));
  232. foreach ($entities as $entity) {
  233. foreach ($entity->{$image_field} as $langcode => $items) {
  234. foreach ($items as $delta => $item) {
  235. if ($item['fid'] == $file->fid) {
  236. $entity->{$image_field}[$langcode][$delta]['width'] = $file->metadata['width'];
  237. $entity->{$image_field}[$langcode][$delta]['height'] = $file->metadata['height'];
  238. }
  239. }
  240. }
  241. // Save the updated field column values.
  242. _file_entity_entity_fields_update($entity_type, $entity);
  243. }
  244. }
  245. }
  246. }
  247. /**
  248. * Update an entity's field values without changing anything on the entity.
  249. */
  250. function _file_entity_entity_fields_update($entity_type, $entity) {
  251. list($id) = entity_extract_ids($entity_type, $entity);
  252. if (empty($id)) {
  253. throw new Exception(t('Cannot call _file_entity_update_entity_fields() on a new entity.'));
  254. }
  255. // Some modules use the original property.
  256. if (!isset($entity->original)) {
  257. $entity->original = $entity;
  258. }
  259. // Ensure that file_field_update() will not trigger additional usage.
  260. unset($entity->revision);
  261. // Invoke the field presave and update hooks.
  262. field_attach_presave($entity_type, $entity);
  263. field_attach_update($entity_type, $entity);
  264. // Clear the cache for this entity now.
  265. entity_get_controller($entity_type)->resetCache(array($id));
  266. }
  267. /**
  268. * Implements hook_file_metadata_info().
  269. */
  270. function file_entity_file_metadata_info() {
  271. $info['width'] = array('label' => t('Width'), 'type' => 'integer');
  272. $info['height'] = array('label' => t('Height'), 'type' => 'integer');
  273. return $info;
  274. }