diff --git a/includes/MediaBrowserUpload.inc b/includes/MediaBrowserUpload.inc
index 03b5385..071d992 100644
--- a/includes/MediaBrowserUpload.inc
+++ b/includes/MediaBrowserUpload.inc
@@ -18,10 +18,14 @@ class MediaBrowserUpload extends MediaBrowserPlugin {
}
$upload_form = drupal_get_form($upload_form_id, $this->params);
- return array(
+ $plugin = array(
'form' => array($upload_form),
'#attached' => $attached,
);
+ // 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($upload_form);
+ return $plugin;
}
}
diff --git a/includes/media.browser.inc b/includes/media.browser.inc
index 2d3bae7..f024e53 100644
--- a/includes/media.browser.inc
+++ b/includes/media.browser.inc
@@ -120,17 +120,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[] = "" . check_plain($plugins[$key]['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[] = "" . check_plain($plugins[$key]['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 2aa9f9f..37e44d9 100644
--- a/js/media.browser.js
+++ b/js/media.browser.js
@@ -15,16 +15,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 96199e6..26ebd25 100644
--- a/media.module
+++ b/media.module
@@ -1179,3 +1179,20 @@ function media_load_all_exports($module, $directory, $extension, $name = NULL) {
return $return;
}
+
+/**
+ * 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/includes/MediaBrowserInternet.inc b/modules/media_internet/includes/MediaBrowserInternet.inc
index 1d66225..c3d9d9c 100644
--- a/modules/media_internet/includes/MediaBrowserInternet.inc
+++ b/modules/media_internet/includes/MediaBrowserInternet.inc
@@ -10,12 +10,16 @@ class MediaBrowserInternet extends MediaBrowserPlugin {
function view() {
// @todo: implement the multiselect argument here.
$from_web_form = drupal_get_form('media_internet_add', $this->params['types'], $this->params['multiselect']);
- return array(
+ $plugin = array(
'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;
}
}
diff --git a/modules/media_internet/media_internet.module b/modules/media_internet/media_internet.module
index 53c1817..54155d7 100644
--- a/modules/media_internet/media_internet.module
+++ b/modules/media_internet/media_internet.module
@@ -119,10 +119,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;
}
@@ -132,7 +132,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;
}
@@ -149,7 +149,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;
}
}
@@ -183,12 +183,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;
}