diff --git a/includes/media.browser.inc b/includes/media.browser.inc
index d4a426b..34effae 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;
}
@@ -255,11 +264,13 @@ function media_media_browser_plugin_view($plugin_name, $params) {
}
$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/js/media.browser.js b/js/media.browser.js
index 8d4ac13..d0a2b87 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.module b/media.module
index a74653e..248fa38 100644
--- a/media.module
+++ b/media.module
@@ -1151,3 +1151,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.module b/modules/media_internet/media_internet.module
index 63bab07..7b49292 100644
--- a/modules/media_internet/media_internet.module
+++ b/modules/media_internet/media_internet.module
@@ -44,13 +44,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;
}
@@ -129,10 +133,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;
}
@@ -142,7 +146,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;
}
@@ -159,7 +163,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;
}
}
@@ -193,12 +197,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', 'Unknown error: unable to add file, please check URL / Embed code and try again ' . $embed_code);
+ form_set_error('embed_code', t('Unknown error: unable to add file. Please check URL or embed code and try again.'));
return;
}