Drupal Behavior

  • What is Drupal behavior?
  • What level of service does it offer module developers?
  • What relation does it map to jQuery.ready ?
+52
jquery drupal drupal-6
Oct. 15 '10 at 10:33
source share
4 answers

Long version: Drupal.behaviors is not just a replacement for jQuery.ready, since the latter only works once (when the DOM is ready for manipulation): the behavior can be triggered several times during page execution and can be triggered whenever new DOM elements are inserted into the document.

In addition, modules can override or extend existing behavior (for example, if one of the modules has the behavior of adding a bounce effect on all links, the second module can replace the behavior with another bounce effect).

Short version: it is more modular, although the documentation can be improved.




In addition, starting with Drupal 7, parameters defined using drupal_add_js (PHP) or in Drupal.settings.modulename (Javascript) are passed directly as the second parameter (the first one of which is the context) to the behavior.

For example:

 Drupal.behaviors.changeLinks = function(context, settings){ if (!settings) settings = Drupal.settings.changeLinks; $("a", context).hover(function() { $(this).css('color', settings.color); }); }; 

And if one of your script (or others) creates new nodes, it can still apply behavior to new nodes, not knowing which other modules were installed:

 var newNodes = $('<a href="#">Hello</a> <a href="#">World</a>').appendTo('#someDiv'); Drupal.attachBehaviors(newNodes); 
+75
Oct. 15 '10 at 10:54
source share

Duplicated functionality

Note that the Drupal.behaviors architecture duplicates functionality already in jQuery.

In addition, at the time of writing, there was no documentation or case studies for Drupal.behaviors outside of Drupal itself ; and the documentation in Drupal (as mentioned above) can greatly help in improvement. At the time of this writing, it turned out that the main detailed documentation is available only for limited access.

This means that you may notice performance degradation, anomalies, and unexpected results that do not meet the jQuery standard , which are endemic to the Drupal.behaviors ecosystem.

JQuery native functionality

Unlike Drupal.behaviors, the built-in functionality of the standard jQuery API is widely documented , including built-in demos and examples . In addition, there are many live examples available on sites like jsfiddle.

The links in the see section also list jQuery api calls related to handling new DOM elements inserted into a document.

see also

+8
Sep 26 '13 at 16:15
source share

Besides the answers above about key points, you can transfer data from php to javascript as follows

Passing values ​​from PHP to Javascript using "Drupal.settings"

You can easily make variables from PHP available to Javascript on the front panel using Drupal.settings using the drupal_add_js () function

 <?php drupal_add_js(array('myModule' => array('key' => 'value')), 'setting'); ?> 

or

 <?php $element['#attached']['js'][] = array( 'type' => 'setting', 'data' => array('myModule' => array('key' => 'value')), ); ?> 

This will be available in Javascript as:

  if (Drupal.settings.myModule.key === 'value') { alert('Got it!'); } 
+4
Feb 28 '16 at 9:26
source share

Looking for a similar answer and came here, still without clues. Finally, a little more explanation (and examples) was found from the article here: https://benmarshall.me/drupal-behaviors/

I am not an author, so I can cite only some texts:

What is Drupal behavior?

In short, Drupal.behaviors is a more modular and better way to implement jQuery.ready. Unlike jQuery.ready, which runs only once when the DOM is ready to be manipulated, Drupal.behaviors can be run multiple times during page execution. Even better, they can be run when new DOM elements are inserted into the document (i.e. AJAX-driven content).

Drupal.behaviors can also override or even extend existing behaviors. So, for example, if the behavior of a module adds a bounce effect on all links, another module can replace this behavior with a different bounce effect.

Another additional bonus of Drupal.behaviors (starting with Drupal 7) is the ability to use drupal_add_js (PHP) or Drupal.settings.modulename (JS) and the transfer parameters as the second parameter (the first of which is the context) to the behavior.

+2
Jul 17 '15 at 10:11
source share



All Articles