diff --git a/css/media.css b/css/media.css index 4686e7c..acd1ebf 100644 --- a/css/media.css +++ b/css/media.css @@ -71,17 +71,6 @@ list-style: none; } -/** This is a massive hack. There must be a better way. See media.fields.inc **/ - -.media-widget .fid { - display:none; -} - -.media-widget a.button + a.button { - margin-left: 15px; - display: none; -} - /* @end */ /* @group media item */ diff --git a/includes/media.pages.inc b/includes/media.pages.inc index 016696f..4737a9a 100644 --- a/includes/media.pages.inc +++ b/includes/media.pages.inc @@ -278,3 +278,31 @@ function media_add_remote($form, &$form_state) { return $form; } + +/** + * CTools modal callback for editing a file. + */ +function media_file_edit_modal($form, &$form_state, $file, $js) { + ctools_include('modal'); + ctools_include('ajax'); + + $form_state['ajax'] = $js; + $form_state['build_info']['files'][] = drupal_get_path('module', 'file_entity') . '/includes/file_entity.pages.inc'; + module_load_include('inc', 'file_entity', 'file_entity.pages'); + + $output = ctools_modal_form_wrapper('file_entity_edit', $form_state); + + if ($js) { + $commands = $output; + + if ($form_state['executed']) { + $commands[] = ctools_modal_command_dismiss(t('File saved')); + } + + print ajax_render($commands); + exit(); + } + + // Otherwise, just return the output. + return $output; +} diff --git a/includes/media.variables.inc b/includes/media.variables.inc index 9978b33..64998e6 100644 --- a/includes/media.variables.inc +++ b/includes/media.variables.inc @@ -123,9 +123,6 @@ function media_variable_default($name = NULL) { // Attributes which can be modified via the wysiwyg and persist. 'wysiwyg_allowed_attributes' => array('height', 'width', 'hspace', 'vspace', 'border', 'align', 'style', 'alt', 'title', 'class', 'id'), - 'field_select_media_text' => 'Select media', - 'field_remove_media_text' => 'Remove media', - // Name of the theme to use in media popup dialogs, defaults to admin_theme 'dialog_theme' => '', // @TODO: Make a configuration form with this. @@ -149,8 +146,10 @@ function media_variable_default($name = NULL) { 'icon_base_directory' => drupal_get_path('module', 'media') . '/images/icons', 'icon_set' => 'default', - // Deprecated variables. + // Deprecated variables no longer in use, but should still be uninstalled. 'show_file_type_rebuild_nag' => NULL, + 'field_select_media_text' => NULL, + 'field_remove_media_text' => NULL, ); } diff --git a/js/media.js b/js/media.js index aa445b7..2d35a3e 100644 --- a/js/media.js +++ b/js/media.js @@ -23,11 +23,13 @@ Drupal.behaviors.mediaElement = { //options = Drupal.settings.media.fields[this.id]; var fidField = $('.fid', this); var previewField = $('.preview', this); - var removeButton = $('.remove', this); // Actually a link, but looks like a button. + var editButton = $('.edit', this); + var removeButton = $('.remove', this); - // Show the Remove button if there's an already selected media. - if (fidField.val() != 0) { - removeButton.css('display', 'inline-block'); + // Hide the edit and remove buttons if there is no file data yet. + if (fidField.val() == 0) { + editButton.hide(); + removeButton.hide(); } // When someone clicks the link to pick media (or clicks on an existing thumbnail) @@ -39,33 +41,35 @@ Drupal.behaviors.mediaElement = { return; } var mediaFile = mediaFiles[0]; - // Set the value of the filefield fid (hidden). + // Set the value of the filefield fid (hidden) and trigger a change. fidField.val(mediaFile.fid); + fidField.trigger('change'); // Set the preview field HTML. previewField.html(mediaFile.preview); - // Show the Remove button. - removeButton.show(); }, globalOptions); return false; }); // When someone clicks the Remove button. $('.remove', this).bind('click', function () { - // Set the value of the filefield fid (hidden). + // Set the value of the filefield fid (hidden) and trigger change. fidField.val(0); + fidField.trigger('change'); // Set the preview field HTML. previewField.html(''); - // Hide the Remove button. - removeButton.hide(); return false; }); - $('.media-edit-link', this).bind('click', function () { - var fid = fidField.val(); - if (fid) { - Drupal.media.popups.mediaFieldEditor(fid, function (r) { alert(r); }); + // Show or hide the edit/remove buttons if the field has a file or not. + $('.fid', this).bind('change', function() { + if (fidField.val() == 0) { + editButton.hide(); + removeButton.hide(); + } + else { + editButton.show(); + removeButton.show(); } - return false; }); }); diff --git a/media.module b/media.module index 79e960a..da6e9ee 100644 --- a/media.module +++ b/media.module @@ -185,9 +185,9 @@ function media_menu() { 'page arguments' => array('media_format_form', 1), 'access callback' => 'media_access', 'access arguments' => array('view'), - 'weight' => 0, 'file' => 'includes/media.filter.inc', 'theme callback' => 'media_dialog_get_theme_name', + 'type' => MENU_CALLBACK, ); if (module_exists('multiform')) { @@ -203,10 +203,30 @@ function media_menu() { ); } + // We could re-use the file/%file/edit path for the modal callback, but + // it is just easier to use our own namespace here. + $items['media/%file/edit/%ctools_js'] = array( + 'title' => 'Edit', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('media_file_edit_modal', 1, 3), + 'access callback' => 'media_access', + 'access arguments' => array('edit'), + 'file' => 'includes/media.pages.inc', + 'type' => MENU_CALLBACK, + ); + return $items; } /** + * Implement hook_admin_path(). + */ +function media_admin_paths() { + $paths['media/*/edit/*'] = TRUE; + return $paths; +} + +/** * Implement hook_permission(). */ function media_permission() { @@ -469,6 +489,22 @@ 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. + if (!empty($form_state['ajax'])) { + // Remove the preview and the delete button. + $form['preview']['#access'] = FALSE; + $form['actions']['delete']['#access'] = FALSE; + + // Convert the cancel link to a button which triggers a modal close. + $form['actions']['cancel']['#attributes']['class'][] = 'button'; + $form['actions']['cancel']['#attributes']['class'][] = 'ctools-close-modal'; + } +} + /* ***************************************** */ /* API FUNCTIONS */ /* ***************************************** */ @@ -776,8 +812,15 @@ function media_element_info() { */ function media_element_process(&$element, &$form_state, $form) { $fid = isset($element['#value']['fid']) ? $element['#value']['fid'] : 0; - $file = file_load($fid); - $path = drupal_get_path('module', 'media'); + $file = $fid ? file_load($fid) : FALSE; + + // Add the CTools modal JavaScript for the edit button if necessary. + ctools_include('modal'); + ctools_include('ajax'); + ctools_modal_add_js(); + + // Set some default element properties. + $element['#file'] = $file; $element['title'] = array( '#type' => 'item', @@ -785,53 +828,55 @@ function media_element_process(&$element, &$form_state, $form) { '#markup' => '', '#description' => $element['#description'], '#required' => $element['#required'], + '#weight' => -100, ); - //@TODO: This should be loaded from the JS in case of a failed form submission. - $markup = ''; - if (!empty($file)) { - $preview = media_get_thumbnail_preview($file); - $markup = drupal_render($preview); - } + // @todo This should be loaded from the JS in case of a failed form submission. $element['preview'] = array( - '#type' => 'item', - '#markup' => $markup, '#prefix' => '