filefield_paths.inc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * @file
  4. * File (Field) Paths module integration.
  5. */
  6. /**
  7. * Implements hook_filefield_paths_field_settings().
  8. */
  9. function filefield_paths_filefield_paths_field_settings() {
  10. return array(
  11. 'file_path' => array(
  12. 'title' => 'File path',
  13. 'sql' => 'filepath',
  14. 'form' => array(
  15. 'value' => array(
  16. '#type' => 'textfield',
  17. '#title' => t('File path'),
  18. '#maxlength' => 512,
  19. '#size' => 128,
  20. ),
  21. ),
  22. ),
  23. 'file_name' => array(
  24. 'title' => 'File name',
  25. 'sql' => 'filename',
  26. 'form' => array(
  27. 'value' => array(
  28. '#type' => 'textfield',
  29. '#title' => t('File name'),
  30. '#default_value' => '[file:ffp-name-only-original].[file:ffp-extension-original]',
  31. ),
  32. ),
  33. )
  34. );
  35. }
  36. /**
  37. * Implements hook_filefield_paths_process_file().
  38. */
  39. function filefield_paths_filefield_paths_process_file($type, $entity, $field, $instance, $langcode, &$items) {
  40. $settings = $instance['settings']['filefield_paths'];
  41. foreach ($items as &$file) {
  42. if ($file['timestamp'] == REQUEST_TIME || $settings['active_updating']) {
  43. $token_data = array(
  44. 'file' => (object) $file,
  45. $type => $entity
  46. );
  47. // Copy the original file for comparision purposes.
  48. $old_file = $file;
  49. // Process filename.
  50. $file['filename'] = !empty($settings['file_name']['value'])
  51. ? filefield_paths_process_string($settings['file_name']['value'], $token_data, $settings['file_name']['options'])
  52. : $file['filename'];
  53. // Process filepath.
  54. $file['uri'] = "{$field['settings']['uri_scheme']}://" . filefield_paths_process_string($settings['file_path']['value'] . "/{$file['filename']}", $token_data, $settings['file_path']['options']);
  55. // Finalize file if necessary.
  56. if ($file !== $old_file) {
  57. if (file_prepare_directory(drupal_dirname($file['uri']), FILE_CREATE_DIRECTORY) && file_move((object) $old_file, $file['uri'])) {
  58. // Process regular expression.
  59. _filefield_paths_replace_path($old_file['uri'], $file['uri'], $entity);
  60. // Remove any old empty directories.
  61. $scheme = file_uri_scheme($old_file['uri']);
  62. $paths = explode('/', str_replace("{$scheme}://", '', drupal_dirname($old_file['uri'])));
  63. while ($paths) {
  64. if (@drupal_rmdir("{$scheme}://" . implode('/', $paths)) == TRUE) {
  65. array_pop($paths);
  66. continue;
  67. }
  68. break;
  69. }
  70. }
  71. }
  72. }
  73. }
  74. }