Skip to content

Instantly share code, notes, and snippets.

@ao5357
Created December 15, 2021 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ao5357/72e9cb31fcb5bc907ea72245f4d1978b to your computer and use it in GitHub Desktop.
Save ao5357/72e9cb31fcb5bc907ea72245f4d1978b to your computer and use it in GitHub Desktop.
Failed attempt to use a mutationObserver to replace a Drupal destination parameter on a use-ajax trigger link. The Drupal.ajax event listener registers it before the fix!
/**
* @file
* Global utilities.
*/
var michiganDesignInit = false; // Global scoped to track attachment just once.
(function($, Drupal) {
Drupal.behaviors.michigan_design = {
attach: function(context, settings) {
if (context === document && !michiganDesignInit) {
michiganDesignInit = true;
// Switch incorrect /views/ajax destination for current page path.
var $bootstrapAccordion = document.querySelector('.block-bootstrap-collapse');
if ($bootstrapAccordion && "MutationObserver" in window) {
var mutationObserver = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
if (mutation.type === 'childList') {
var $destinations = mutation.target.querySelectorAll('a[href*="destination=/views/ajax"]');
if ($destinations.length) {
Array.prototype.forEach.call($destinations, (destination) => {
var href = destination.getAttribute('href');
var newHref = href.replace('/views/ajax', window.location.pathname);
destination.setAttribute('href', newHref);
});
}
}
});
});
mutationObserver.observe($bootstrapAccordion, {
attributes: true,
characterData: true,
childList: true,
subtree: true,
attributeOldValue: false,
characterDataOldValue: false,
});
}
}
}
};
})(jQuery, Drupal);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment