filefield_sources_plupload.module 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * @file
  4. * A File field extension to allow multi uploads using Plupload.
  5. *
  6. */
  7. /**
  8. * Implements hook_filefield_sources_info().
  9. */
  10. function filefield_sources_plupload_filefield_sources_info() {
  11. $source = array();
  12. $source['plupload'] = array(
  13. 'name' => t('Advanced upload widget (Plupload)'),
  14. 'label' => t('Advanced upload'),
  15. 'description' => t('Upload using enhanced file selection.'),
  16. 'process' => 'filefield_sources_plupload_source_process',
  17. );
  18. return $source;
  19. }
  20. /**
  21. * A #process callback to extend the filefield_widget element type.
  22. *
  23. */
  24. function filefield_sources_plupload_source_process($element, $form_state, $form) {
  25. $field = field_widget_field($element, $form_state);
  26. $instance = field_widget_instance($element, $form_state);
  27. // Get the upload size. Use PHP limit if not defined. This can be specified
  28. // larger than PHP limit, since Plupload transfers using 1mb chunks.
  29. $max_filesize = !empty($instance['settings']['max_filesize']) ? parse_size($instance['settings']['max_filesize']) : parse_size(file_upload_max_size());
  30. if ($field['cardinality'] == 1) {
  31. $element['filefield_plupload'] = array(
  32. '#weight' => 100.5,
  33. '#markup' => '<p>' . t('This upload type can not be used on single value fields.') . '</p>',
  34. // Required for proper theming.
  35. '#filefield_source' => TRUE,
  36. '#prefix' => '<div class="filefield-source filefield-source-plupload clear-block">',
  37. '#suffix' => '</div>',
  38. );
  39. }
  40. else {
  41. $element['filefield_plupload'] = array(
  42. '#weight' => 100.5,
  43. // Required for proper theming.
  44. '#filefield_source' => TRUE,
  45. '#prefix' => '<div class="filefield-source filefield-source-plupload clear-block">',
  46. '#suffix' => '</div>',
  47. );
  48. $element['filefield_plupload']['pud'] = array(
  49. '#type' => 'plupload',
  50. '#title' => t('Select one or more files to upload'),
  51. // Even though filefield does validation on submit, this is required for
  52. // client side validation as well as proper file munging during upload.
  53. '#upload_validators' => $element['#upload_validators'],
  54. '#plupload_settings' => array(
  55. 'max_file_size' => $max_filesize,
  56. 'chunk_size' => '1mb',
  57. 'runtimes' => 'html5,flash,silverlight,html4',
  58. ),
  59. // We need our own value callback as we need access to $form_state.
  60. '#value_callback' => 'filefield_sources_plupload_element_value',
  61. '#process' => array('filefield_sources_plupload_element_process'),
  62. );
  63. $element['filefield_plupload']['upload_button'] = array(
  64. '#name' => implode('_', $element['#array_parents']) . '_transfer',
  65. '#type' => 'submit',
  66. '#value' => t('Start upload'),
  67. '#validate' => array(),
  68. '#submit' => array('filefield_sources_plupload_submit'),
  69. '#limit_validation_errors' => array($element['#parents']),
  70. '#ajax' => array(
  71. 'path' => 'file/ajax/' . implode('/', $element['#array_parents']) . '/' . $form['form_build_id']['#value'],
  72. 'wrapper' => $element['#id'] . '-ajax-wrapper',
  73. 'method' => 'replace',
  74. 'effect' => 'fade',
  75. 'event' => 'pud_update',
  76. ),
  77. );
  78. }
  79. return $element;
  80. }
  81. /**
  82. * Replace Plupload Integration's javascript with our own.
  83. *
  84. */
  85. function filefield_sources_plupload_element_process($element) {
  86. $module_path = drupal_get_path('module', 'filefield_sources_plupload');
  87. $element['#attached']['js'] = array($module_path . '/js/plupload.js');
  88. return $element;
  89. }
  90. /**
  91. * Our value handler not only sets the Plupload fields values, but also adds the
  92. * files to the filefiels values.
  93. *
  94. */
  95. function filefield_sources_plupload_element_value($element, $input = FALSE, &$form_state = NULL) {
  96. if (isset($input) && $input === FALSE) {
  97. return array();
  98. }
  99. // We rely on Plupload Integration module to handle the actual field values.
  100. $pud_value = plupload_element_value($element, $input, $form_state);
  101. if (empty($pud_value)) {
  102. return array();
  103. }
  104. $field_parents = array_slice($element['#array_parents'], 0, -3);
  105. $field_element = drupal_array_get_nested_value($form_state['complete form'], $field_parents);
  106. $field_name = $field_element['#field_name'];
  107. $langcode = $field_element['#language'];
  108. $upload_delta = isset($field_element['#file_upload_delta']) ? $field_element['#file_upload_delta'] : 0;
  109. $upload_location = isset($field_element[$upload_delta]['#upload_location']) ? $field_element[$upload_delta]['#upload_location'] : file_default_scheme() . '://';
  110. $upload_validators = isset($field_element[$upload_delta]['#upload_validators']) ? $field_element[$upload_delta]['#upload_validators'] : array();
  111. // A URI may already have a trailing slash or look like "public://".
  112. if (drupal_substr($upload_location, -1) != '/') {
  113. $upload_location .= '/';
  114. }
  115. if (!file_prepare_directory($upload_location, FILE_CREATE_DIRECTORY)) {
  116. watchdog('file', 'The upload directory %directory for the file field !name could not be created or is not accessible. A newly uploaded file could not be saved in this directory as a consequence, and the upload was canceled.', array('%directory' => $upload_location, '!name' => $field_name));
  117. form_set_error($field_name, t('The file could not be uploaded.'));
  118. return FALSE;
  119. }
  120. // Validate, clean up and move the files into it's destination, then register
  121. // as managed files (status = 0 until entity is saved).
  122. $saved_files = array();
  123. foreach ($pud_value as $uploaded_file) {
  124. if ($uploaded_file['status'] == 'done') {
  125. $source = $uploaded_file['tmppath'];
  126. $extensions = $upload_validators['file_validate_extensions'][0];
  127. // Transliterate, munge and validate file name.
  128. $filename = filefield_sources_clean_filename($uploaded_file['name'], $extensions);
  129. // Move the file to a temporary destination using final base file name.
  130. $temp_destination = file_stream_wrapper_uri_normalize('temporary://' . $filename);
  131. $temp_filepath = file_unmanaged_move($source, $temp_destination, FILE_EXISTS_REPLACE);
  132. // Save the files to their final destination.
  133. if ($file = filefield_sources_save_file($temp_filepath, $upload_validators, $upload_location)) {
  134. $saved_files[] = $file;
  135. }
  136. }
  137. else {
  138. form_set_error('pud', t('Upload of %name failed.', array('%name' => $uploaded_file['name'])));
  139. }
  140. }
  141. // Get exisitng file values.
  142. // File Field items are stored in the field state starting from Drupal 7.9.
  143. $field_state = field_form_get_state($field_element['#field_parents'], $field_name, $langcode, $form_state);
  144. if (isset($field_state['items'])) {
  145. $field_values = $field_state['items'];
  146. }
  147. else {
  148. $field_values = drupal_array_get_nested_value($form_state['values'], $field_parents);
  149. }
  150. // Update field values with new files.
  151. foreach ($saved_files as $saved_file) {
  152. $field_values[$upload_delta] = (array) $saved_file;
  153. $field_values[$upload_delta]['_weight'] = $upload_delta;
  154. $upload_delta++;
  155. }
  156. // Update form_state values.
  157. drupal_array_set_nested_value($form_state['values'], $field_parents, $field_values);
  158. // Update items.
  159. $field_state['items'] = $field_values;
  160. field_form_set_state($field_element['#field_parents'], $field_name, $langcode, $form_state, $field_state);
  161. return $pud_value;
  162. }
  163. /**
  164. * We need our own submit handler until the following issue gets resolved:
  165. * http://drupal.org/node/1329056
  166. *
  167. */
  168. function filefield_sources_plupload_submit(&$form, &$form_state) {
  169. $parents = array_slice($form_state['triggering_element']['#array_parents'], 0, -3);
  170. $element = drupal_array_get_nested_value($form, $parents);
  171. $field_name = $element['#field_name'];
  172. $langcode = $element['#language'];
  173. // Get existing file values.
  174. // File Field items are stored in the field state after ajax reloads starting
  175. // from Drupal 7.9. We try to support all releases by merging the items.
  176. $field_state = field_form_get_state($element['#field_parents'], $field_name, $langcode, $form_state);
  177. $field_values = drupal_array_get_nested_value($form_state['values'], $parents);
  178. if (isset($field_values) && isset($field_state['items'])) {
  179. $field_values += $field_state['items'];
  180. }
  181. elseif (isset($field_state['items'])) {
  182. $field_values = $field_state['items'];
  183. }
  184. if (isset($field_values)) {
  185. // Update sort order according to weight. Note that this is always stored in
  186. // form state. Sorts done before ajax reloads does not work using regular
  187. // upload, but that is a core bug.
  188. usort($field_values, '_field_sort_items_helper');
  189. // Update form_state values.
  190. drupal_array_set_nested_value($form_state['values'], $parents, $field_values);
  191. // Update items.
  192. $field_state['items'] = $field_values;
  193. field_form_set_state($element['#field_parents'], $field_name, $langcode, $form_state, $field_state);
  194. }
  195. // Clear out input as it will need to be rebuildt.
  196. drupal_array_set_nested_value($form_state['input'], $element['#parents'], NULL);
  197. $form_state['rebuild'] = TRUE;
  198. }