Top Banner
Upgrade your javascript to Drupal 8
35

Upgrade your javascript to drupal 8

May 10, 2015

Download

Technology

Drupal 8 introduces a lot of changes for javascript: how to add a JS file to a page, how Drupal process the aggregation of JS files, what are the new javascript APIs, what do we mean by mobile-first.

This session will focus on showing Drupal developers how to update their javascript from Drupal 7 to 8 by taking an example module and going through all the relevant changes.

By the end you'll know what fancy new features Drupal 8 provides you to build rich applications on top of Drupal while keeping mobile devices in mind.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Upgrade your javascript to drupal 8

Upgrade your javascript to Drupal 8

Page 2: Upgrade your javascript to drupal 8

Upgrade your JavaScriptto Drupal 8

Théodore 'nod_' Biadala

JS Maintainer for Drupal coreTechnical consultant @ Acquia

DrupalCon, Prague 2013

Page 3: Upgrade your javascript to drupal 8

Few things before starting

Who can write a custom module?

Who knows about render array?

Who knows about #attached?

Who knows about hook_library()?

Page 4: Upgrade your javascript to drupal 8

TL ; DR

Page 5: Upgrade your javascript to drupal 8

Javascript changes(search & replace)

Drupal.settings

↓drupalSettings

Page 6: Upgrade your javascript to drupal 8

Javascript changes(search & replace)

Drupal.theme.prototype

↓Drupal.theme

Page 7: Upgrade your javascript to drupal 8

Javascript changes(search & replace)

Drupal.ajax.prototype.commands

↓Drupal.AjaxCommands.prototype

Page 8: Upgrade your javascript to drupal 8

PHP changes

drupal_add_js()

scripts[] =

Page 9: Upgrade your javascript to drupal 8

PHP changes

drupal_add_js()

scripts[] =DON'T

Page 10: Upgrade your javascript to drupal 8

PHP changes

#attached

hook_library_info()DO

Page 11: Upgrade your javascript to drupal 8

hook_library_info ?

$libraries['myscript'] = array(

'js' => array('script.js'),

'dependencies' => array(

array('system', 'jquery'),

array('system', 'drupal'),

array('system', 'drupalSettings'),

array('system', 'jquery.once'),

));

Page 12: Upgrade your javascript to drupal 8

hook_library_info ?

$libraries['myscript'] = array(

'js' => array('script.js'),

'dependencies' => array(

array('system', 'jquery'),

array('system', 'drupal'),

array('system', 'drupalSettings'),

array('system', 'jquery.once'),

));

Page 13: Upgrade your javascript to drupal 8

hook_library_info ?

$libraries['myscript'] = array(

'js' => array('script.js'),

'dependencies' => array(

array('system', 'jquery'),

array('system', 'drupal'),

array('system', 'drupalSettings'),

array('system', 'jquery.once'),

));

Page 14: Upgrade your javascript to drupal 8

hook_library_info ?

$libraries['myscript'] = array(

'js' => array('script.js'),

'dependencies' => array(

array('system', 'jquery'),

array('system', 'drupal'),

array('system', 'drupalSettings'),

array('system', 'jquery.once'),

));

Page 15: Upgrade your javascript to drupal 8

hook_library_info ?

$libraries['myscript'] = array(

'js' => array('script.js'),

'dependencies' => array(

array('system', 'jquery'),

array('system', 'drupal'),

array('system', 'drupalSettings'),

array('system', 'jquery.once'),

));

Page 16: Upgrade your javascript to drupal 8

Example

Page 17: Upgrade your javascript to drupal 8

BEFORE

function my_page() {

drupal_add_ js($module_path . '/script.js');

return array(

'#theme' => 'item_list',

'#items' => array('one', 'two', 'three'),

);

}

Page 18: Upgrade your javascript to drupal 8

AFTERfunction my_module_library_info () {

$libraries['myscript'] = array(

'js' => array($module_path . '/script.js'),

'dependencies' => array( /* … */ ) );

return $libraries;

}

function my_page() {

return array( '#theme' => 'item_list',

'#items' => array('one', 'two', 'three'),

'#attach' => array( 'library' => array( array('my_module', 'myscript') ) );

}

Page 19: Upgrade your javascript to drupal 8

Maybe if patch #1996238 gets in

my_module.library.yml:

myscript:

js:

- { file: script.js }

dependencies:

- system/jquery

- system/drupal

- system/drupalSettings

- system/jquery.once

my_module.module:

function my_page() {

return array(

'#theme' => 'item_list',

'#items' => array( /* … */ ),

'#attach' => array(

'library' => array(

'my_module/myscript',

) ) );

}

Page 20: Upgrade your javascript to drupal 8

DONE !

Page 21: Upgrade your javascript to drupal 8

Why ?

Page 22: Upgrade your javascript to drupal 8

Drupal 7 problems

Page 23: Upgrade your javascript to drupal 8

D7 problems

jQuery 1.4.4

jQuery + drupal.js on all pages

Core JS breaks easily

Contrib JS is not great

Page 24: Upgrade your javascript to drupal 8

Drupal 8 Solutions

Page 25: Upgrade your javascript to drupal 8

D8 solutions

Update all third party JS libraries

Declare all script dependencies

Strict mode & JSHint

Coding standards

Page 26: Upgrade your javascript to drupal 8

D8 solutions

Update all third party JS libraries

Declare all script dependenciesStrict mode & JSHint

Coding standards

Page 27: Upgrade your javascript to drupal 8

New

Page 28: Upgrade your javascript to drupal 8

New libraries

jQuery 2

Underscore

Backbone

Modernizr

CKEditor

Joyride

Page 29: Upgrade your javascript to drupal 8

New APIs

Drupal.announce(text, priority)

Drupal.displace(broadcast)

Drupal.debounce(func, wait)

Drupal.dialog(element, options)

Page 30: Upgrade your javascript to drupal 8

New Features

Responsive tables

Responsive images

Quick edit

Many, many more…

Page 31: Upgrade your javascript to drupal 8

Wait… jQuery 2?

Page 32: Upgrade your javascript to drupal 8

drupal.org/project/ie8

Page 33: Upgrade your javascript to drupal 8
Page 34: Upgrade your javascript to drupal 8
Page 35: Upgrade your javascript to drupal 8

Questions !

Théodore BIADALA

@nod_

[email protected]

Take the survey:

prague2013.drupal.org/session/upgrading-your-js-drupal-8

Pics found on: wtfevolution.tumblr.com