Skip to content

Instantly share code, notes, and snippets.

@LittleCoding
Created November 3, 2021 18:46
Show Gist options
  • Save LittleCoding/c80578c359767bceeb9a3fbf7f2582d5 to your computer and use it in GitHub Desktop.
Save LittleCoding/c80578c359767bceeb9a3fbf7f2582d5 to your computer and use it in GitHub Desktop.
Basic JS for Drupal 8/9 project (module, theme)
window.Drupal = window.Drupal || { behaviors: {}, locale: {} };
(function ($, Drupal, settings) {
'use strict';
Drupal.myproject = {
preInit: function() {},
init: function(settings) {},
postInit: function() {}
};
Drupal.behaviors.myproject = {
attach: function (context, settings) {
// DOM loaded
Drupal.myproject.preInit();
$(document, context).once('myprojectInit').each( function() {
// Page ready -- with single run wrapper
Drupal.myproject.init(settings);
$(window).on('load', function() {
// Page fully loaded
Drupal.myproject.postInit();
});
});
}
};
})(jQuery, Drupal, drupalSettings);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment