diff --git a/js/media.format_form.js b/js/media.format_form.js index 446cb54..3d47cb7 100644 --- a/js/media.format_form.js +++ b/js/media.format_form.js @@ -23,7 +23,7 @@ Drupal.behaviors.mediaFormatForm = { $('' + Drupal.t('Submit') + '').appendTo($('#media-format-form')).bind('click', Drupal.media.formatForm.submit); $('' + Drupal.t('Cancel') + '').appendTo($('#media-format-form')).bind('click', Drupal.media.formatForm.submit); - if (Drupal.settings.media_format_form.autosubmit) { + if (Drupal.settings.media_format_form != undefined && Drupal.settings.media_format_form.autosubmit) { $('.button.fake-ok').click(); } } diff --git a/js/media.popups.js b/js/media.popups.js index a7bf374..b5b9867 100644 --- a/js/media.popups.js +++ b/js/media.popups.js @@ -100,6 +100,8 @@ Drupal.media.popups.mediaBrowser = function (onSelect, globalOptions, pluginOpti Drupal.media.popups.mediaBrowser.mediaBrowserOnLoad = function (e) { var options = e.data; + if (this.contentWindow.Drupal.media == undefined) return; + if (this.contentWindow.Drupal.media.browser.selectedMedia.length > 0) { var ok = (Drupal && Drupal.t) ? Drupal.t('OK') : 'OK'; var ok_func = $(this).dialog('option', 'buttons')[ok]; diff --git a/js/plugins/media.views.js b/js/plugins/media.views.js index aec2323..95b673b 100644 --- a/js/plugins/media.views.js +++ b/js/plugins/media.views.js @@ -32,6 +32,12 @@ Drupal.behaviors.mediaViews = { for (index in Drupal.settings.media.files) { if (Drupal.settings.media.files[index].fid == fid) { selectedFiles.push(Drupal.settings.media.files[index]); + + // If multiple tabs contains the same file, it will be present in the + // files-array multiple times, so we break out early so we don't have + // it in the selectedFiles array multiple times. + // This would interfer with multi-selection, so... + break; } } Drupal.media.browser.selectMedia(selectedFiles); diff --git a/media.module b/media.module index caa05fa..ea7ec8f 100644 --- a/media.module +++ b/media.module @@ -440,6 +440,26 @@ function media_file_operations_info() { } /** + * Return a URL for editing an files. + * + * Works with an array of fids or a single fid. + * + * @param mixed $fids + * An array of file IDs or a single file ID. + */ +function media_file_edit_url($fids) { + if (!is_array($fids)) { + $fids = array($fids); + } + + if (count($fids) > 1) { + return 'admin/content/file/edit-multiple/' . implode(' ', $fids); + } else { + return 'file/' . reset($fids) . '/edit'; + } +} + +/** * Callback for the edit operation. * * Redirects the user to the edit multiple files page. @@ -452,14 +472,7 @@ function media_file_operations_info() { function media_file_operation_edit_multiple($fids) { // The thumbnail browser returns TRUE/FALSE for each item, so use array keys. $fids = array_keys(array_filter($fids)); - // Note that we add a destination here to prevent normal form redirects. - if (count($fids) > 1) { - drupal_goto('admin/content/file/edit-multiple/' . implode(' ', $fids), array('query' => drupal_get_destination())); - } - else { - $fid = current($fids); - drupal_goto('file/' . current($fids) . '/edit', array('query' => drupal_get_destination())); - } + drupal_goto(media_file_edit_url($fids), array('query' => drupal_get_destination())); } /** @@ -524,7 +537,7 @@ function media_form_field_ui_field_edit_form_alter(&$form, &$form_state) { * Implements hook_form_FORM_ID_alter(). */ function media_form_file_entity_edit_alter(&$form, &$form_state) { - // Make adjustments to the media edit form when used in a modal. + // Make adjustments to the file edit form when used in a CTools modal. if (!empty($form_state['ajax'])) { // Remove the preview and the delete button. $form['preview']['#access'] = FALSE; @@ -535,6 +548,14 @@ function media_form_file_entity_edit_alter(&$form, &$form_state) { $form['actions']['cancel']['#attributes']['class'][] = 'button-no'; $form['actions']['cancel']['#attributes']['class'][] = 'ctools-close-modal'; } + + // Make adjustments to the file edit form when used in the media modal. + $params = drupal_get_query_parameters(); + if (!empty($params['render']) && $params['render'] == 'media-popup') { + $form['#attached']['js'][] = drupal_get_path('module', 'media') . '/js/media.browser.edit.js'; + $form['actions']['cancel']['#access'] = FALSE; + $form['actions']['delete']['#access'] = FALSE; + } } /** @@ -556,14 +577,39 @@ function media_form_alter(&$form, &$form_state, $form_id) { } } +/** + * Submit handler; direction form submissions in the media browser. + */ function media_browser_form_submit($form, &$form_state) { + $url = NULL; + $parameters = array(); + + // Single upload if (!empty($form_state['file'])) { $file = $form_state['file']; - $form_state['redirect'] = array('media/browser', array('query' => array('render' => 'media-popup', 'fid' => $file->fid))); + $url = 'media/browser'; + $parameters = array('query' => array('render' => 'media-popup', 'fid' => $file->fid)); } + + // Multi upload if (!empty($form_state['files'])) { $files = $form_state['files']; - $form_state['redirect'] = array('media/browser', array('query' => array('render' => 'media-popup', 'fid' => array_keys($files)))); + $url = 'media/browser'; + $parameters = array('query' => array('render' => 'media-popup', 'fid' => array_keys($files))); + } + + // If $url is set, we had some sort of upload, so redirect + if (!empty($url)) { + // We the user has access to edit media, we change the redirect to the file-edit url + // first, then secondly to the destionation decided above. + if (media_access('edit')) { + $destination = $url . '?' . drupal_http_build_query($parameters['query']); + $url = media_file_edit_url($parameters['query']['fid']); + $parameters = array('query' => array('render' => 'media-popup', 'destination' => $destination)); + } + + // Redirect the form. + $form_state['redirect'] = array($url, $parameters); } }