diff --git a/includes/media.fields.inc b/includes/media.fields.inc index 4ec3628..9b44567 100644 --- a/includes/media.fields.inc +++ b/includes/media.fields.inc @@ -119,6 +119,7 @@ function media_field_widget_form(&$form, &$form_state, $field, $instance, $langc 'file_extensions' => isset($field_settings['file_extensions']) ? $field_settings['file_extensions'] : variable_get('file_entity_default_allowed_extensions', 'jpg jpeg gif png txt doc docx xls xlsx pdf ppt pptx pps ppsx odt ods odp mp3 mov mp4 m4a m4v mpeg avi ogg oga ogv weba webp webm'), 'max_filesize' => isset($field_settings['max_filesize']) ? $field_settings['max_filesize'] : 0, 'uri_scheme' => !empty($field['settings']['uri_scheme']) ? $field['settings']['uri_scheme'] : file_default_scheme(), + 'multiselect' => $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED, ), ), // Allows this field to return an array instead of a single value. @@ -283,6 +284,24 @@ function media_field_widget_process($element, &$form_state, $form) { * wrapper around the entire group so it can be replaced all at once. */ function media_field_widget_process_multiple($element, &$form_state, $form) { + // In order to support multiple selection, we need to reconstruct the _POST + // data that is checked in media_attach_file(). We need to reconstruct the + // field's _POST key name, for example: field_mediafield_und_0. + $upload_name_prefix = implode('_', $element['#parents']) . '_'; + $upload_name = $upload_name_prefix . $element['#file_upload_delta']; + + if (!empty($_POST['media'][$upload_name])) { + $files = explode(',', $_POST['media'][$upload_name]); + $count = count($files); + // Supposing #file_upload_delta is always the last delta this will work + for ($i = 0; $i < $count; $i++) { + // For each file selected, increment the field key to be processed. + // field_mediafield_und_0 becomes field_mediafield_und_1, etc. + $_POST['media'][$upload_name_prefix . ($element['#file_upload_delta'] + $i)] = $files[$i]; + $element[] = $element[$element['#file_upload_delta']]; + } + } + $element_children = element_children($element, TRUE); $count = count($element_children); diff --git a/js/media.js b/js/media.js index 4e7fdd9..b550ee4 100644 --- a/js/media.js +++ b/js/media.js @@ -71,19 +71,31 @@ Drupal.media.openBrowser = function (event) { return; } - // Grab the first of the selected media files. - var mediaFile = mediaFiles[0]; + var mediaFileValue; + // Process the value based on multiselect. + if (mediaFiles.length > 1) { + // Reverse array to have files in correct order + mediaFiles.reverse(); + // Concatenate the array into a comma separated string. + mediaFileValue = mediaFiles.map(function(file) { + return file.fid; + }).join(','); + } + else { + // Grab the first of the selected media files. + mediaFileValue = mediaFiles[0].fid; + + // Display a preview of the file using the selected media file's display. + previewField.html(mediaFileValue.preview); + } // Set the value of the hidden file ID field and trigger a change. - uploadField.val(mediaFile.fid); + uploadField.val(mediaFileValue); uploadField.trigger('change'); // Find the attach button and automatically trigger it. var attachButton = uploadField.siblings('.attach'); attachButton.trigger('mousedown'); - - // Display a preview of the file using the selected media file's display. - previewField.html(mediaFile.preview); }, configuration); return false; diff --git a/tests/media.test b/tests/media.test index fe4470c..b292989 100644 --- a/tests/media.test +++ b/tests/media.test @@ -883,6 +883,7 @@ class MediaElementSettingsTestCase extends MediaFileFieldTestCase { 'file_extensions' => 'txt', 'max_filesize' => '', 'uri_scheme' => 'public', + 'multiselect' => $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED, ), ), ),