uuid_file.features.inc 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * @file
  4. * Features hooks for the uuid_file features component.
  5. */
  6. /**
  7. * Implements hook_features_export().
  8. */
  9. function uuid_file_features_export($data, &$export, $module_name = '') {
  10. $pipe = array();
  11. $export['dependencies']['uuid_features'] = 'uuid_features';
  12. foreach ($data as $uuid) {
  13. $export['features']['uuid_file'][$uuid] = $uuid;
  14. }
  15. return $pipe;
  16. }
  17. /**
  18. * Implements hook_features_export_render().
  19. */
  20. function uuid_file_features_export_render($module, $data) {
  21. $translatables = $code = array();
  22. $code[] = ' $files = array();';
  23. $code[] = '';
  24. foreach ($data as $uuid) {
  25. $fid = uuid_get_serial_id('files', 'fid', $uuid);
  26. if (!$fid) {
  27. continue;
  28. }
  29. $file = field_file_load($fid);
  30. $file_parts = explode('.', $file['filepath']);
  31. $extension = array_pop($file_parts);
  32. $export = array(
  33. 'uuid' => $uuid,
  34. 'hash' => md5_file($file['filepath']),
  35. 'extension' => $extension,
  36. );
  37. $code[] = ' $files[] = ' . features_var_export($export, ' ') . ';';
  38. }
  39. if (!empty($translatables)) {
  40. $code[] = features_translatables_export($translatables, ' ');
  41. }
  42. $code[] = ' return $files;';
  43. $code = implode("\n", $code);
  44. return array('uuid_features_default_files' => $code);
  45. }
  46. /**
  47. * Implements hook_features_revert().
  48. */
  49. function uuid_file_features_revert($module) {
  50. uuid_file_features_rebuild($module);
  51. }
  52. /**
  53. * Implements hook_features_rebuild().
  54. * Rebuilds files based on UUID from code defaults.
  55. */
  56. function uuid_file_features_rebuild($module) {
  57. $files = module_invoke($module, 'uuid_features_default_files');
  58. if (!empty($files)) {
  59. $source_dir = drupal_get_path('module', $module) . '/uuid_file';
  60. foreach ($files as $data) {
  61. $source = $source_dir . '/' . $data['uuid'] . '.' . $data['extension'];
  62. $data = file_get_contents($source);
  63. // Copy the file and save to db.
  64. $file = file_save_data($data, NULL, FILE_EXISTS_REPLACE);
  65. }
  66. }
  67. }