On Github pbuyle / dcmtl-javascript
by Pierre Buyle
scripts[] = awesomeness.js
drupal_add_js($data, $options)
$element['#attached']['js'][] = array($data => $options)
scripts[] = awesomeness.js
Add JavaScript to the currently rendered page.
drupal_add_js('path/to/awesomeness.js', 'file')
drupal_add_js('alert("Awesome!");', 'inline');
drupal_add_js(array('myAwesomeModule' => array( 'foo' => 'bar', 'bar' => 'foo', )), 'setting');
drupal_add_js('//aweso.me/ness.js', 'external');
drupal_add_js('path/to/awesomeness.js', array( 'type' => 'file',
'scope' => 'footer',
'group' => 'JSTHEME', 'weigth' => '-10',
'preprocess' => TRUE, 'every_page' => TRUE,
'defer' => TRUE, 'cache' => FALSE,
));
Attach JavaScript to a renderable array.
$element['#attached']['js'][] = array($data => $options);Invoke drupal_add_js($data, $options) when $element is rendered.
$element['#attached']['js'][] = array('path/to/awesomeness.js', 'file')
$element['#attached']['js'][] = array('alert("Awesome!");', 'inline');
$element['#attached']['js'][] = array(array('myAwesomeModule' => array( 'foo' => 'bar', 'bar' => 'foo', )), 'setting');
$element['#attached']['js'][] = array('//aweso.me/ness.js', 'external');
Things to known and use when writing JavaScript for Drupal
(function($) { // Here $ is the jQuery object })(jQuery);
Replace $(document).ready() to process elements.
Drupal.behaviors.doSomething = { attach: function(context, settings) { // do something hereAlso used when element are removed, moved or serialized.
} detach: function(context, settings, trigger) { // do something here too
} }Attach and detach behavior altering the DOM
Drupal.attachBehaviors(context, [settings]) Drupal.detachBehaviors(context, [settings], [trigger])
$('div.something', context).once('do-something');
Passing values from PHP to Javascript
drupal_add_js( array('myAwesomeModule' => array( 'foo' => 'bar', 'bar' => 'foo', ) ), 'setting');
Drupal.settings.myAwesomeModule.foo; Drupal.settings.myAwesomeModule.bar; Drupal.behaviors.doSomething = { attach: function(context, settings) { settings.myAwesomeModule.foo } }
(function($) { Drupal.behaviors.doSomething = { attach: function(context, settings) { $('div.something', context).once('do-something').doSomething({ foo: settings.myAwesomeModule.foo, bar: settings.myAwesomeModule.bar }); } } })(jQuery);
Theme functions in JavaScript.
Dupal.theme.prototype.awesome = function(str) { return '<span class="awesome">' + str + '</span>'; }
var html = Drupal.theme('awesome', 'Hello World');
Dupal.theme.awesome = function(str) { return '<div class="awesome">' + str + '</div>'; }
Drupal.t(str, args, options)
Drupal.formatPlural(count, singular, plural, args, options)
Drupal.formatString(str, args)
Drupal.checkPlain(str)
Writing re-usable JavaScript with dependencies management.
A library defines a set of JavaScript files
/** * Implements hook_library(). */ awesome_library() { return array( 'awesomeness' => array( 'title' => 'Awesome Library', 'website' => 'http://aweso.me/', 'version' => '1.0.0' 'js' => array('full/path/to/awesome.js' => array()),and/or CSS files
'css' => array('full/path/to/awesome.css' => array()),optionally using settings
'js' => array(array('type' => 'setting', 'data' => $settings)),and optionally requiring another library
'dependencies' => array( array($module, $library), )
), ) }
drupal_add_library($module, $library)
$element['#attached']['library'][] = array($module, $library)
How to use and manage third party libraries.
/** * Implements hook_libraries_info() */ function awesome_libraries_info() { return array( 'awesome' => array( 'name' => 'Awesome Library', 'vendor url' => 'http://aweso.me', 'download url' => 'https://github.com/awesome/awesomejs/zipball/latest', 'version arguments' => array( 'file' => 'package.json', 'pattern' => '/"version": "(.*)"/', ), 'files' => array('js' => array('awesome.min.js')), ); );
/** * Implements hook_library() */ function awesome_library() { $libraries = array(); if (($library = libraries_detect('awesome')) && $library['installed']) { $libraries['awesome'] = array( 'title' => $library['name'], 'website' => $library['vendor url'], 'version' => $library['version'], 'js' => array( library['library path'] . '/' . $library['files']['js'][0] ) ); } return $libraries }
(function($) { Drupal.behaviors.doSomething = { attach: function(context, settings) { $('div.something', context).once('do-something').doSomething({ foo: settings.myAwesomeModule.foo, bar: settings.myAwesomeModule.bar }); } } })(jQuery);