You may take it or leave it - I know it looks like a pretty big change out of the blue, but I spent an evening on this so thought I'd at least share.

Use-case

I'm working on some highly advanced field formatters to build some layouts that leverage vertical tabs, jquery UI and things. They have their own js and css they need.
I also want to use the DS view mode switcher - for A/B testing as much as anything, but at least two versions of a layout will be exposed in the end also.

Problem

The current (7.x-2.x) iteration of the view mode switcher buttons is a pretty lightweight use of AJAX. It's got some buttons that don't work if you open in new tab, you can't bookmark specific versions of a page, it doesn't seem to have had much love.
But most importantly, it doesn't use the Drupal API method for refreshing page resources. Javascripts that are not loaded on the first version of a display are not available if you use the view switcher, so things don't work.

I'm using jquery UI, but similar issues would be seen of you were embedding a slideshow or a video or anything fancy on a second view mode.

Solution

With reference to an old presentation by merlinofchaos and a lot of trial and error studying what was going on with AJAX in core I worked out the one true way to do AJAX and keep the rest of our Drupal site working transparently. The good news is that the result is significantly simpler, and has less code than before, while being more powerful and likely to be robust when combined with other features.

* Replaced the button links with class="use-ajax" style links instead
* Re-instated access directly to the view-mode-by-url (argument) which seemed to have been lost
* Adjusted the callback to use the no-js/ajax system that core & views uses
* totally dropped the custom jquery AJAX handler - it's entirely redundant when replaced by misc/ajax.js
* Also did some rendering tidy-ups to bring things into current practice - converted a load of string concatenation into drupal_render sort of structure (though didn't bother with a theme or a hook_alter yet)
* use #attached for library inclusion, not a conditional drupal_add_js()
* Tried to improve docs and code style as I went, but only in moderation.

Result

Now you can use view modes that require javascripts, and view mode switcher at the same time.

I understand if this is not important enough to go in - but if it's not worthy, I may fork this off into a stand-alone project anyway, just because I find it useful. I'd rather get it in to DS proper though, as the functionality belongs here - but deserves to work better here.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dman’s picture

Patch. It's not huge really - it actually removes a lot of code.

aspilicious’s picture

Version: 7.x-2.6 » 8.x-2.x-dev
Priority: Normal » Major

Great! In the jungle of issues, I missed this one, I'm afraid to change this functionality so drastically at this point.
You probably don't have any time but *if* you have some could you port the patch to D8?

dman’s picture

Assigned: Unassigned » dman

Was a while ago, and I'm not up to speed with D8 (still) but I'll stick it in my queue and see if I can resurrect my testing cases. No promises.

aspilicious’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.