1

The structure of Drupal.behaviors is rather similar in Drupal 7 and Drupal 8. Where do attach() and detach() come from? Thid code is from drupal.js , still wondering where attach method in this.attach is defined?

Drupal.attachBehaviors = function (context, settings) {
  context = context || document;
  settings = settings || Drupal.settings;
  // Execute all of them.
  $.each(Drupal.behaviors, function () {
    if ($.isFunction(this.attach)) {
      this.attach(context, settings);
    }
  });
};
4

The attach() and detach() methods are part of the Behaviors interface defined by Drupal:

Any object defined as a property of Drupal.behaviors will get its attach() method called when the DOM has loaded both initially and after any AJAX calls.

https://www.drupal.org/docs/8/api/javascript-api/javascript-api-overview

  • Thanx. I see that I didnt have good understanding of JS and declaring methods, I was thinking this was some already declared method like api.jquery.com/append that I can use on elements, objects. I dig a bit deeper and saw what is what. – Marko Blazekovic Jan 25 '19 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.