SlideShare uma empresa Scribd logo
1 de 96
Drupal Javascript for
developers
Whoami?
• C lin Mariană
• Lead developer @ Dream Production
• calin@dreamproduction.com
• d.o: mariancalinro
• @mariancalinro
• github.com/calin-marian
What will we cover?
• Adding JS to the page, both at module and theme level
• Writing Drupal aware JS code
• Libraries management
• Ajax framework
• Drupal JS functions
• Drupal JS theme functions
• If we have enough time … some Drupal 8 changes
Adding JS to page
• declare it in the .info file of your module
scripts[] = path/to/component.js
• Using drupal_add_js()
drupal_add_js(drupal_get_path(‘module’, ‘mymodule’) .
‘/path/to/component.js’);
• attach it to a render array
$build[‘myelement’] = array(
‘#theme’ => ‘my_theme’,
‘#myvar’ => $myvar,
‘#attached’ => array(
‘js’ => drupal_get_path(‘module’, ‘mymodule') .
‘/path/to/component.js’
),
);
Adding JS to page
• declare it in the .info file of your module
scripts[] = path/to/component.js
• Using drupal_add_js()
drupal_add_js(drupal_get_path(‘module’, ‘mymodule') .
‘/path/to/component.js’);
• attach it to a render array
$build[‘myelement’] = array(
‘#theme’ => ‘my_theme’,
‘#myvar’ => $myvar,
‘#attached’ => array(
‘js’ => drupal_get_path(‘module’, ‘mymodule') .
‘/path/to/component.js’
),
);
Adding JS to page
• declare it in the .info file of your module
scripts[] = path/to/component.js
• Using drupal_add_js()
drupal_add_js(drupal_get_path(‘module’, ‘mymodule') .
‘/path/to/component.js’);
• attach it to a render array
$build[‘myelement’] = array(
‘#theme’ => ‘my_theme’,
‘#myvar’ => $myvar,
‘#attached’ => array(
‘js’ => drupal_get_path(‘module’, ‘mymodule') .
‘/path/to/component.js’
),
);
Adding JS to page
• declare it in the .info file of your module
scripts[] = path/to/component.js
• Using drupal_add_js()
drupal_add_js(drupal_get_path(‘module’, ‘mymodule') .
‘/path/to/component.js’);
• attach it to a render array
$build[‘myelement’] = array(
‘#theme’ => ‘my_theme’,
‘#myvar’ => $myvar,
‘#attached’ => array(
‘js’ => drupal_get_path(‘module’, ‘mymodule') .
‘/path/to/component.js’
),
);
Adding JS to page
• for a theme, there are 2 ways to add a JS file to the page:
• declare it in the .info file of your theme, same as module
scripts[] = path/to/component.js
• Using drupal_add_js() from template.php, in the
hook_preprocess_html() function
Adding JS to page
• for a theme, there are 2 ways to add a JS file to the page:
• declare it in the .info file of your theme, same as module
scripts[] = path/to/component.js
• Using drupal_add_js() from template.php, in the
hook_preprocess_html() function
Adding JS to page
• for a theme, there are 2 ways to add a JS file to the page:
• declare it in the .info file of your theme, same as module
scripts[] = path/to/component.js
• Using drupal_add_js() from template.php, in the
hook_preprocess_html() function
Closures
(function ($) {
// Code that uses jQuery's $ can follow here.
$(‘a’).on(‘click’, function(event){
event.preventDefault();
alert(‘Links are disabled, you are trapped on this
page. Hahahaha!!!’);
});
var window = "Whoops, at least I only broke my code.";
}(jQuery));
Closures
(function ($) {
// Code that uses jQuery's $ can follow here.
$(‘a’).on(‘click’, function(event){
event.preventDefault();
alert(‘Links are disabled, you are trapped on this
page. Hahahaha!!!’);
});
var window = "Whoops, at least I only broke my code.";
}(jQuery));
Closures
(function ($) {
// Code that uses jQuery's $ can follow here.
$(‘a’).on(‘click’, function(event){
event.preventDefault();
alert(‘Links are disabled, you are trapped on this
page. Hahahaha!!!’);
});
var window = "Whoops, at least I only broke my code.";
}(jQuery));
Settings
<?php
drupal_add_js(array(
'myModule' => array(
'key' => 'value'
)
), 'setting');
==================================
(function($){
console.log(Drupal.settings.myModule.key); // logs 'value'
})(jQuery)
Settings
<?php
drupal_add_js(array(
'myModule' => array(
'key' => 'value'
)
), 'setting');
==================================
(function($){
console.log(Drupal.settings.myModule.key); // logs 'value'
})(jQuery)
Settings
<?php
drupal_add_js(array(
'myModule' => array(
'key' => 'value'
)
), 'setting');
==================================
(function($){
console.log(Drupal.settings.myModule.key); // logs 'value'
})(jQuery)
Behaviors
• Drupal’s way of dealing with attaching and detaching functionalities
to dynamic content.
• Invoked by Drupal automatically when content is added or removed
to the page by the Ajax framework
• Objects in the namespace Drupal.behaviors that have the
methods attach and detach - detach is optional if you do not need
to run some code when content is removed from page.
Behaviors
(function($){
Drupal.behaviors.myComponent = {
attach: function(context, settings) {
// Make your DOM manipulations and attach your
// event handlers for your component.
},
detach: function(context, settings) {
// This is optional, use it if your component needs to destroy
// variables to free memory, or do other tasks when the content
// your component is attached to is removed from the DOM.
}
}
})(jQuery)
Behaviors
(function($){
Drupal.behaviors.myComponent = {
attach: function(context, settings) {
// Make your DOM manipulations and attach your
// event handlers for your component.
},
detach: function(context, settings) {
// This is optional, use it if your component needs to destroy
// variables to free memory, or do other tasks when the content
// your component is attached to is removed from the DOM.
}
}
})(jQuery)
Behaviors
(function($){
Drupal.behaviors.myComponent = {
attach: function(context, settings) {
// Make your DOM manipulations and attach your
// event handlers for your component.
},
detach: function(context, settings) {
// This is optional, use it if your component needs to destroy
// variables to free memory, or do other tasks when the content
// your component is attached to is removed from the DOM.
}
}
})(jQuery)
Behaviors
(function($){
Drupal.behaviors.myBxSlider = {
attach: function(context, settings) {
var options = settings.myBxSlider,
Drupal.myBxSlider = $(options.sliderSelector, context)
.bxSlider(options.sliderOptions);
},
detach: function(context, settings) {
delete Drupal.myBxSlider;
}
}
})(jQuery)
Behaviors
(function($){
Drupal.behaviors.myBxSlider = {
attach: function(context, settings) {
var options = settings.myBxSlider,
Drupal.myBxSlider = $(options.sliderSelector, context)
.bxSlider(options.sliderOptions);
},
detach: function(context, settings) {
delete Drupal.myBxSlider;
}
}
})(jQuery)
Behaviors
• When adding content to the page, call Drupal.attachBehaviors
on the content. This allows other components to attach themselves to
the content. The Ajax framework does this for you automatically.
• Example:
Drupal.attachBehaviors(insertedContent);
• Bad example:
Drupal.attachBehaviors();
• When run without context, the full page is the context. This means
behaviors’ attach methods will run more than once, on the same
content.
Behaviors
• When adding content to the page, call Drupal.attachBehaviors
on the content. This allows other components to attach themselves to
the content. The Ajax framework does this for you automatically.
• Example:
Drupal.attachBehaviors(insertedContent);
• Bad example:
Drupal.attachBehaviors();
• When run without context, the full page is the context. This means
behaviors’ attach methods will run more than once, on the same
content.
Drupal once
• To protect yourself against having your code run twice on the same
content, Drupal implements a jQuery method, called once:
$(selector).once(stringKey, handler)
• The handler is only called once, no mater how many times the
method is invoked on the same content.
• It works by adding a class on the content, and checking for that class
when it’s invoked.
• Alternative usage:
$(selector).once(‘myComponent’).wrap(‘<div
class=“myWrapper”></div>’);
Drupal once
(function($){
Drupal.behaviors.myExample = {
attach: function(context, settings) {
$(‘a’, context)
.once(‘notification’)
.click(function(event){
event.preventDefault();
alert(‘Links are disabled, you are trapped on this
page. Hahahaha!!!’);
});
}
}
})(jQuery);
Drupal once
(function($){
Drupal.behaviors.myExample = {
attach: function(context, settings) {
$(‘a’, context)
.once(‘notification’)
.click(function(event){
event.preventDefault();
alert(‘Links are disabled, you are trapped on this
page. Hahahaha!!!’);
});
}
}
})(jQuery);
Drupal once
(function($){
Drupal.behaviors.myExample = {
attach: function(context, settings) {
$(‘a’, context).once(‘notification’, function(){
$(this).click(function(event){
event.preventDefault();
alert(‘Links are disabled, you are trapped on this
page. Hahahaha!!!’);
});
});
}
}
})(jQuery);
Libraries
• Allows you to add all the JavaScript and CSS of one component at
once
• Example:
drupal_add_library('system', 'ui.accordion');
$build['#attached']['library'][] = array('system',
‘ui.accordion');
• hook_library - to define your library
• hook_library_alter - to modify libraries provided by other
modules
Libraries
• Allows you to add all the JavaScript and CSS of one component at
once
• Example:
drupal_add_library('system', 'ui.accordion');
$build['#attached']['library'][] = array('system',
‘ui.accordion');
• hook_library - to define your library
• hook_library_alter - to modify libraries provided by other
modules
Libraries
• Allows you to add all the JavaScript and CSS of one component at
once
• Example:
drupal_add_library('system', 'ui.accordion');
$build['#attached']['library'][] = array('system',
‘ui.accordion');
• hook_library - to define your library
• hook_library_alter - to modify libraries provided by other
modules
function system_library() {
// ...
$libraries['ui.accordion'] = array(
'title' => 'jQuery UI: Accordion',
'website' => 'http://jqueryui.com/demos/accordion/',
'version' => '1.7.2',
'js' => array(
'misc/ui/ui.accordion.js' => array(),
),
'css' => array(
'misc/ui/ui.accordion.css' => array(),
),
'dependencies' => array(
array('system', 'ui'),
),
);
return $libraries;
}
function system_library() {
// ...
$libraries['ui.accordion'] = array(
'title' => 'jQuery UI: Accordion',
'website' => 'http://jqueryui.com/demos/accordion/',
'version' => '1.7.2',
'js' => array(
'misc/ui/ui.accordion.js' => array(),
),
'css' => array(
'misc/ui/ui.accordion.css' => array(),
),
'dependencies' => array(
array('system', 'ui'),
),
);
return $libraries;
}
function system_library() {
// ...
$libraries['ui.accordion'] = array(
'title' => 'jQuery UI: Accordion',
'website' => 'http://jqueryui.com/demos/accordion/',
'version' => '1.7.2',
'js' => array(
'misc/ui/ui.accordion.js' => array(),
),
'css' => array(
'misc/ui/ui.accordion.css' => array(),
),
'dependencies' => array(
array('system', 'ui'),
),
);
return $libraries;
}
function system_library() {
// ...
$libraries['ui.accordion'] = array(
'title' => 'jQuery UI: Accordion',
'website' => 'http://jqueryui.com/demos/accordion/',
'version' => '1.7.2',
'js' => array(
'misc/ui/ui.accordion.js' => array(),
),
'css' => array(
'misc/ui/ui.accordion.css' => array(),
),
'dependencies' => array(
array('system', 'ui'),
),
);
return $libraries;
}
Ajax Framework
• Ajax is a technique that updates content on the page from the server
without page refresh.
• Drupal’s Ajax framework helps you with this task.
• It implements more than a few commands that you can trigger from
the response of the Ajax call.
• Does some boilerplate code.
Ajax Framework
• ajax_command_insert()
• ajax_command_before()
• ajax_command_after()
• ajax_command_replace()
• ajax_command_invoke()
• ajax_command_settings()
• For the full list, google “Drupal Ajax framework commands”
Ajax Framework
• A php array such as...
array(
'command' => 'insert',
'method' => 'append',
'selector' => '#my-div',
'data' => '<div>Some new content</div>',
)
• ... will turn into this object received on the js side:
{ command: insert, method: append, selector:#my-
div, data: '<div>Some new content</div>' }
• ... a clear instruction for ajax.js to follow
Ajax Framework
• To implement new command in js:
Drupal.ajax.prototype.commands.myCommand = function
(ajax, response, status) {
// in response you have an object with the properties
// sent from php, such as myvar, selector, data.
}
• Then in php, you can return the following array from an Ajax call:
array(
'command' => 'myCommand',
'myvar' => 'myvalue',
'selector' => '#my-div',
'data' => '<div>Some new content</div>',
)
Ajax Framework
Example
Ajax Framework
• You print somewhere in the page this link:
array(
'#type' => 'link',
'#title' => t('Ajax Link'),
'#href' => 'my-ajax-test/nojs',
'#suffix' => '<div id="ajax-display"></div>',
'#ajax' => array(
'effect' => 'fade',
),
);
Ajax Framework
• You print somewhere in the page this link:
array(
'#type' => 'link',
'#title' => t('Ajax Link'),
'#href' => 'my-ajax-test/nojs',
'#suffix' => '<div id="ajax-display"></div>',
'#ajax' => array(
'effect' => 'fade',
),
);
Ajax Framework
• You print somewhere in the page this link:
array(
'#type' => 'link',
'#title' => t('Ajax Link'),
'#href' => 'my-ajax-test/nojs',
'#suffix' => '<div id="ajax-display"></div>',
'#ajax' => array(
'effect' => 'fade',
),
);
Ajax Framework
• Alternative way:
array(
'#type' => 'link',
'#title' => t('Ajax Link'),
'#href' => 'my-ajax-test/nojs',
'#suffix' => '</div><div id="ajax-
display"></div>',
'#attributes' => array(
'class' => array(‘use-ajax’),
),
);
Ajax Framework
• You need to define a new menu item:
$items['my-ajax-test/%'] = array(
'title' => 'Ajax test callback',
'type' => MENU_CALLBACK,
'page callback' => 'ajax_link_callback',
'page arguments' => array(1),
'access arguments' => array('access content'),
);
Ajax Framework
• You need to define a new menu item:
$items['my-ajax-test/%'] = array(
'title' => 'Ajax test callback',
'type' => MENU_CALLBACK,
'page callback' => 'ajax_link_callback',
'page arguments' => array(1),
'access arguments' => array('access content'),
);
Ajax Framework
• Then, define the callback for that menu item:
function ajax_link_callback($ajax) {
$time = t('The current time is: !time',
array('!time' => date('Y-m-d H:i:s')));
if ($ajax == 'ajax') {
$commands = array();
$commands[] = ajax_command_replace(
'#ajax-display',
"<div id='ajax-display'>" . $time . "</div>");
ajax_deliver(array(
'#type' => ‘ajax',
'#commands' => $commands));
} else {
return array('#markup' => $time);
}
}
Ajax Framework
• Then, define the callback for that menu item:
function ajax_link_callback($ajax) {
$time = t('The current time is: !time',
array('!time' => date('Y-m-d H:i:s')));
if ($ajax == 'ajax') {
$commands = array();
$commands[] = ajax_command_replace(
'#ajax-display',
"<div id='ajax-display'>" . $time . "</div>");
ajax_deliver(array(
'#type' => ‘ajax',
'#commands' => $commands));
} else {
return array('#markup' => $time);
}
}
Ajax Framework
• Then, define the callback for that menu item:
function ajax_link_callback($ajax) {
$time = t('The current time is: !time',
array('!time' => date('Y-m-d H:i:s')));
if ($ajax == 'ajax') {
$commands = array();
$commands[] = ajax_command_replace(
'#ajax-display',
"<div id='ajax-display'>" . $time . "</div>");
ajax_deliver(array(
'#type' => ‘ajax',
'#commands' => $commands));
} else {
return array('#markup' => $time);
}
}
Ajax Framework
• Then, define the callback for that menu item:
function ajax_link_callback($ajax) {
$time = t('The current time is: !time',
array('!time' => date('Y-m-d H:i:s')));
if ($ajax == 'ajax') {
$commands = array();
$commands[] = ajax_command_replace(
'#ajax-display',
"<div id='ajax-display'>" . $time . "</div>");
ajax_deliver(array(
'#type' => ‘ajax',
'#commands' => $commands));
} else {
return array('#markup' => $time);
}
}
Ajax Framework
• Then, define the callback for that menu item:
function ajax_link_callback($ajax) {
$time = t('The current time is: !time',
array('!time' => date('Y-m-d H:i:s')));
if ($ajax == 'ajax') {
$commands = array();
$commands[] = ajax_command_replace(
'#ajax-display',
"<div id='ajax-display'>" . $time . "</div>");
ajax_deliver(array(
'#type' => ‘ajax',
'#commands' => $commands));
} else {
return array('#markup' => $time);
}
}
Ajax Framework
• Then, define the callback for that menu item:
function ajax_link_callback($ajax) {
$time = t('The current time is: !time',
array('!time' => date('Y-m-d H:i:s')));
if ($ajax == 'ajax') {
$commands = array();
$commands[] = ajax_command_replace(
'#ajax-display',
"<div id='ajax-display'>" . $time . "</div>");
ajax_deliver(array(
'#type' => ‘ajax',
'#commands' => $commands));
} else {
return array('#markup' => $time);
}
}
Ajax Forms
• Ajax forms in Drupal are provided by the Form API.
• You write the form using the Drupal Form API.
• You do not write or see any Javascript.
Ajax Forms
• Specifiy what form element activates the Ajax behavior by adding #ajax
to it.
• Specify what part of the form’s HTML is to be replaced by the callback
using the ‘wrapper’ attribute.
• Provide a callback function that receives the whole rebuild form, and
returns the piece of form that will replace the wrapper content.
• Your form builder needs to take into account $form_state[‘values’]
when building the form.
• Find examples in the Drupal Examples for Developers(examples)
module.
Ajax Forms
• Example of FAPI Ajax:
$form['dropdown_first'] = array(
'#type' => 'select',
'#title' => 'Instrument Type',
'#options' => $options_first,
'#default_value' =>
$form_state['values']['dropdown_first'],
'#ajax' => array(
'callback' =>
'dependent_dropdown_callback',
'wrapper' => 'dropdown-second-replace',
),
);
Ajax Forms
• Example of FAPI Ajax:
$form['dropdown_first'] = array(
'#type' => 'select',
'#title' => 'Instrument Type',
'#options' => $options_first,
'#default_value' =>
$form_state['values']['dropdown_first'],
'#ajax' => array(
'callback' =>
'dependent_dropdown_callback',
'wrapper' => 'dropdown-second-replace',
),
);
Ajax Forms
• Example of FAPI Ajax:
$form['dropdown_second'] = array(
'#type' => 'select',
'#title' => $options_first[$selected] . ' ' .
t('Instruments'),
'#prefix' =>
'<div id="dropdown-second-replace">',
'#suffix' => '</div>',
'#options' => _get_second_dropdown_options(
$form_state['values']['dropdown_first']),
'#default_value' =>
$form_state['values']['dropdown_second'],
);
Ajax Forms
• Example of FAPI Ajax:
$form['dropdown_second'] = array(
'#type' => 'select',
'#title' => $options_first[$selected] . ' ' .
t('Instruments'),
'#prefix' =>
'<div id="dropdown-second-replace">',
'#suffix' => '</div>',
'#options' => _get_second_dropdown_options(
$form_state['values']['dropdown_first']),
'#default_value' =>
$form_state['values']['dropdown_second'],
);
Ajax Forms
• Example of FAPI Ajax:
$form['dropdown_second'] = array(
'#type' => 'select',
'#title' => $options_first[$selected] . ' ' .
t('Instruments'),
'#prefix' =>
'<div id="dropdown-second-replace">',
'#suffix' => '</div>',
'#options' => _get_second_dropdown_options(
$form_state['values']['dropdown_first']),
'#default_value' =>
$form_state['values']['dropdown_second'],
);
Ajax Forms
function dependent_dropdown_callback($form, $form_state) {
return $form['dropdown_second'];
}
Ajax Forms
function dependent_dropdown_callback($form, $form_state) {
return $form['dropdown_second'];
}
States
• Provides interactive forms where the actual underlying form doesn't change,
just the presentation to the user.
• You mark a form element that is dependent on another element (opposite of
#ajax)
• You specify the condition and resultant action:
'checked' => array(
':input[name="more_info"]' => array('filled' => TRUE)
),
• The Conditional fields (conditional_fields) module provides an UI for
using this API on fields.
• Find examples in the Drupal Examples for Developers(examples) module.
States
$form['source'] = array(
'#type' => 'checkboxes',
'#options' => drupal_map_assoc(array(
t(‘TV'),
t(‘Newspaper’),
t(‘Internet),
t(‘Other…)
)),
'#title' => t(‘Where did you hear of us?'),
);
States
$form['source'] = array(
'#type' => 'checkboxes',
'#options' => drupal_map_assoc(array(
t(‘TV'),
t(‘Newspaper’),
t(‘Internet),
t(‘Other…)
)),
'#title' => t(‘Where did you hear of us?'),
);
States
$form[‘source_text’] = array(
'#type' => 'textfield',
'#title' => t(‘Write in a few words where you heard of us'),
'#states' => array(
'visible' => array(
':input[name=source]' => array(
'value' => t(‘Other…’)
),
),
),
);
States
$form[‘source_text’] = array(
'#type' => 'textfield',
'#title' => t(‘Write in a few words where you heard of us'),
'#states' => array(
'visible' => array(
':input[name=source]' => array(
'value' => t(‘Other…’)
),
),
),
);
States
$form[‘source_text’] = array(
'#type' => 'textfield',
'#title' => t(‘Write in a few words where you heard of us'),
'#states' => array(
'visible' => array(
':input[name=source]' => array(
'value' => t(‘Other…’)
),
),
),
);
Drupal.theme
• Is the JavaScript counterpart to theme()
• To define a theme function simply create a new public method for the
Drupal.theme class:
Dupal.theme.prototype.displayName = function(name,
url) {
return '<a href="' + url + '">' + name + '</a>';
}
• Then invoke it through Drupal.theme when needed:
var name = "John Doe";
var url = "http://example.com";
var display = Drupal.theme('displayName', name,
url)
Drupal.theme
• Is the JavaScript counterpart to theme()
• To define a theme function simply create a new public method for the
Drupal.theme class:
Dupal.theme.prototype.displayName = function(name,
url) {
return '<a href="' + url + '">' + name + '</a>';
}
• Then invoke it through Drupal.theme when needed:
var name = "John Doe";
var url = "http://example.com";
var display = Drupal.theme('displayName', name,
url)
Drupal.theme
• Is the JavaScript counterpart to theme()
• To define a theme function simply create a new public method for the
Drupal.theme class:
Dupal.theme.prototype.displayName = function(name,
url) {
return '<a href="' + url + '">' + name + '</a>';
}
• Then invoke it through Drupal.theme when needed:
var name = "John Doe";
var url = "http://example.com";
var display = Drupal.theme('displayName', name,
url)
Drupal.theme
• Another developer changes the implementation:
Dupal.theme.prototype.displayName = function(name,
url) {
return ‘<div class=‘username-wrapper”><a href="'
+ url + '">' + name + ‘</a></div>';
}
• All places where the theme function is used will
now display the updated markup.
Drupal.theme
• Allows you to alter the output of components defined by third parties.
• Allows others to alter the output of your components.
• In D7 there is only one theme implementation in core (placeholder).
Multilingual
• Drupal has multilingual support in JS.
• You can wrap your text in Drupal.t(), and it will be available for
translation in the UI. If there is a translation for your text in the current
language, it will be used.
• It only works if the text is explicitly written inside the call to Drupal.t(),
and not if it’s inside a variable.
• Drupal.t(str, args, options)
str - the string to be translated, has to be in English
args - an object of replacement pairs to be made after translation
options - options object, only ‘context’ key is used, defaults to empty
string
Other API’s
• autocomplete
• FAPI property:
'#autocomplete_path' => ‘some/path’,
• the callback function for that path needs to return a JSON with
matches in the format ‘value’ => ‘display_string’
Other API’s
• autocomplete
• FAPI property:
'#autocomplete_path' => ‘some/path’,
• the callback function for that path needs to return a JSON with
matches in the format ‘value’ => ‘display_string’
Other API’s
• autocomplete
• FAPI property:
'#autocomplete_path' => ‘some/path’,
• the callback function for that path needs to return a JSON with
matches in the format ‘value’ => ‘display_string’
Other API’s
• tabledrag - drupal_add_tabledrag()
• Assists in adding the tableDrag JavaScript behavior to a themed
table
• Draggable tables should be used wherever an outline or list of
sortable items needs to be arranged by an end-user. Draggable
tables are very flexible and can manipulate the value of form
elements placed within individual columns.
• not so simple to implement
Other API’s - tabledrag
Drupal 8
• Drupal.settings -> drupalSettings
• Drupal.theme.prototype -> Drupal.theme
• Drupal.ajax.prototype.commands ->
Drupal.AjaxCommands.prototype
Drupal 8
• Only the JavaScript required on a particular page will be added to
that page
• jQuery is not automatically loaded on all pages anymore.
• You have to declare a dependency for your code on jQuery to have
jQuery loaded on the page
Drupal 8 - Defining a library
*.libraries.yml in your module:
cuddly-slider:
version: 1.x
css:
theme:
css/cuddly-slider.css: {}
js:
js/cuddly-slider.js: {}
dependencies:
- core/jquery
Drupal 8 - Attaching a library
to existing content
<?php
function mymodule_element_info_alter(array &$types) {
if (isset($types['table']) {
$types['table']['#attached']['library'][] =
'mymodule/cuddly-slider';
}
}
Drupal 8 - Attaching a library
to new content
<?php
$build[‘your_element’]['#attached']['library'][] =
‘mymodule/cuddly-slider';
Drupal 8 - Attaching a library
to pages
hook_page_attachments(). Example from the Contextual links module
<?php
function contextual_page_attachments(array &$page) {
if (!Drupal::currentUser()
->hasPermission('access contextual links')) {
return;
}
$page['#attached']['library'][] =
'contextual/drupal.contextual-links';
}
Drupal 8 - Attaching settings
$build[‘#attached']['drupalSettings']['mymodule'][‘cuddlySlider']
['foo'] = 'bar';
Drupal 8 - Inline JavaScript
• Inline JavaScript is discouraged.
• It's recommended to put the JS you want to use inline in a file
instead, because that allows that JavaScript to be cached on the
client side.
• It allows JavaScript code to be reviewed and linted
Drupal 8 - Inline JavaScript
that generates markup
• Examples of this are ads, social media sharing buttons, social media
listing widgets.
• Option 1: Put the script inside a custom block.
• Option 2: Put the script inside a twig template.
Drupal 8 - Inline JavaScript that
affects the entire page
• Example: Google Analytics. It should not be front end / styling
• hook_page_attachments() — define attached HTML <HEAD> data
by using the 'html_head' key in the #attached property:
Drupal 8 - Inline JavaScript that
affects the entire page
<?php
function mymodule_page_attachments(array &$page) {
$page['#attached']['html_head'][] = [
// The data.
[
'#tag' => 'script',
'#value' => 'alert("Hello world!");',
],
// A key, to make it possible to recognize this HTML <HEAD>
element when altering.
'hello-world'
];
}
Drupal 8
• Use “Strict Mode”
• "use strict";
• It catches some common coding bloopers, throwing exceptions.
• It prevents, or throws errors, when relatively “unsafe” actions are
taken (such as gaining access to the global object).
• It disables features that are confusing or poorly thought out.
Drupal 8 - Strict Mode
• Variables and Properties
• An attempt to assign foo = "bar"; where ‘foo’ hasn’t been defined
will fail.
• Deleting a variable, a function, or an argument will result in an
error.
• Defining a property more than once in an object literal will cause
an exception to be thrown
Drupal 8 - Strict Mode
eval is evil
Drupal 8
More Libraries
Drupal 8
• jQuery 2
• underscore
• Backbone
• Modernizr
• CKEditor
Drupal 8
Thank you
• calin@dreamproduction.com
• d.o: mariancalinro
• @mariancalinro
• github.com/calin-marian

Mais conteúdo relacionado

Mais procurados

JavaScript in Drupal 7: What developers need to know
JavaScript in Drupal 7: What developers need to knowJavaScript in Drupal 7: What developers need to know
JavaScript in Drupal 7: What developers need to knowkatbailey
 
In-depth changes to Drupal 8 javascript
In-depth changes to Drupal 8 javascriptIn-depth changes to Drupal 8 javascript
In-depth changes to Drupal 8 javascriptThéodore Biadala
 
Drupal & javascript
Drupal & javascriptDrupal & javascript
Drupal & javascriptAlmog Baku
 
Drupal Step-by-Step: How We Built Our Training Site, Part 1
Drupal Step-by-Step: How We Built Our Training Site, Part 1Drupal Step-by-Step: How We Built Our Training Site, Part 1
Drupal Step-by-Step: How We Built Our Training Site, Part 1Acquia
 
Drush - use full power - DrupalCamp Donetsk 2014
Drush - use full power - DrupalCamp Donetsk 2014Drush - use full power - DrupalCamp Donetsk 2014
Drush - use full power - DrupalCamp Donetsk 2014Alex S
 
JQuery In Drupal
JQuery In DrupalJQuery In Drupal
JQuery In Drupalkatbailey
 
How to migrate Cakephp 1.x to 2.x
How to migrate Cakephp 1.x to 2.xHow to migrate Cakephp 1.x to 2.x
How to migrate Cakephp 1.x to 2.xAndolasoft Inc
 
Using RequireJS with CakePHP
Using RequireJS with CakePHPUsing RequireJS with CakePHP
Using RequireJS with CakePHPStephen Young
 
Drupal 7 Theming - Behind the scenes
Drupal 7 Theming - Behind the scenes Drupal 7 Theming - Behind the scenes
Drupal 7 Theming - Behind the scenes ramakesavan
 
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)arcware
 
Introduction to backbone presentation
Introduction to backbone presentationIntroduction to backbone presentation
Introduction to backbone presentationBrian Hogg
 
Learning the basics of the Drupal API
Learning the basics of the Drupal APILearning the basics of the Drupal API
Learning the basics of the Drupal APIAlexandru Badiu
 
【前端Mvc】之豆瓣说实践
【前端Mvc】之豆瓣说实践【前端Mvc】之豆瓣说实践
【前端Mvc】之豆瓣说实践taobao.com
 
Using JavaScript in Drupal
Using JavaScript in DrupalUsing JavaScript in Drupal
Using JavaScript in Drupalkatbailey
 
No Coding Necessary: Building User Macros and Dynamic Reports Inside Confluen...
No Coding Necessary: Building User Macros and Dynamic Reports Inside Confluen...No Coding Necessary: Building User Macros and Dynamic Reports Inside Confluen...
No Coding Necessary: Building User Macros and Dynamic Reports Inside Confluen...Atlassian
 
Becoming A Drupal Master Builder
Becoming A Drupal Master BuilderBecoming A Drupal Master Builder
Becoming A Drupal Master BuilderPhilip Norton
 
JavaScript for Flex Devs
JavaScript for Flex DevsJavaScript for Flex Devs
JavaScript for Flex DevsAaronius
 
No Coding Necessary: Building Confluence User Macros Cheat Sheet - Atlassian ...
No Coding Necessary: Building Confluence User Macros Cheat Sheet - Atlassian ...No Coding Necessary: Building Confluence User Macros Cheat Sheet - Atlassian ...
No Coding Necessary: Building Confluence User Macros Cheat Sheet - Atlassian ...Atlassian
 
Introducing Assetic: Asset Management for PHP 5.3
Introducing Assetic: Asset Management for PHP 5.3Introducing Assetic: Asset Management for PHP 5.3
Introducing Assetic: Asset Management for PHP 5.3Kris Wallsmith
 

Mais procurados (20)

JavaScript in Drupal 7: What developers need to know
JavaScript in Drupal 7: What developers need to knowJavaScript in Drupal 7: What developers need to know
JavaScript in Drupal 7: What developers need to know
 
Backbone
BackboneBackbone
Backbone
 
In-depth changes to Drupal 8 javascript
In-depth changes to Drupal 8 javascriptIn-depth changes to Drupal 8 javascript
In-depth changes to Drupal 8 javascript
 
Drupal & javascript
Drupal & javascriptDrupal & javascript
Drupal & javascript
 
Drupal Step-by-Step: How We Built Our Training Site, Part 1
Drupal Step-by-Step: How We Built Our Training Site, Part 1Drupal Step-by-Step: How We Built Our Training Site, Part 1
Drupal Step-by-Step: How We Built Our Training Site, Part 1
 
Drush - use full power - DrupalCamp Donetsk 2014
Drush - use full power - DrupalCamp Donetsk 2014Drush - use full power - DrupalCamp Donetsk 2014
Drush - use full power - DrupalCamp Donetsk 2014
 
JQuery In Drupal
JQuery In DrupalJQuery In Drupal
JQuery In Drupal
 
How to migrate Cakephp 1.x to 2.x
How to migrate Cakephp 1.x to 2.xHow to migrate Cakephp 1.x to 2.x
How to migrate Cakephp 1.x to 2.x
 
Using RequireJS with CakePHP
Using RequireJS with CakePHPUsing RequireJS with CakePHP
Using RequireJS with CakePHP
 
Drupal 7 Theming - Behind the scenes
Drupal 7 Theming - Behind the scenes Drupal 7 Theming - Behind the scenes
Drupal 7 Theming - Behind the scenes
 
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
 
Introduction to backbone presentation
Introduction to backbone presentationIntroduction to backbone presentation
Introduction to backbone presentation
 
Learning the basics of the Drupal API
Learning the basics of the Drupal APILearning the basics of the Drupal API
Learning the basics of the Drupal API
 
【前端Mvc】之豆瓣说实践
【前端Mvc】之豆瓣说实践【前端Mvc】之豆瓣说实践
【前端Mvc】之豆瓣说实践
 
Using JavaScript in Drupal
Using JavaScript in DrupalUsing JavaScript in Drupal
Using JavaScript in Drupal
 
No Coding Necessary: Building User Macros and Dynamic Reports Inside Confluen...
No Coding Necessary: Building User Macros and Dynamic Reports Inside Confluen...No Coding Necessary: Building User Macros and Dynamic Reports Inside Confluen...
No Coding Necessary: Building User Macros and Dynamic Reports Inside Confluen...
 
Becoming A Drupal Master Builder
Becoming A Drupal Master BuilderBecoming A Drupal Master Builder
Becoming A Drupal Master Builder
 
JavaScript for Flex Devs
JavaScript for Flex DevsJavaScript for Flex Devs
JavaScript for Flex Devs
 
No Coding Necessary: Building Confluence User Macros Cheat Sheet - Atlassian ...
No Coding Necessary: Building Confluence User Macros Cheat Sheet - Atlassian ...No Coding Necessary: Building Confluence User Macros Cheat Sheet - Atlassian ...
No Coding Necessary: Building Confluence User Macros Cheat Sheet - Atlassian ...
 
Introducing Assetic: Asset Management for PHP 5.3
Introducing Assetic: Asset Management for PHP 5.3Introducing Assetic: Asset Management for PHP 5.3
Introducing Assetic: Asset Management for PHP 5.3
 

Semelhante a Drupal Javascript for developers

Tips for writing Javascript for Drupal
Tips for writing Javascript for DrupalTips for writing Javascript for Drupal
Tips for writing Javascript for DrupalSergey Semashko
 
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
jQuery UI Widgets, Drag and Drop, Drupal 7 JavascriptjQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
jQuery UI Widgets, Drag and Drop, Drupal 7 JavascriptDarren Mothersele
 
8 things to know about theming in drupal 8
8 things to know about theming in drupal 88 things to know about theming in drupal 8
8 things to know about theming in drupal 8Logan Farr
 
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)Doris Chen
 
Drupal 8, Where Did the Code Go? From Info Hook to Plugin
Drupal 8, Where Did the Code Go? From Info Hook to PluginDrupal 8, Where Did the Code Go? From Info Hook to Plugin
Drupal 8, Where Did the Code Go? From Info Hook to PluginAcquia
 
D7 theming what's new - London
D7 theming what's new - LondonD7 theming what's new - London
D7 theming what's new - LondonMarek Sotak
 
Drupal Module Development - OSI Days 2010
Drupal Module Development - OSI Days 2010Drupal Module Development - OSI Days 2010
Drupal Module Development - OSI Days 2010Siva Epari
 
Drupal Module Development
Drupal Module DevelopmentDrupal Module Development
Drupal Module Developmentipsitamishra
 
Drupal Theme Development - DrupalCon Chicago 2011
Drupal Theme Development - DrupalCon Chicago 2011Drupal Theme Development - DrupalCon Chicago 2011
Drupal Theme Development - DrupalCon Chicago 2011Ryan Price
 
The new static resources framework
The new static resources frameworkThe new static resources framework
The new static resources frameworkmarcplmer
 
Multilingualism makes better programmers
Multilingualism makes better programmersMultilingualism makes better programmers
Multilingualism makes better programmersAlexander Varwijk
 
Learning jQuery made exciting in an interactive session by one of our team me...
Learning jQuery made exciting in an interactive session by one of our team me...Learning jQuery made exciting in an interactive session by one of our team me...
Learning jQuery made exciting in an interactive session by one of our team me...Thinqloud
 
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD Combination
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD CombinationLotusphere 2012 Speedgeeking - jQuery & Domino, a RAD Combination
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD CombinationSean Burgess
 
JavaScript in Drupal 7: What developers need to know
JavaScript in Drupal 7: What developers need to knowJavaScript in Drupal 7: What developers need to know
JavaScript in Drupal 7: What developers need to knowWork at Play
 
[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVCAlive Kuo
 

Semelhante a Drupal Javascript for developers (20)

Tips for writing Javascript for Drupal
Tips for writing Javascript for DrupalTips for writing Javascript for Drupal
Tips for writing Javascript for Drupal
 
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
jQuery UI Widgets, Drag and Drop, Drupal 7 JavascriptjQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
 
8 things to know about theming in drupal 8
8 things to know about theming in drupal 88 things to know about theming in drupal 8
8 things to know about theming in drupal 8
 
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
 
Drupal 8, Where Did the Code Go? From Info Hook to Plugin
Drupal 8, Where Did the Code Go? From Info Hook to PluginDrupal 8, Where Did the Code Go? From Info Hook to Plugin
Drupal 8, Where Did the Code Go? From Info Hook to Plugin
 
D7 theming what's new - London
D7 theming what's new - LondonD7 theming what's new - London
D7 theming what's new - London
 
Drupal Module Development - OSI Days 2010
Drupal Module Development - OSI Days 2010Drupal Module Development - OSI Days 2010
Drupal Module Development - OSI Days 2010
 
Drupal Module Development
Drupal Module DevelopmentDrupal Module Development
Drupal Module Development
 
Django at the Disco
Django at the DiscoDjango at the Disco
Django at the Disco
 
Django at the Disco
Django at the DiscoDjango at the Disco
Django at the Disco
 
Django at the Disco
Django at the DiscoDjango at the Disco
Django at the Disco
 
Django at the Disco
Django at the DiscoDjango at the Disco
Django at the Disco
 
Drupal Theme Development - DrupalCon Chicago 2011
Drupal Theme Development - DrupalCon Chicago 2011Drupal Theme Development - DrupalCon Chicago 2011
Drupal Theme Development - DrupalCon Chicago 2011
 
The new static resources framework
The new static resources frameworkThe new static resources framework
The new static resources framework
 
Multilingualism makes better programmers
Multilingualism makes better programmersMultilingualism makes better programmers
Multilingualism makes better programmers
 
Learning jQuery made exciting in an interactive session by one of our team me...
Learning jQuery made exciting in an interactive session by one of our team me...Learning jQuery made exciting in an interactive session by one of our team me...
Learning jQuery made exciting in an interactive session by one of our team me...
 
Django at the Disco
Django at the DiscoDjango at the Disco
Django at the Disco
 
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD Combination
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD CombinationLotusphere 2012 Speedgeeking - jQuery & Domino, a RAD Combination
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD Combination
 
JavaScript in Drupal 7: What developers need to know
JavaScript in Drupal 7: What developers need to knowJavaScript in Drupal 7: What developers need to know
JavaScript in Drupal 7: What developers need to know
 
[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC
 

Último

原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查ydyuyu
 
Meaning of On page SEO & its process in detail.
Meaning of On page SEO & its process in detail.Meaning of On page SEO & its process in detail.
Meaning of On page SEO & its process in detail.krishnachandrapal52
 
Indian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi EscortsIndian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi EscortsMonica Sydney
 
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdfMatthew Sinclair
 
Tadepalligudem Escorts Service Girl ^ 9332606886, WhatsApp Anytime Tadepallig...
Tadepalligudem Escorts Service Girl ^ 9332606886, WhatsApp Anytime Tadepallig...Tadepalligudem Escorts Service Girl ^ 9332606886, WhatsApp Anytime Tadepallig...
Tadepalligudem Escorts Service Girl ^ 9332606886, WhatsApp Anytime Tadepallig...meghakumariji156
 
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
20240509 QFM015 Engineering Leadership Reading List April 2024.pdfMatthew Sinclair
 
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...APNIC
 
20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdfMatthew Sinclair
 
Real Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirtReal Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirtrahman018755
 
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdfMatthew Sinclair
 
APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53APNIC
 
Abu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Abu Dhabi Escorts Service 0508644382 Escorts in Abu DhabiAbu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Abu Dhabi Escorts Service 0508644382 Escorts in Abu DhabiMonica Sydney
 
Ballia Escorts Service Girl ^ 9332606886, WhatsApp Anytime Ballia
Ballia Escorts Service Girl ^ 9332606886, WhatsApp Anytime BalliaBallia Escorts Service Girl ^ 9332606886, WhatsApp Anytime Ballia
Ballia Escorts Service Girl ^ 9332606886, WhatsApp Anytime Balliameghakumariji156
 
best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...
best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...
best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...kajalverma014
 
Trump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts SweatshirtTrump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts Sweatshirtrahman018755
 
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样ayvbos
 
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girlsRussian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girlsMonica Sydney
 
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdfpdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdfJOHNBEBONYAP1
 
一比一原版奥兹学院毕业证如何办理
一比一原版奥兹学院毕业证如何办理一比一原版奥兹学院毕业证如何办理
一比一原版奥兹学院毕业证如何办理F
 
Local Call Girls in Seoni 9332606886 HOT & SEXY Models beautiful and charmin...
Local Call Girls in Seoni  9332606886 HOT & SEXY Models beautiful and charmin...Local Call Girls in Seoni  9332606886 HOT & SEXY Models beautiful and charmin...
Local Call Girls in Seoni 9332606886 HOT & SEXY Models beautiful and charmin...kumargunjan9515
 

Último (20)

原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
 
Meaning of On page SEO & its process in detail.
Meaning of On page SEO & its process in detail.Meaning of On page SEO & its process in detail.
Meaning of On page SEO & its process in detail.
 
Indian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi EscortsIndian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
 
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
 
Tadepalligudem Escorts Service Girl ^ 9332606886, WhatsApp Anytime Tadepallig...
Tadepalligudem Escorts Service Girl ^ 9332606886, WhatsApp Anytime Tadepallig...Tadepalligudem Escorts Service Girl ^ 9332606886, WhatsApp Anytime Tadepallig...
Tadepalligudem Escorts Service Girl ^ 9332606886, WhatsApp Anytime Tadepallig...
 
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
 
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
 
20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf
 
Real Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirtReal Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirt
 
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
 
APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53
 
Abu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Abu Dhabi Escorts Service 0508644382 Escorts in Abu DhabiAbu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Abu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
 
Ballia Escorts Service Girl ^ 9332606886, WhatsApp Anytime Ballia
Ballia Escorts Service Girl ^ 9332606886, WhatsApp Anytime BalliaBallia Escorts Service Girl ^ 9332606886, WhatsApp Anytime Ballia
Ballia Escorts Service Girl ^ 9332606886, WhatsApp Anytime Ballia
 
best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...
best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...
best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...
 
Trump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts SweatshirtTrump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts Sweatshirt
 
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
 
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girlsRussian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
 
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdfpdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
 
一比一原版奥兹学院毕业证如何办理
一比一原版奥兹学院毕业证如何办理一比一原版奥兹学院毕业证如何办理
一比一原版奥兹学院毕业证如何办理
 
Local Call Girls in Seoni 9332606886 HOT & SEXY Models beautiful and charmin...
Local Call Girls in Seoni  9332606886 HOT & SEXY Models beautiful and charmin...Local Call Girls in Seoni  9332606886 HOT & SEXY Models beautiful and charmin...
Local Call Girls in Seoni 9332606886 HOT & SEXY Models beautiful and charmin...
 

Drupal Javascript for developers

  • 2. Whoami? • C lin Mariană • Lead developer @ Dream Production • calin@dreamproduction.com • d.o: mariancalinro • @mariancalinro • github.com/calin-marian
  • 3. What will we cover? • Adding JS to the page, both at module and theme level • Writing Drupal aware JS code • Libraries management • Ajax framework • Drupal JS functions • Drupal JS theme functions • If we have enough time … some Drupal 8 changes
  • 4. Adding JS to page • declare it in the .info file of your module scripts[] = path/to/component.js • Using drupal_add_js() drupal_add_js(drupal_get_path(‘module’, ‘mymodule’) . ‘/path/to/component.js’); • attach it to a render array $build[‘myelement’] = array( ‘#theme’ => ‘my_theme’, ‘#myvar’ => $myvar, ‘#attached’ => array( ‘js’ => drupal_get_path(‘module’, ‘mymodule') . ‘/path/to/component.js’ ), );
  • 5. Adding JS to page • declare it in the .info file of your module scripts[] = path/to/component.js • Using drupal_add_js() drupal_add_js(drupal_get_path(‘module’, ‘mymodule') . ‘/path/to/component.js’); • attach it to a render array $build[‘myelement’] = array( ‘#theme’ => ‘my_theme’, ‘#myvar’ => $myvar, ‘#attached’ => array( ‘js’ => drupal_get_path(‘module’, ‘mymodule') . ‘/path/to/component.js’ ), );
  • 6. Adding JS to page • declare it in the .info file of your module scripts[] = path/to/component.js • Using drupal_add_js() drupal_add_js(drupal_get_path(‘module’, ‘mymodule') . ‘/path/to/component.js’); • attach it to a render array $build[‘myelement’] = array( ‘#theme’ => ‘my_theme’, ‘#myvar’ => $myvar, ‘#attached’ => array( ‘js’ => drupal_get_path(‘module’, ‘mymodule') . ‘/path/to/component.js’ ), );
  • 7. Adding JS to page • declare it in the .info file of your module scripts[] = path/to/component.js • Using drupal_add_js() drupal_add_js(drupal_get_path(‘module’, ‘mymodule') . ‘/path/to/component.js’); • attach it to a render array $build[‘myelement’] = array( ‘#theme’ => ‘my_theme’, ‘#myvar’ => $myvar, ‘#attached’ => array( ‘js’ => drupal_get_path(‘module’, ‘mymodule') . ‘/path/to/component.js’ ), );
  • 8. Adding JS to page • for a theme, there are 2 ways to add a JS file to the page: • declare it in the .info file of your theme, same as module scripts[] = path/to/component.js • Using drupal_add_js() from template.php, in the hook_preprocess_html() function
  • 9. Adding JS to page • for a theme, there are 2 ways to add a JS file to the page: • declare it in the .info file of your theme, same as module scripts[] = path/to/component.js • Using drupal_add_js() from template.php, in the hook_preprocess_html() function
  • 10. Adding JS to page • for a theme, there are 2 ways to add a JS file to the page: • declare it in the .info file of your theme, same as module scripts[] = path/to/component.js • Using drupal_add_js() from template.php, in the hook_preprocess_html() function
  • 11. Closures (function ($) { // Code that uses jQuery's $ can follow here. $(‘a’).on(‘click’, function(event){ event.preventDefault(); alert(‘Links are disabled, you are trapped on this page. Hahahaha!!!’); }); var window = "Whoops, at least I only broke my code."; }(jQuery));
  • 12. Closures (function ($) { // Code that uses jQuery's $ can follow here. $(‘a’).on(‘click’, function(event){ event.preventDefault(); alert(‘Links are disabled, you are trapped on this page. Hahahaha!!!’); }); var window = "Whoops, at least I only broke my code."; }(jQuery));
  • 13. Closures (function ($) { // Code that uses jQuery's $ can follow here. $(‘a’).on(‘click’, function(event){ event.preventDefault(); alert(‘Links are disabled, you are trapped on this page. Hahahaha!!!’); }); var window = "Whoops, at least I only broke my code."; }(jQuery));
  • 14. Settings <?php drupal_add_js(array( 'myModule' => array( 'key' => 'value' ) ), 'setting'); ================================== (function($){ console.log(Drupal.settings.myModule.key); // logs 'value' })(jQuery)
  • 15. Settings <?php drupal_add_js(array( 'myModule' => array( 'key' => 'value' ) ), 'setting'); ================================== (function($){ console.log(Drupal.settings.myModule.key); // logs 'value' })(jQuery)
  • 16. Settings <?php drupal_add_js(array( 'myModule' => array( 'key' => 'value' ) ), 'setting'); ================================== (function($){ console.log(Drupal.settings.myModule.key); // logs 'value' })(jQuery)
  • 17. Behaviors • Drupal’s way of dealing with attaching and detaching functionalities to dynamic content. • Invoked by Drupal automatically when content is added or removed to the page by the Ajax framework • Objects in the namespace Drupal.behaviors that have the methods attach and detach - detach is optional if you do not need to run some code when content is removed from page.
  • 18. Behaviors (function($){ Drupal.behaviors.myComponent = { attach: function(context, settings) { // Make your DOM manipulations and attach your // event handlers for your component. }, detach: function(context, settings) { // This is optional, use it if your component needs to destroy // variables to free memory, or do other tasks when the content // your component is attached to is removed from the DOM. } } })(jQuery)
  • 19. Behaviors (function($){ Drupal.behaviors.myComponent = { attach: function(context, settings) { // Make your DOM manipulations and attach your // event handlers for your component. }, detach: function(context, settings) { // This is optional, use it if your component needs to destroy // variables to free memory, or do other tasks when the content // your component is attached to is removed from the DOM. } } })(jQuery)
  • 20. Behaviors (function($){ Drupal.behaviors.myComponent = { attach: function(context, settings) { // Make your DOM manipulations and attach your // event handlers for your component. }, detach: function(context, settings) { // This is optional, use it if your component needs to destroy // variables to free memory, or do other tasks when the content // your component is attached to is removed from the DOM. } } })(jQuery)
  • 21. Behaviors (function($){ Drupal.behaviors.myBxSlider = { attach: function(context, settings) { var options = settings.myBxSlider, Drupal.myBxSlider = $(options.sliderSelector, context) .bxSlider(options.sliderOptions); }, detach: function(context, settings) { delete Drupal.myBxSlider; } } })(jQuery)
  • 22. Behaviors (function($){ Drupal.behaviors.myBxSlider = { attach: function(context, settings) { var options = settings.myBxSlider, Drupal.myBxSlider = $(options.sliderSelector, context) .bxSlider(options.sliderOptions); }, detach: function(context, settings) { delete Drupal.myBxSlider; } } })(jQuery)
  • 23. Behaviors • When adding content to the page, call Drupal.attachBehaviors on the content. This allows other components to attach themselves to the content. The Ajax framework does this for you automatically. • Example: Drupal.attachBehaviors(insertedContent); • Bad example: Drupal.attachBehaviors(); • When run without context, the full page is the context. This means behaviors’ attach methods will run more than once, on the same content.
  • 24. Behaviors • When adding content to the page, call Drupal.attachBehaviors on the content. This allows other components to attach themselves to the content. The Ajax framework does this for you automatically. • Example: Drupal.attachBehaviors(insertedContent); • Bad example: Drupal.attachBehaviors(); • When run without context, the full page is the context. This means behaviors’ attach methods will run more than once, on the same content.
  • 25. Drupal once • To protect yourself against having your code run twice on the same content, Drupal implements a jQuery method, called once: $(selector).once(stringKey, handler) • The handler is only called once, no mater how many times the method is invoked on the same content. • It works by adding a class on the content, and checking for that class when it’s invoked. • Alternative usage: $(selector).once(‘myComponent’).wrap(‘<div class=“myWrapper”></div>’);
  • 26. Drupal once (function($){ Drupal.behaviors.myExample = { attach: function(context, settings) { $(‘a’, context) .once(‘notification’) .click(function(event){ event.preventDefault(); alert(‘Links are disabled, you are trapped on this page. Hahahaha!!!’); }); } } })(jQuery);
  • 27. Drupal once (function($){ Drupal.behaviors.myExample = { attach: function(context, settings) { $(‘a’, context) .once(‘notification’) .click(function(event){ event.preventDefault(); alert(‘Links are disabled, you are trapped on this page. Hahahaha!!!’); }); } } })(jQuery);
  • 28. Drupal once (function($){ Drupal.behaviors.myExample = { attach: function(context, settings) { $(‘a’, context).once(‘notification’, function(){ $(this).click(function(event){ event.preventDefault(); alert(‘Links are disabled, you are trapped on this page. Hahahaha!!!’); }); }); } } })(jQuery);
  • 29. Libraries • Allows you to add all the JavaScript and CSS of one component at once • Example: drupal_add_library('system', 'ui.accordion'); $build['#attached']['library'][] = array('system', ‘ui.accordion'); • hook_library - to define your library • hook_library_alter - to modify libraries provided by other modules
  • 30. Libraries • Allows you to add all the JavaScript and CSS of one component at once • Example: drupal_add_library('system', 'ui.accordion'); $build['#attached']['library'][] = array('system', ‘ui.accordion'); • hook_library - to define your library • hook_library_alter - to modify libraries provided by other modules
  • 31. Libraries • Allows you to add all the JavaScript and CSS of one component at once • Example: drupal_add_library('system', 'ui.accordion'); $build['#attached']['library'][] = array('system', ‘ui.accordion'); • hook_library - to define your library • hook_library_alter - to modify libraries provided by other modules
  • 32. function system_library() { // ... $libraries['ui.accordion'] = array( 'title' => 'jQuery UI: Accordion', 'website' => 'http://jqueryui.com/demos/accordion/', 'version' => '1.7.2', 'js' => array( 'misc/ui/ui.accordion.js' => array(), ), 'css' => array( 'misc/ui/ui.accordion.css' => array(), ), 'dependencies' => array( array('system', 'ui'), ), ); return $libraries; }
  • 33. function system_library() { // ... $libraries['ui.accordion'] = array( 'title' => 'jQuery UI: Accordion', 'website' => 'http://jqueryui.com/demos/accordion/', 'version' => '1.7.2', 'js' => array( 'misc/ui/ui.accordion.js' => array(), ), 'css' => array( 'misc/ui/ui.accordion.css' => array(), ), 'dependencies' => array( array('system', 'ui'), ), ); return $libraries; }
  • 34. function system_library() { // ... $libraries['ui.accordion'] = array( 'title' => 'jQuery UI: Accordion', 'website' => 'http://jqueryui.com/demos/accordion/', 'version' => '1.7.2', 'js' => array( 'misc/ui/ui.accordion.js' => array(), ), 'css' => array( 'misc/ui/ui.accordion.css' => array(), ), 'dependencies' => array( array('system', 'ui'), ), ); return $libraries; }
  • 35. function system_library() { // ... $libraries['ui.accordion'] = array( 'title' => 'jQuery UI: Accordion', 'website' => 'http://jqueryui.com/demos/accordion/', 'version' => '1.7.2', 'js' => array( 'misc/ui/ui.accordion.js' => array(), ), 'css' => array( 'misc/ui/ui.accordion.css' => array(), ), 'dependencies' => array( array('system', 'ui'), ), ); return $libraries; }
  • 36. Ajax Framework • Ajax is a technique that updates content on the page from the server without page refresh. • Drupal’s Ajax framework helps you with this task. • It implements more than a few commands that you can trigger from the response of the Ajax call. • Does some boilerplate code.
  • 37. Ajax Framework • ajax_command_insert() • ajax_command_before() • ajax_command_after() • ajax_command_replace() • ajax_command_invoke() • ajax_command_settings() • For the full list, google “Drupal Ajax framework commands”
  • 38. Ajax Framework • A php array such as... array( 'command' => 'insert', 'method' => 'append', 'selector' => '#my-div', 'data' => '<div>Some new content</div>', ) • ... will turn into this object received on the js side: { command: insert, method: append, selector:#my- div, data: '<div>Some new content</div>' } • ... a clear instruction for ajax.js to follow
  • 39. Ajax Framework • To implement new command in js: Drupal.ajax.prototype.commands.myCommand = function (ajax, response, status) { // in response you have an object with the properties // sent from php, such as myvar, selector, data. } • Then in php, you can return the following array from an Ajax call: array( 'command' => 'myCommand', 'myvar' => 'myvalue', 'selector' => '#my-div', 'data' => '<div>Some new content</div>', )
  • 41. Ajax Framework • You print somewhere in the page this link: array( '#type' => 'link', '#title' => t('Ajax Link'), '#href' => 'my-ajax-test/nojs', '#suffix' => '<div id="ajax-display"></div>', '#ajax' => array( 'effect' => 'fade', ), );
  • 42. Ajax Framework • You print somewhere in the page this link: array( '#type' => 'link', '#title' => t('Ajax Link'), '#href' => 'my-ajax-test/nojs', '#suffix' => '<div id="ajax-display"></div>', '#ajax' => array( 'effect' => 'fade', ), );
  • 43. Ajax Framework • You print somewhere in the page this link: array( '#type' => 'link', '#title' => t('Ajax Link'), '#href' => 'my-ajax-test/nojs', '#suffix' => '<div id="ajax-display"></div>', '#ajax' => array( 'effect' => 'fade', ), );
  • 44. Ajax Framework • Alternative way: array( '#type' => 'link', '#title' => t('Ajax Link'), '#href' => 'my-ajax-test/nojs', '#suffix' => '</div><div id="ajax- display"></div>', '#attributes' => array( 'class' => array(‘use-ajax’), ), );
  • 45. Ajax Framework • You need to define a new menu item: $items['my-ajax-test/%'] = array( 'title' => 'Ajax test callback', 'type' => MENU_CALLBACK, 'page callback' => 'ajax_link_callback', 'page arguments' => array(1), 'access arguments' => array('access content'), );
  • 46. Ajax Framework • You need to define a new menu item: $items['my-ajax-test/%'] = array( 'title' => 'Ajax test callback', 'type' => MENU_CALLBACK, 'page callback' => 'ajax_link_callback', 'page arguments' => array(1), 'access arguments' => array('access content'), );
  • 47. Ajax Framework • Then, define the callback for that menu item: function ajax_link_callback($ajax) { $time = t('The current time is: !time', array('!time' => date('Y-m-d H:i:s'))); if ($ajax == 'ajax') { $commands = array(); $commands[] = ajax_command_replace( '#ajax-display', "<div id='ajax-display'>" . $time . "</div>"); ajax_deliver(array( '#type' => ‘ajax', '#commands' => $commands)); } else { return array('#markup' => $time); } }
  • 48. Ajax Framework • Then, define the callback for that menu item: function ajax_link_callback($ajax) { $time = t('The current time is: !time', array('!time' => date('Y-m-d H:i:s'))); if ($ajax == 'ajax') { $commands = array(); $commands[] = ajax_command_replace( '#ajax-display', "<div id='ajax-display'>" . $time . "</div>"); ajax_deliver(array( '#type' => ‘ajax', '#commands' => $commands)); } else { return array('#markup' => $time); } }
  • 49. Ajax Framework • Then, define the callback for that menu item: function ajax_link_callback($ajax) { $time = t('The current time is: !time', array('!time' => date('Y-m-d H:i:s'))); if ($ajax == 'ajax') { $commands = array(); $commands[] = ajax_command_replace( '#ajax-display', "<div id='ajax-display'>" . $time . "</div>"); ajax_deliver(array( '#type' => ‘ajax', '#commands' => $commands)); } else { return array('#markup' => $time); } }
  • 50. Ajax Framework • Then, define the callback for that menu item: function ajax_link_callback($ajax) { $time = t('The current time is: !time', array('!time' => date('Y-m-d H:i:s'))); if ($ajax == 'ajax') { $commands = array(); $commands[] = ajax_command_replace( '#ajax-display', "<div id='ajax-display'>" . $time . "</div>"); ajax_deliver(array( '#type' => ‘ajax', '#commands' => $commands)); } else { return array('#markup' => $time); } }
  • 51. Ajax Framework • Then, define the callback for that menu item: function ajax_link_callback($ajax) { $time = t('The current time is: !time', array('!time' => date('Y-m-d H:i:s'))); if ($ajax == 'ajax') { $commands = array(); $commands[] = ajax_command_replace( '#ajax-display', "<div id='ajax-display'>" . $time . "</div>"); ajax_deliver(array( '#type' => ‘ajax', '#commands' => $commands)); } else { return array('#markup' => $time); } }
  • 52. Ajax Framework • Then, define the callback for that menu item: function ajax_link_callback($ajax) { $time = t('The current time is: !time', array('!time' => date('Y-m-d H:i:s'))); if ($ajax == 'ajax') { $commands = array(); $commands[] = ajax_command_replace( '#ajax-display', "<div id='ajax-display'>" . $time . "</div>"); ajax_deliver(array( '#type' => ‘ajax', '#commands' => $commands)); } else { return array('#markup' => $time); } }
  • 53. Ajax Forms • Ajax forms in Drupal are provided by the Form API. • You write the form using the Drupal Form API. • You do not write or see any Javascript.
  • 54. Ajax Forms • Specifiy what form element activates the Ajax behavior by adding #ajax to it. • Specify what part of the form’s HTML is to be replaced by the callback using the ‘wrapper’ attribute. • Provide a callback function that receives the whole rebuild form, and returns the piece of form that will replace the wrapper content. • Your form builder needs to take into account $form_state[‘values’] when building the form. • Find examples in the Drupal Examples for Developers(examples) module.
  • 55. Ajax Forms • Example of FAPI Ajax: $form['dropdown_first'] = array( '#type' => 'select', '#title' => 'Instrument Type', '#options' => $options_first, '#default_value' => $form_state['values']['dropdown_first'], '#ajax' => array( 'callback' => 'dependent_dropdown_callback', 'wrapper' => 'dropdown-second-replace', ), );
  • 56. Ajax Forms • Example of FAPI Ajax: $form['dropdown_first'] = array( '#type' => 'select', '#title' => 'Instrument Type', '#options' => $options_first, '#default_value' => $form_state['values']['dropdown_first'], '#ajax' => array( 'callback' => 'dependent_dropdown_callback', 'wrapper' => 'dropdown-second-replace', ), );
  • 57. Ajax Forms • Example of FAPI Ajax: $form['dropdown_second'] = array( '#type' => 'select', '#title' => $options_first[$selected] . ' ' . t('Instruments'), '#prefix' => '<div id="dropdown-second-replace">', '#suffix' => '</div>', '#options' => _get_second_dropdown_options( $form_state['values']['dropdown_first']), '#default_value' => $form_state['values']['dropdown_second'], );
  • 58. Ajax Forms • Example of FAPI Ajax: $form['dropdown_second'] = array( '#type' => 'select', '#title' => $options_first[$selected] . ' ' . t('Instruments'), '#prefix' => '<div id="dropdown-second-replace">', '#suffix' => '</div>', '#options' => _get_second_dropdown_options( $form_state['values']['dropdown_first']), '#default_value' => $form_state['values']['dropdown_second'], );
  • 59. Ajax Forms • Example of FAPI Ajax: $form['dropdown_second'] = array( '#type' => 'select', '#title' => $options_first[$selected] . ' ' . t('Instruments'), '#prefix' => '<div id="dropdown-second-replace">', '#suffix' => '</div>', '#options' => _get_second_dropdown_options( $form_state['values']['dropdown_first']), '#default_value' => $form_state['values']['dropdown_second'], );
  • 60. Ajax Forms function dependent_dropdown_callback($form, $form_state) { return $form['dropdown_second']; }
  • 61. Ajax Forms function dependent_dropdown_callback($form, $form_state) { return $form['dropdown_second']; }
  • 62. States • Provides interactive forms where the actual underlying form doesn't change, just the presentation to the user. • You mark a form element that is dependent on another element (opposite of #ajax) • You specify the condition and resultant action: 'checked' => array( ':input[name="more_info"]' => array('filled' => TRUE) ), • The Conditional fields (conditional_fields) module provides an UI for using this API on fields. • Find examples in the Drupal Examples for Developers(examples) module.
  • 63. States $form['source'] = array( '#type' => 'checkboxes', '#options' => drupal_map_assoc(array( t(‘TV'), t(‘Newspaper’), t(‘Internet), t(‘Other…) )), '#title' => t(‘Where did you hear of us?'), );
  • 64. States $form['source'] = array( '#type' => 'checkboxes', '#options' => drupal_map_assoc(array( t(‘TV'), t(‘Newspaper’), t(‘Internet), t(‘Other…) )), '#title' => t(‘Where did you hear of us?'), );
  • 65. States $form[‘source_text’] = array( '#type' => 'textfield', '#title' => t(‘Write in a few words where you heard of us'), '#states' => array( 'visible' => array( ':input[name=source]' => array( 'value' => t(‘Other…’) ), ), ), );
  • 66. States $form[‘source_text’] = array( '#type' => 'textfield', '#title' => t(‘Write in a few words where you heard of us'), '#states' => array( 'visible' => array( ':input[name=source]' => array( 'value' => t(‘Other…’) ), ), ), );
  • 67. States $form[‘source_text’] = array( '#type' => 'textfield', '#title' => t(‘Write in a few words where you heard of us'), '#states' => array( 'visible' => array( ':input[name=source]' => array( 'value' => t(‘Other…’) ), ), ), );
  • 68. Drupal.theme • Is the JavaScript counterpart to theme() • To define a theme function simply create a new public method for the Drupal.theme class: Dupal.theme.prototype.displayName = function(name, url) { return '<a href="' + url + '">' + name + '</a>'; } • Then invoke it through Drupal.theme when needed: var name = "John Doe"; var url = "http://example.com"; var display = Drupal.theme('displayName', name, url)
  • 69. Drupal.theme • Is the JavaScript counterpart to theme() • To define a theme function simply create a new public method for the Drupal.theme class: Dupal.theme.prototype.displayName = function(name, url) { return '<a href="' + url + '">' + name + '</a>'; } • Then invoke it through Drupal.theme when needed: var name = "John Doe"; var url = "http://example.com"; var display = Drupal.theme('displayName', name, url)
  • 70. Drupal.theme • Is the JavaScript counterpart to theme() • To define a theme function simply create a new public method for the Drupal.theme class: Dupal.theme.prototype.displayName = function(name, url) { return '<a href="' + url + '">' + name + '</a>'; } • Then invoke it through Drupal.theme when needed: var name = "John Doe"; var url = "http://example.com"; var display = Drupal.theme('displayName', name, url)
  • 71. Drupal.theme • Another developer changes the implementation: Dupal.theme.prototype.displayName = function(name, url) { return ‘<div class=‘username-wrapper”><a href="' + url + '">' + name + ‘</a></div>'; } • All places where the theme function is used will now display the updated markup.
  • 72. Drupal.theme • Allows you to alter the output of components defined by third parties. • Allows others to alter the output of your components. • In D7 there is only one theme implementation in core (placeholder).
  • 73. Multilingual • Drupal has multilingual support in JS. • You can wrap your text in Drupal.t(), and it will be available for translation in the UI. If there is a translation for your text in the current language, it will be used. • It only works if the text is explicitly written inside the call to Drupal.t(), and not if it’s inside a variable. • Drupal.t(str, args, options) str - the string to be translated, has to be in English args - an object of replacement pairs to be made after translation options - options object, only ‘context’ key is used, defaults to empty string
  • 74. Other API’s • autocomplete • FAPI property: '#autocomplete_path' => ‘some/path’, • the callback function for that path needs to return a JSON with matches in the format ‘value’ => ‘display_string’
  • 75. Other API’s • autocomplete • FAPI property: '#autocomplete_path' => ‘some/path’, • the callback function for that path needs to return a JSON with matches in the format ‘value’ => ‘display_string’
  • 76. Other API’s • autocomplete • FAPI property: '#autocomplete_path' => ‘some/path’, • the callback function for that path needs to return a JSON with matches in the format ‘value’ => ‘display_string’
  • 77. Other API’s • tabledrag - drupal_add_tabledrag() • Assists in adding the tableDrag JavaScript behavior to a themed table • Draggable tables should be used wherever an outline or list of sortable items needs to be arranged by an end-user. Draggable tables are very flexible and can manipulate the value of form elements placed within individual columns. • not so simple to implement
  • 78. Other API’s - tabledrag
  • 79. Drupal 8 • Drupal.settings -> drupalSettings • Drupal.theme.prototype -> Drupal.theme • Drupal.ajax.prototype.commands -> Drupal.AjaxCommands.prototype
  • 80. Drupal 8 • Only the JavaScript required on a particular page will be added to that page • jQuery is not automatically loaded on all pages anymore. • You have to declare a dependency for your code on jQuery to have jQuery loaded on the page
  • 81. Drupal 8 - Defining a library *.libraries.yml in your module: cuddly-slider: version: 1.x css: theme: css/cuddly-slider.css: {} js: js/cuddly-slider.js: {} dependencies: - core/jquery
  • 82. Drupal 8 - Attaching a library to existing content <?php function mymodule_element_info_alter(array &$types) { if (isset($types['table']) { $types['table']['#attached']['library'][] = 'mymodule/cuddly-slider'; } }
  • 83. Drupal 8 - Attaching a library to new content <?php $build[‘your_element’]['#attached']['library'][] = ‘mymodule/cuddly-slider';
  • 84. Drupal 8 - Attaching a library to pages hook_page_attachments(). Example from the Contextual links module <?php function contextual_page_attachments(array &$page) { if (!Drupal::currentUser() ->hasPermission('access contextual links')) { return; } $page['#attached']['library'][] = 'contextual/drupal.contextual-links'; }
  • 85. Drupal 8 - Attaching settings $build[‘#attached']['drupalSettings']['mymodule'][‘cuddlySlider'] ['foo'] = 'bar';
  • 86. Drupal 8 - Inline JavaScript • Inline JavaScript is discouraged. • It's recommended to put the JS you want to use inline in a file instead, because that allows that JavaScript to be cached on the client side. • It allows JavaScript code to be reviewed and linted
  • 87. Drupal 8 - Inline JavaScript that generates markup • Examples of this are ads, social media sharing buttons, social media listing widgets. • Option 1: Put the script inside a custom block. • Option 2: Put the script inside a twig template.
  • 88. Drupal 8 - Inline JavaScript that affects the entire page • Example: Google Analytics. It should not be front end / styling • hook_page_attachments() — define attached HTML <HEAD> data by using the 'html_head' key in the #attached property:
  • 89. Drupal 8 - Inline JavaScript that affects the entire page <?php function mymodule_page_attachments(array &$page) { $page['#attached']['html_head'][] = [ // The data. [ '#tag' => 'script', '#value' => 'alert("Hello world!");', ], // A key, to make it possible to recognize this HTML <HEAD> element when altering. 'hello-world' ]; }
  • 90. Drupal 8 • Use “Strict Mode” • "use strict"; • It catches some common coding bloopers, throwing exceptions. • It prevents, or throws errors, when relatively “unsafe” actions are taken (such as gaining access to the global object). • It disables features that are confusing or poorly thought out.
  • 91. Drupal 8 - Strict Mode • Variables and Properties • An attempt to assign foo = "bar"; where ‘foo’ hasn’t been defined will fail. • Deleting a variable, a function, or an argument will result in an error. • Defining a property more than once in an object literal will cause an exception to be thrown
  • 92. Drupal 8 - Strict Mode eval is evil
  • 94. Drupal 8 • jQuery 2 • underscore • Backbone • Modernizr • CKEditor
  • 96. Thank you • calin@dreamproduction.com • d.o: mariancalinro • @mariancalinro • github.com/calin-marian

Notas do Editor

  1. We are a small company in Timisoara, 15 people We specialize in custom Drupal and Wordpress solutions I am leading the Drupal team
  2. backend developer / front end developer who develops in JS in the day to day live who has developed in JS for Drupal who has used the Drupal Ajax framework D7 accent/D8 accent
  3. js is added to html in the process phase
  4. It&amp;apos;s best practice to wrap your code in a closure, a closure is an anonymous function variables defined in a closure can’t accidentally overwrite global variables allows us to use $ for jQuery, even if jQuery.noConflict() is called, if we pass the jQuery object as a parameter
  5. It&amp;apos;s best practice to wrap your code in a closure, a closure is an anonymous function variables defined in a closure can’t accidentally overwrite global variables allows us to use $ for jQuery, even if jQuery.noConflict() is called, if we pass the jQuery object as a parameter
  6. It&amp;apos;s best practice to wrap your code in a closure, a closure is an anonymous function variables defined in a closure can’t accidentally overwrite global variables allows us to use $ for jQuery, even if jQuery.noConflict() is called, if we pass the jQuery object as a parameter
  7. A way of passing information from your PHP to your JS, e.g. module configuration settings (e.g - google maps api key)
  8. A way of passing information from your PHP to your JS, e.g. module configuration settings (e.g - google maps api key)
  9. A way of passing information from your PHP to your JS, e.g. module configuration settings (e.g - google maps api key)
  10. What happens if Drupal.attachBehaviors is run more than once?
  11. What happens if Drupal.attachBehaviors is run more than once?
  12. Some meta information about the library
  13. The assets
  14. and the dependencies
  15. What can we do with it?
  16. Example with checkboxes and facetapi.
  17. Virtually any attempt to use the name ‘eval’ is prohibited – as is the ability to assign the eval function to a variable or a property of an object. Attempting to overwrite the arguments object will result in an error. Defining identically-named arguments will result in an error. with(){} statements are dead when strict mode is enabled - in fact it even appears as a syntax error.
  18. jQuery2 - drupal.org/project/ie8 backbone - toolbar, edit
  19. jQuery2 - drupal.org/project/ie8 backbone - toolbar, edit