diff --git a/includes/media.browser.inc b/includes/media.browser.inc
index ae1c826..8c0d010 100644
--- a/includes/media.browser.inc
+++ b/includes/media.browser.inc
@@ -69,17 +69,26 @@ function media_browser($selected = NULL) {
//Add any JS settings
$browser_settings[$key] = isset($plugin['#settings']) ? $plugin['#settings'] : array();
- // If this is a "ajax" style tab, add the href, otherwise an id.
- $href = isset($plugin['#callback']) ? $plugin['#callback'] : "#media-tab-$key";
- $tabs[] = "{$plugin['#title']}";
+ $tab_id = "media-tab-$key";
+ if (isset($plugin['#callback'])) {
+ // If this is a "ajax" style tab, where the plugin provides a callback
+ // URL for the tab's content, add that callback URL to the link.
+ $href = $plugin['#callback'];
+ }
+ else {
+ // For regular plugins that provide HTML content, add a link fragment
+ // as the href, so jQuery UI Tabs has something to work with.
+ $href = '#' . $tab_id;
+ }
+ $tabs[] = "{$plugin['#title']}";
// Create a div for each tab's content.
$plugin['#prefix'] = <<
+
EOS;
$plugin['#suffix'] = <<
-
+
EOS;
}
diff --git a/js/media.browser.js b/js/media.browser.js
index c2809bd..28e5d4f 100644
--- a/js/media.browser.js
+++ b/js/media.browser.js
@@ -11,16 +11,34 @@ Drupal.media.browser.selectionFinalized = function (selectedMedia) {
};
Drupal.behaviors.experimentalMediaBrowser = {
- attach: function (context) {
- if (Drupal.settings.media.selectedMedia) {
+ attach: function (context, settings) {
+ var active, index, plugins, tabSettings;
+ if (settings.media.selectedMedia) {
Drupal.media.browser.selectMedia(Drupal.settings.media.selectedMedia);
// Fire a confirmation of some sort.
Drupal.media.browser.finalizeSelection();
}
- $('#media-browser-tabset').tabs({
- show: Drupal.media.browser.resizeIframe
+ // If one of the plugins has requested that its tab be active (after a
+ // validation error, for example) make that the active tab.
+ plugins = settings.media.browser;
+ $.each(plugins, function (pluginName) {
+ if (this.active) {
+ active = pluginName;
+ }
});
-
+ tabSettings = {
+ show: Drupal.media.browser.resizeIframe
+ };
+ if (active) {
+ // To set a tab as selected, we have to first find its zero-based index
+ // within the list of tabs.
+ var tab = $('a#media-tab-' + active + '-link');
+ index = $('#media-browser-tabset a').index(tab);
+ if (index > -1) {
+ tabSettings.selected = index;
+ }
+ }
+ $('#media-browser-tabset', context).tabs(tabSettings);
$('.media-browser-tab').each( Drupal.media.browser.validateButtons );
}
diff --git a/media.media.inc b/media.media.inc
index 55cd985..04da4db 100644
--- a/media.media.inc
+++ b/media.media.inc
@@ -55,11 +55,13 @@ function media_media_browser_plugin_view($plugin_name, $params) {
module_load_include('inc', 'media', 'includes/media.pages');
$upload_form = drupal_get_form($upload_form_id, $params);
- return array(
+ $plugin = array(
'#title' => t('Upload'),
'form' => array($upload_form),
'#attached' => $attached,
+ '#settings' => array('active' => media_form_has_errors($upload_form)),
);
+ return $plugin;
break;
case 'library':
return array(
diff --git a/media.module b/media.module
index 0da1f34..93a220c 100644
--- a/media.module
+++ b/media.module
@@ -1180,3 +1180,20 @@ function media_views_api() {
'path' => drupal_get_path('module', 'media') . '/includes',
);
}
+
+/**
+ * Helper function to determine whether a form has any errors.
+ */
+function media_form_has_errors($form) {
+ $error = form_get_error($form);
+ if (!empty($error)) {
+ return TRUE;
+ }
+ $children = element_children($form);
+ foreach ($children as $element) {
+ if (media_form_has_errors($form[$element])) {
+ return TRUE;
+ }
+ }
+ return FALSE;
+}
diff --git a/modules/media_internet/media_internet.media.inc b/modules/media_internet/media_internet.media.inc
index 25eb22c..3b2b28a 100644
--- a/modules/media_internet/media_internet.media.inc
+++ b/modules/media_internet/media_internet.media.inc
@@ -34,13 +34,17 @@ function media_internet_media_browser_plugin_view($plugin_name, $params) {
case 'media_internet':
// @todo: implement the multiselect argument here.
$from_web_form = drupal_get_form('media_internet_add', $types, $multiselect);
- return array(
+ $plugin = array(
'#title' => t('Web'),
'form' => array($from_web_form),
'#attached' => array(
//'js' => array($path . '/js/plugins/media.fromurl.js'),
),
);
+ // If we're redisplaying this form after a validation error, set it to be
+ // the active tab in the media browser.
+ $plugin['#settings']['active'] = media_form_has_errors($from_web_form);
+ return $plugin;
break;
}
diff --git a/modules/media_internet/media_internet.module b/modules/media_internet/media_internet.module
index 8805b6b..59b0fbe 100644
--- a/modules/media_internet/media_internet.module
+++ b/modules/media_internet/media_internet.module
@@ -95,10 +95,10 @@ function media_internet_add_validate($form, &$form_state) {
$provider = media_internet_get_provider($embed_code);
$provider->validate();
} catch (MediaInternetNoHandlerException $e) {
- form_set_error('url', $e->getMessage());
+ form_set_error('embed_code', $e->getMessage());
return;
} catch (MediaInternetValidationException $e) {
- form_set_error('url', $e->getMessage());
+ form_set_error('embed_code', $e->getMessage());
return;
}
@@ -108,7 +108,7 @@ function media_internet_add_validate($form, &$form_state) {
try {
$file = $provider->getFileObject();
} catch (Exception $e) {
- form_set_error('url', $e->getMessage());
+ form_set_error('embed_code', $e->getMessage());
return;
}
@@ -125,7 +125,7 @@ function media_internet_add_validate($form, &$form_state) {
else {
$message .= ' ' . array_pop($errors);
}
- form_set_error('url', $message);
+ form_set_error('embed_code', $message);
return FALSE;
}
}
@@ -159,12 +159,12 @@ function media_internet_add_submit($form, &$form_state) {
$file = $provider->save();
}
catch (Exception $e) {
- form_set_error('url', $e->getMessage());
+ form_set_error('embed_code', $e->getMessage());
return;
}
if (!$file->fid) {
- form_set_error('url', t('The file %file could not be saved. An unknown error has occurred.', array('%file' => $embed_code)));
+ form_set_error('embed_code', t('The file %file could not be saved. An unknown error has occurred.', array('%file' => $embed_code)));
return;
}