diff --git a/media_multiselect.js b/media_multiselect.js index 0523763..fc48a5d 100644 --- a/media_multiselect.js +++ b/media_multiselect.js @@ -4,35 +4,48 @@ for (var base in settings.ajax) { var element_settings = settings.ajax[base]; if (element_settings.event == 'media_select' && !$('#' + base + '.media-multiselect-processed').length) { + $('#' + base).show(); // Bind a click-event to the 'add more' button. $('#' + base).click((function(element) { return function(event) { var media_settings = settings.media.multi_select.elements[element]; - // Add a new beforeSerialize that adds in our fids - Drupal.ajax[element].beforeSerialize = function (element, options) { + // Add a new beforeSerialize that adds in our fids. + Drupal.ajax[element].beforeSerialize = function(element, options) { // Add the fids to the form_values. $(this.media_multiselect_files).each(function() { options.data['media_multiselect_fids[' + this.fid + ']'] = this.fid; - }) + }); // Call the prototype, so we preseve any existing functionality in there. - Drupal.ajax.prototype.beforeSerialize.call(this, element, options) - } - - var button = this; + Drupal.ajax.prototype.beforeSerialize.call(this, element, options); + }; + + var wrapper = $(this).closest('div.form-wrapper'); // Launch the Media Browser. Drupal.media.popups.mediaBrowser(function(files) { - Drupal.ajax[element].media_multiselect_files = files - $(button).trigger('media_select'); + Drupal.ajax[element].media_multiselect_files = files; + for (var i = 0; i < files.length; i++) { + var button = wrapper.find('.multi-browse').first(); + var uploadField = button.siblings('.upload'); + + // Set the value of the hidden file ID field and trigger a change. + uploadField.val(files[i].fid); + uploadField.trigger('change'); + + // Find the attach button and automatically trigger it. + var attachButton = uploadField.siblings('.attach'); + Drupal.ajax[attachButton.attr('id')].options.async = false; + attachButton.trigger('mousedown'); + } }, media_settings.global); // Aaaand prevent default. event.preventDefault(); - }})(base)) + }})(base)); $('#' + base).addClass('media-multiselect-processed'); } } } - } -})(jQuery); \ No newline at end of file + }; +})(jQuery); diff --git a/media_multiselect.module b/media_multiselect.module index 6caa609..a1dd383 100644 --- a/media_multiselect.module +++ b/media_multiselect.module @@ -84,7 +84,7 @@ function media_multiselect_field_widget_form(&$form, &$form_state, $field, $inst } // Then ask the Field API to generate that widget - $element += field_multiple_value_form($field, $instance, $langcode, $items, $form, $form_state); + $element += media_field_widget_form($form, $form_state, $field, $instance, $langcode, $items, $delta, $element); // If we have a != 1, != unlimited field, we have a fixed amount of values // which we can't ajaxify the 'add-more' button of, as it does not have a @@ -93,55 +93,59 @@ function media_multiselect_field_widget_form(&$form, &$form_state, $field, $inst return $element; } - // Copy over the media-options - $element['#media_options'] = $element[0]['#media_options']; - $element['#media_options']['global']['multiselect'] = TRUE; + $element[$element['#file_upload_delta']]['#media_options']['global']['multiselect'] = TRUE; + $element[$element['#file_upload_delta']]['#process'][] = 'media_multiselect_change_add_button'; - do { - // Remove the empty placeholder-item at the bottom, and any access items in case - // we have a limited cardinality field that has too many items chosen! - unset($element[$element['#max_delta']]); - $element['#max_delta'] -= 1; - } while ($real_cardinality != -1 && $element['#max_delta'] >= $real_cardinality); - - // Add our JS + // If we no longer have any items, change the theme to avoid an empty table. + if ($element['#file_upload_delta'] == 0) { + $element[$element['#file_upload_delta']]['#add_button_title'] = t('Select media'); + } + elseif ($element['#file_upload_delta'] == $real_cardinality - 1) { + $element[$element['#file_upload_delta']]['#add_button_access'] = FALSE; + } + else { + $element[$element['#file_upload_delta']]['#add_button_title'] = t('Add media'); + } + + // Add our JS. $element['#attached'] = array( 'js' => array( drupal_get_path('module', 'media_multiselect') . '/media_multiselect.js', ), ); - // Attach the browser JS (this is normally done by the media-elements, - // but on empty nodes, there arent any media-elements, as we unset the - // empty placeholder) - module_load_include('inc', 'media', 'includes/media.browser'); - media_attach_browser_js($element); - $element['#after_build'][] = 'media_multiselect_add_js_settings'; - // Change the AJAX info for the add-more button. - $element['add_more']['#ajax']['event'] = 'media_select'; - $element['add_more']['#ajax']['callback'] = 'media_multiselect_ajax_callback'; - $element['add_more']['#submit'][0] = 'media_multiselect_ajax_submit'; - $element['add_more']['#media_multiselect_value_handler'] = 'media_multiselect_add_more_value_handler'; - - // If we now no longer has any items, change the theme to not get the - // empty table - if ($element['#max_delta'] < 0) { - $element['add_more']['#value'] = t('Select'); - $element['#theme'] = 'media_multiselect_empty_field'; - } - elseif ($element['#max_delta'] == $real_cardinality -1 ) { - $element['add_more']['#access'] = FALSE; + $element['#process'][] = 'media_multiselect_process_widget_form'; + + return $element; +} + +/** + * Change the AJAX info for the add-more button. + */ +function media_multiselect_change_add_button($element) { + $button = $element['browse_button']; + + $button['#ajax']['event'] = 'media_select'; + $button['#ajax']['callback'] = 'media_multiselect_ajax_callback'; + $button['#submit'][0] = 'media_multiselect_ajax_submit'; + $button['#media_multiselect_value_handler'] = 'media_multiselect_add_more_value_handler'; + + if (isset($element['#add_button_access']) && $element['#add_button_access'] === FALSE) { + $button['#access'] = FALSE; } else { - $element['add_more']['#value'] = t('Add'); + $button['#title'] = $element['#add_button_title']; } - $element['#process'] = array( - 'media_multiselect_process_widget_form', - ); - + // Remove 'browse' class because media module will overwrite click events for + // the buttons with this class. + $button['#attributes']['class'] = array_values(array_diff($button['#attributes']['class'], array('browse'))); + $button['#attributes']['class'][] = 'multi-browse'; + + $element['browse_button'] = $button; + return $element; } @@ -151,45 +155,22 @@ function media_multiselect_field_widget_form(&$form, &$form_state, $field, $inst * of the value-elements with their own #process-instruction. */ function media_multiselect_process_widget_form($element) { - $element['add_more']['#media_multiselect_field_wrapper'] = $element['#array_parents']; + $element[$element['#file_upload_delta']]['browse_button']['#media_multiselect_field_wrapper'] = $element['#array_parents']; - preg_match('/id="(.*?)"/', $element['#prefix'], $matches); - list($_, $wrapper_id) = $matches; - for ($i = 0; $i <= $element['#max_delta']; $i++) { + for ($i = 0; $i <= $element['#file_upload_delta']; $i++) { $element[$i] += element_info('media'); - $element[$i]['#wrapper_id'] = $wrapper_id; - $element[$i]['#process'][] = 'media_multiselect_improve_remove_button'; $element[$i]['#media_multiselect_field_wrapper'] = $element['#array_parents']; } - return $element; -} - -/** - * #process-callback for each value-element - * Alters the remove-button to be a ajax-submit instead, so we can remove the - * element from the form, instead of just blanking it. - */ -function media_multiselect_improve_remove_button($element) { - $element['remove'] = array( - '#type' => 'submit', - '#value' => t('Remove'), - '#weight' => $element['remove']['#weight'], - '#submit' => array('media_multiselect_ajax_submit'), - '#media_multiselect_value_handler' => 'media_multiselect_remove_button_value_handler', - '#name' => $element['#field_name'] . '_remove_' . $element['#delta'], - '#ajax' => array( - 'callback' => 'media_multiselect_ajax_callback', - 'wrapper' => $element['#wrapper_id'], - ), - '#media_multiselect_field_wrapper' => array_slice($element['#array_parents'], 0, -1), - ); + // Prefix and suffix used for Ajax replacement. + $element['#prefix'] = '