filefield_paths.inc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * @file
  4. * File (Field) Paths module integration.
  5. */
  6. /**
  7. * Implements hook_filefield_paths_field_settings().
  8. *
  9. * @param $field
  10. * @param $instance
  11. *
  12. * @return array
  13. */
  14. function filefield_paths_filefield_paths_field_settings($field, $instance) {
  15. return array(
  16. 'file_path' => array(
  17. 'title' => 'File path',
  18. 'form' => array(
  19. 'value' => array(
  20. '#type' => 'textfield',
  21. '#title' => t('File path'),
  22. '#maxlength' => 512,
  23. '#size' => 128,
  24. '#element_validate' => array('_file_generic_settings_file_directory_validate'),
  25. '#default_value' => $instance['settings']['file_directory'],
  26. ),
  27. ),
  28. ),
  29. 'file_name' => array(
  30. 'title' => 'File name',
  31. 'form' => array(
  32. 'value' => array(
  33. '#type' => 'textfield',
  34. '#title' => t('File name'),
  35. '#maxlength' => 512,
  36. '#size' => 128,
  37. '#default_value' => '[file:ffp-name-only-original].[file:ffp-extension-original]',
  38. ),
  39. ),
  40. )
  41. );
  42. }
  43. /**
  44. * Implements hook_filefield_paths_process_file().
  45. *
  46. * @param $type
  47. * @param $entity
  48. * @param $field
  49. * @param $instance
  50. * @param $langcode
  51. * @param $items
  52. */
  53. function filefield_paths_filefield_paths_process_file($type, $entity, $field, $instance, $langcode, &$items) {
  54. if (isset($instance['settings']['filefield_paths'])) {
  55. $settings = $instance['settings']['filefield_paths'];
  56. // Check that the destination is writeable.
  57. $wrappers = file_get_stream_wrappers(STREAM_WRAPPERS_WRITE);
  58. foreach ($items as &$file) {
  59. $source_scheme = file_uri_scheme($file['uri']);
  60. $temporary_scheme = file_uri_scheme(variable_get('filefield_paths_temp_location', 'public://filefield_paths'));
  61. $destination_scheme = $field['settings']['uri_scheme'];
  62. if (in_array($source_scheme, array($temporary_scheme, $destination_scheme)) && !empty($wrappers[$destination_scheme])) {
  63. // Process file if this is a new entity, 'Active updating' is set or
  64. // file wasn't previously attached to the entity.
  65. if (isset($entity->original) && empty($settings['active_updating']) && !empty($entity->original->{$field['field_name']}[$langcode])) {
  66. foreach ($entity->original->{$field['field_name']}[$langcode] as $original_file) {
  67. if ($original_file['fid'] == $file['fid']) {
  68. continue(2);
  69. }
  70. }
  71. }
  72. $token_data = array(
  73. 'file' => (object) $file,
  74. $type => $entity
  75. );
  76. // Copy the original file for comparison purposes.
  77. $old_file = $file;
  78. // Process filename.
  79. $settings['file_name']['options']['context'] = 'file_name';
  80. $file['filename'] = !empty($settings['file_name']['value']) ? filefield_paths_process_string($settings['file_name']['value'], $token_data, $settings['file_name']['options']) : $file['filename'];
  81. // Process filepath.
  82. $settings['file_path']['options']['context'] = 'file_path';
  83. $path = filefield_paths_process_string($settings['file_path']['value'], $token_data, $settings['file_path']['options']);
  84. $file['uri'] = file_stream_wrapper_uri_normalize("{$destination_scheme}://{$path}/{$file['filename']}");
  85. // Ensure file uri is no more than 255 characters.
  86. if (drupal_strlen($file['uri']) > 255) {
  87. watchdog('filefield_paths', 'File path was truncated.', array(), WATCHDOG_INFO);
  88. $pathinfo = pathinfo($file['uri']);
  89. $file['uri'] = drupal_substr($file['uri'], 0, 254 - drupal_strlen($pathinfo['extension'])) . ".{$pathinfo['extension']}";
  90. }
  91. // Finalize file if necessary.
  92. if ($file !== $old_file) {
  93. $dirname = drupal_dirname($file['uri']);
  94. if (file_prepare_directory($dirname, FILE_CREATE_DIRECTORY) && $new_file = file_move((object) $old_file, $file['uri'])) {
  95. // Process regular expression.
  96. _filefield_paths_replace_path($old_file['uri'], $file['uri'], $entity);
  97. // Create redirect from old location.
  98. if (module_exists('redirect') && !empty($settings['redirect']) && $settings['active_updating']) {
  99. _filefield_paths_create_redirect($old_file['uri'], $new_file->uri);
  100. }
  101. // Remove any old empty directories.
  102. $paths = explode('/', str_replace("{$source_scheme}://", '', drupal_dirname($old_file['uri'])));
  103. while ($paths) {
  104. if (@drupal_rmdir("{$source_scheme}://" . implode('/', $paths)) == TRUE) {
  105. array_pop($paths);
  106. continue;
  107. }
  108. break;
  109. }
  110. }
  111. else {
  112. watchdog('filefield_paths', 'The file %old could not be moved to the destination of %new. Ensure your permissions are set correctly.', array(
  113. '%old' => $old_file['uri'],
  114. '%new' => $file['uri'],
  115. ));
  116. }
  117. }
  118. }
  119. }
  120. }
  121. }