SlideShare a Scribd company logo
1 of 33
Download to read offline
Demystifying Ajax
Callback Commands
(in Drupal 8)
Mike Miles
Genuine ( )wearegenuine.com
All the internet places: mikemiles86
Defining Callback Commands
Instructions built by the Server and 
executed by the Client in an Ajax event.
Callback Command: JavaScript
Attached to 'Drupal.AjaxCommands.prototype'
Defined in 'misc/ajax.js'
Accepts 3 arguments:
ajax
response
status
Wrapper for additional JavaScript
Callback Structure: JavaScript
01 (function ($, window, Drupal, drupalSettings) {
02 'use strict';
03 /**
04 * [commandName description]
05 *
06 * @param {Drupal.Ajax} [ajax]
07 * @param {object} response
08 * @param {number} [status]
09 */
10 Drupal.AjaxCommands.prototype.[commandName] = function(ajax, response, status){
11
12 // Custom javascript goes here ...
13
14 }
15
16 })(jQuery, this, Drupal, drupalSettings);
[module]/js/[javascript].js
Example of the structure for the JavaScript half of a callback command as defined in a module.
Callback Command: PHP
Class that implements CommandInterface
Defines a method called 'render'
Returns an associative array:
Must have element with key of 'command'
Value must be name of JavaScript function
Other elements passed as response data
Callback Structure: PHP
01 namespace Drupal[module]Ajax
02 use DrupalCoreAjaxCommandInterface;
03
04 // An Ajax command for calling [commandName]() JavaScript method.
05 class [CommandName]Command implements CommandInterface {
06
07 // Implements DrupalCoreAjaxCommandInterface:render().
08 public function render() {
09 return array(
10 'command' => '[commandName]', // Name of JavaScript Method.
11 // other response arguments...
12 );
13 }
14 }
[module]/src/Ajax/[CommandName]Command.php
Example of the structure for the PHP half of a callback command as defined in a module.
Core Example: Remove
01 Drupal.AjaxCommands.prototype = {
02 // ...
03 /**
04 * Command to remove a chunk from the page.
05 *
06 * @param {Drupal.Ajax} [ajax]
07 * @param {object} response
08 * @param {string} response.selector
09 * @param {object} [response.settings]
10 * @param {number} [status]
11 */
12 remove: function (ajax, response, status) {
13 var settings = response.settings || ajax.settings || drupalSettings;
14 $(response.selector).each(function () {
15 Drupal.detachBehaviors(this, settings);
16 })
17 .remove();
18 },
19 //...
misc/ajax.js
The JavaScript function for the core 'remove' callback command. It is basically a wrapper for the jQuery
'remove' method.
Core Example: RemoveCommand
01 namespace DrupalCoreAjax;
02 use DrupalCoreAjaxCommandInterface;
03 /**
04 * Ajax command for calling the jQuery remove() method.
05 * ...
06 */
07 class RemoveCommand Implements CommandInterface {
08 // ...
09 /**
10 * Implements DrupalCoreAjaxCommandInterface:render().
11 */
12 public function render() {
13 return array(
14 'command' => 'remove',
15 'selector' => $this->selector,
16 );
17 }
18 }
core/lib/Drupal/Core/Ajax/RemoveCommand.php
The PHP class for the core 'remove' callback command. Implements CommandInterface, so it must
define the method 'render' that returns an associative array.
PHP
01 //...
02 public function render() {
03 return array(
04 'command' => 'remove',
05 'selector' => $this->selector,
06 );
07 }
core/lib/Drupal/Core/Ajax/RemoveCommand.php
JavaScript
01 //...
02 remove: function (ajax, response, status) {
03 var settings = response.settings || ajax.settings || drupalSettings;
04 $(response.selector).each(function () {
05 Drupal.detachBehaviors(this, settings);
06 })
07 .remove();
08 },
misc/ajax.js
Can see how the two halfs are tied together. Value on line #4 of PHP matches JavaScript function name
defined on line #2 in JavaScript. Passed CSS selector on line #5 in PHP is used on line #4 in JavaScript.
Callback Commands
Used in all Ajax requests
Composed of two parts: JavaScript function, PHP Class
Provided by core and modules
Creating Callback Commands
Example Scenario
Create a callback command for the jQuery 'SlideDown' animation
Create a Module
01 name: 'Slide Down Command'
02 type: module
03 description: Provides an Ajax Callback command for the jQuery SlideDown method.
04 package: other
05 core: 8.x
slide_down/slide_down.info.yml
Custom Ajax callback commands must be defined in a module.
Create JavaScript Function
01 (function ($, window, Drupal, drupalSettings) {
02
03 'use strict';
04
05 // Command to Slide Down page elements.
06 Drupal.AjaxCommands.prototype.slideDown = function(ajax, response, status){
07 // Get duration if sent, else use default of slow.
08 var duration = response.duration ? response.duration : "slow";
09 // slide down the selected element(s).
10 $(response.selector).slideDown(duration);
11 }
12 })(jQuery, this, Drupal, drupalSettings);
slide_down/js/slidedown-command.js
Attach a JavaScript function to the AjaxCommands object provided by the Ajax Framework. Accepts the
three arguments and is a wrapper for the jQuery method.
Create Asset Library
01 slidedown:
02 version: VERSION
03 js:
04 js/slidedown-command.js; {}
05 dependencies:
06 - core/drupal.ajax
slide_down/slide_down.libraries.yml
In Drupal 8 custom JavaScript files must be added to an asset library to be able to be included on a
page.
Create PHP Class
01 namespace Drupalslide_downAjax;
02 use DrupalCoreAjaxCommandInterface;
03
04 class SlideDownCommand implements CommandInterface {
05 // ...
06 // Constructs an SlideDownCommand object.
07 public function __construct($selector, $duration = NULL) {
08 $this->selector = $selector;
09 $this->duration = $duration;
10 }
11
12 // Implements DrupalCoreAjaxCommandInterface:render().
13 public function render() {
14 return array(
15 'command' => 'slideDown',
16 'method' => NULL,
17 'selector' => $this->selector,
18 'duration' => $this->duration,
19 );
20 }
21 }
slide_down/src/Ajax/SlideDownCommand.php
Create a PHP class that implements CommandInterface. Must define a 'render' method and return an
associative array. In the array, pass the element with key of 'command' and value being the name of the
JavaScript function and any repsonse data.
To Create a Callback Command:
Create a module
Attach JavaScript function to 'Drupal.AjaxCommands.prototype'
Define an asset library
Create PHP class that implements 'CommandInterface'
Using Callback Commands
Example Scenario
Load watchdog log message details onto the overview page using
Ajax commands.
Add Ajax Library to Page
01 use DrupaldblogControllerDbLogController as ControllerBase;
02
03 class DbLogController extends ControllerBase {
04 // Override overview() method.
05 public function overview() {
06 $build = parent::overview();
07 // ...
08 // Add custom library.
09 $build['#attached']['library'][] = 'ajax_dblog/ajax-dblog';
10 return $build;
11 }
12 // ...
13 }
ajax_dblog/src/Controller/DbLogController.php
Need to attach custom library onto page so that custom JavaScript and Ajax Framework is included.
01 ajax-dblog:
02 version: VERSION
03 css:
04 component:
05 css/ajax_dblog.module.css: {}
06 js:
07 js/behaviors.js: {}
08 dependencies:
09 - slide_down/slidedown
ajax_dblog/ajax_dblog.libraries.yml
01 slidedown:
02 version: VERSION
03 js:
04 js/slidedown-command.js: {}
05 dependencies:
06 - core/drupal.ajax
slide_down/slide_down.libraries.yml
Defining a dependency in the library on another library. The other library depends on the Ajax
Framework. Drupal will follow chain to include all depended JavaScript files.
Add Ajax to Elements
01 namespace Drupalajax_dblogController;
02 use DrupaldblogControllerDbLogController as ControllerBase;
03
04 class DbLogController extends ControllerBase {
05 // Override overview() method.
06 public function overview() {
07 $build = parent::overview();
08 // Alter the links for each log message.
09 foreach ($build['dblog_table']['#rows'] as &$row) {
10 // ...
11 // Build route parameters.
12 $params = array(
13 'method' => 'nojs',
14 //...
15 );
16 // Build link options.
17 $ops = array( 'attributes' => array(
18 'class' => array('use-ajax', 'dblog-event-link'),
19 ));
20 // Replace with a new link.
21 $row['data'][3] = Link::createFromRoute($txt,'ajax_dblog.event',$params,$ops);
22 }
23 return $build; ajax_dblogs/src/Controller/DbLogController.php
Need to have elements that will trigger an Ajax request. Rebuilding links on page to point to new route
(line #21). Links will have the class 'use­ajax' (line #18), which the Ajax Framework will look for.
Page still renders the same. However now includes Ajax Framework including the custom SlideDown
command. The message title links will now trigger an ajax request.
Create Ajax Request Endpoint
01 ajax_dblog.event:
02 path: '/admin/reports/dblog/{method}/event/{event_id}'
03 defaults:
04 _controller: 'Drupalajax_dblogControllerDbLogController::ajaxEventDetails'
05 requirements:
06 _permission: 'access site reports'
07 method: 'nojs|ajax'
ajax_dblog/ajax_dblog.routing.yml
/admin/reports/dblog/nojs/event/123
/admin/reports/dblog/ajax/event/123
Create an endpoint that will handle Ajax Requests. The ajax framework will replace 'nojs' with 'ajax' on
all request. Can use as a check to handle graceful degradation.
Return an AjaxResponse of Callback Commands
01 use DrupalCoreAjaxAjaxResponse;
02 use DrupalCoreAjaxAfterCommand;
03 use DrupalCoreAjaxRemoveCommand;
04 use Drupalslide_downAjaxSlideDownCommand;
05
06 class DbLogController extends ControllerBase {
07 // ...
08 public function ajaxEventDetails($method, $event_id) {
09 //...
10 if ($method == 'ajax') {
11 $event = parent::eventDetails($event_id);
12 $event_details = [ ... ];
13 // Create an AjaxResponse.
14 $response = new AjaxResponse();
15 // Remove old event details.
16 $response->addCommand(new RemoveCommand('.dblog-event-row'));
17 // Insert event details after event.
18 $response->addCommand(new AfterCommand('#dblog-event-' . $event_id, $event_details
19 // SlideDown event details.
20 $response->addCommand(new SlideDownCommand('#dblog-event-details-' . $event_id
21 }
22 // ...
23 } ajax_dblog/src/Controller/DbLogController.php
Have a method that is the endpoint for the Ajax request (line #8). Need to build an AjaxReponse object
(line #14). Will add commands to this response using the 'addCommand' method and creating a new
instance of the relevant Callback Command class (lines #16, #18, #20).
When a message title is clicked, the Ajax request is made. The endpoint builds an AjaxResponse of
commands and Drupal returns a JSON string.
Ajax Response
01 [
02 {
03 "command":"remove",
04 "selector":".dblog-event-row"
05 },
06 {
07 "command":"insert",
08 "method":"after",
09 "selector":"#dblog-event-32",
10 "data":"...",
11 "settings":null
12 },
13 {
14 "command":"slideDown",
15 "method":null,
16 "selector":"#dblog-event-details-32",
17 "duration":null
18 }
19 ]
The returned JSON array is parsed by Ajax Framework. Finds JavaScript function to execute and the
passes the object as the data for the response argument of the function.
To Use Callback Commands
Include the Ajax library and commands on the page.
Have endpoint that returns an AjaxResponse
Add commands to response using 'addCommand'
Resources
Drupal 8 Ajax Framework: bit.ly/Drupal8Ajax
This Presentation: bit.ly/D8DAjax
Presentation Slides: bit.ly/D8DAjaxSlides
Ajax Dblog: bit.ly/AjaxDblog
Creating Commands in D8: bit.ly/D8AjaxCmds
Feedback
@mikemiles86
 #Drupal8Day  @WeAreGenuine D8 Ajax Commands /  Michael Miles
Thank You!

More Related Content

What's hot

#34.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자교육,국...
#34.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자교육,국...#34.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자교육,국...
#34.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자교육,국...탑크리에듀(구로디지털단지역3번출구 2분거리)
 
Magento 2 | Declarative schema
Magento 2 | Declarative schemaMagento 2 | Declarative schema
Magento 2 | Declarative schemaKiel Pykett
 
Catalyst patterns-yapc-eu-2016
Catalyst patterns-yapc-eu-2016Catalyst patterns-yapc-eu-2016
Catalyst patterns-yapc-eu-2016John Napiorkowski
 
Upgrade your javascript to drupal 8
Upgrade your javascript to drupal 8Upgrade your javascript to drupal 8
Upgrade your javascript to drupal 8Théodore Biadala
 
Анатолий Поляков - Drupal.ajax framework from a to z
Анатолий Поляков - Drupal.ajax framework from a to zАнатолий Поляков - Drupal.ajax framework from a to z
Анатолий Поляков - Drupal.ajax framework from a to zLEDC 2016
 
First Steps in Drupal Code Driven Development
First Steps in Drupal Code Driven DevelopmentFirst Steps in Drupal Code Driven Development
First Steps in Drupal Code Driven DevelopmentNuvole
 
Planbox Backbone MVC
Planbox Backbone MVCPlanbox Backbone MVC
Planbox Backbone MVCAcquisio
 
IndexedDB - Querying and Performance
IndexedDB - Querying and PerformanceIndexedDB - Querying and Performance
IndexedDB - Querying and PerformanceParashuram N
 
td_mxc_rubyrails_shin
td_mxc_rubyrails_shintd_mxc_rubyrails_shin
td_mxc_rubyrails_shintutorialsruby
 
An ADF Special Report
An ADF Special Report An ADF Special Report
An ADF Special Report Luc Bors
 
Spring 3.x - Spring MVC
Spring 3.x - Spring MVCSpring 3.x - Spring MVC
Spring 3.x - Spring MVCGuy Nir
 
L2 Web App Development Guest Lecture At University of Surrey 20/11/09
L2 Web App Development Guest Lecture At University of Surrey 20/11/09L2 Web App Development Guest Lecture At University of Surrey 20/11/09
L2 Web App Development Guest Lecture At University of Surrey 20/11/09Daniel Bryant
 
Java Spring MVC Framework with AngularJS by Google and HTML5
Java Spring MVC Framework with AngularJS by Google and HTML5Java Spring MVC Framework with AngularJS by Google and HTML5
Java Spring MVC Framework with AngularJS by Google and HTML5Tuna Tore
 
springmvc-150923124312-lva1-app6892
springmvc-150923124312-lva1-app6892springmvc-150923124312-lva1-app6892
springmvc-150923124312-lva1-app6892Tuna Tore
 

What's hot (20)

#34.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자교육,국...
#34.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자교육,국...#34.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자교육,국...
#34.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자교육,국...
 
Magento 2 | Declarative schema
Magento 2 | Declarative schemaMagento 2 | Declarative schema
Magento 2 | Declarative schema
 
Catalyst patterns-yapc-eu-2016
Catalyst patterns-yapc-eu-2016Catalyst patterns-yapc-eu-2016
Catalyst patterns-yapc-eu-2016
 
Upgrade your javascript to drupal 8
Upgrade your javascript to drupal 8Upgrade your javascript to drupal 8
Upgrade your javascript to drupal 8
 
Анатолий Поляков - Drupal.ajax framework from a to z
Анатолий Поляков - Drupal.ajax framework from a to zАнатолий Поляков - Drupal.ajax framework from a to z
Анатолий Поляков - Drupal.ajax framework from a to z
 
First Steps in Drupal Code Driven Development
First Steps in Drupal Code Driven DevelopmentFirst Steps in Drupal Code Driven Development
First Steps in Drupal Code Driven Development
 
Planbox Backbone MVC
Planbox Backbone MVCPlanbox Backbone MVC
Planbox Backbone MVC
 
IndexedDB - Querying and Performance
IndexedDB - Querying and PerformanceIndexedDB - Querying and Performance
IndexedDB - Querying and Performance
 
Alfredo-PUMEX
Alfredo-PUMEXAlfredo-PUMEX
Alfredo-PUMEX
 
td_mxc_rubyrails_shin
td_mxc_rubyrails_shintd_mxc_rubyrails_shin
td_mxc_rubyrails_shin
 
RicoLiveGrid
RicoLiveGridRicoLiveGrid
RicoLiveGrid
 
Drupal 8: Fields reborn
Drupal 8: Fields rebornDrupal 8: Fields reborn
Drupal 8: Fields reborn
 
Sane Async Patterns
Sane Async PatternsSane Async Patterns
Sane Async Patterns
 
An ADF Special Report
An ADF Special Report An ADF Special Report
An ADF Special Report
 
Reporting solutions for ADF Applications
Reporting solutions for ADF ApplicationsReporting solutions for ADF Applications
Reporting solutions for ADF Applications
 
Spring 3.x - Spring MVC
Spring 3.x - Spring MVCSpring 3.x - Spring MVC
Spring 3.x - Spring MVC
 
Spring mvc
Spring mvcSpring mvc
Spring mvc
 
L2 Web App Development Guest Lecture At University of Surrey 20/11/09
L2 Web App Development Guest Lecture At University of Surrey 20/11/09L2 Web App Development Guest Lecture At University of Surrey 20/11/09
L2 Web App Development Guest Lecture At University of Surrey 20/11/09
 
Java Spring MVC Framework with AngularJS by Google and HTML5
Java Spring MVC Framework with AngularJS by Google and HTML5Java Spring MVC Framework with AngularJS by Google and HTML5
Java Spring MVC Framework with AngularJS by Google and HTML5
 
springmvc-150923124312-lva1-app6892
springmvc-150923124312-lva1-app6892springmvc-150923124312-lva1-app6892
springmvc-150923124312-lva1-app6892
 

Viewers also liked

Sample code to create files on your desktop using APEX
Sample code to create files on your desktop using APEXSample code to create files on your desktop using APEX
Sample code to create files on your desktop using APEXDeepak Balur
 
Pharma24bd.DELAR QUOTATION
Pharma24bd.DELAR QUOTATIONPharma24bd.DELAR QUOTATION
Pharma24bd.DELAR QUOTATIONG M ISLAM
 
ICH S4 (Duration of Chronic Toxicity Testing in Animals (Rodent and Non Roden...
ICH S4 (Duration of Chronic Toxicity Testing in Animals (Rodent and Non Roden...ICH S4 (Duration of Chronic Toxicity Testing in Animals (Rodent and Non Roden...
ICH S4 (Duration of Chronic Toxicity Testing in Animals (Rodent and Non Roden...PHARMADVISOR
 
NinjaScript 2010-10-14
NinjaScript 2010-10-14NinjaScript 2010-10-14
NinjaScript 2010-10-14lrdesign
 
Humanoid robot by mitesh kumar
Humanoid robot by mitesh kumarHumanoid robot by mitesh kumar
Humanoid robot by mitesh kumarMitesh Kumar
 
DIGITAL DIVIDE IN THAILAND: ANALYSIS AND RECOMMENDATIONS
DIGITAL DIVIDE IN THAILAND: ANALYSIS AND RECOMMENDATIONSDIGITAL DIVIDE IN THAILAND: ANALYSIS AND RECOMMENDATIONS
DIGITAL DIVIDE IN THAILAND: ANALYSIS AND RECOMMENDATIONSIAEME Publication
 
RAJESH - GRAPHIC DESIGNER- RESUME 2016
RAJESH - GRAPHIC DESIGNER- RESUME 2016RAJESH - GRAPHIC DESIGNER- RESUME 2016
RAJESH - GRAPHIC DESIGNER- RESUME 2016Rajesh Ravi
 
BSSML16 L3. Clusters and Anomaly Detection
BSSML16 L3. Clusters and Anomaly DetectionBSSML16 L3. Clusters and Anomaly Detection
BSSML16 L3. Clusters and Anomaly DetectionBigML, Inc
 
The Flexibility of Drupal 8
The Flexibility of Drupal 8The Flexibility of Drupal 8
The Flexibility of Drupal 8Michael Miles
 
Οι κάψουλες της Ιαπωνίας
Οι κάψουλες της ΙαπωνίαςΟι κάψουλες της Ιαπωνίας
Οι κάψουλες της ΙαπωνίαςPiperaki Eleni
 
BSSML16 L2. Ensembles and Logistic Regressions
BSSML16 L2. Ensembles and Logistic RegressionsBSSML16 L2. Ensembles and Logistic Regressions
BSSML16 L2. Ensembles and Logistic RegressionsBigML, Inc
 
The Flexibility of Drupal 8 | DCNLights 2017
The Flexibility of Drupal 8 | DCNLights 2017The Flexibility of Drupal 8 | DCNLights 2017
The Flexibility of Drupal 8 | DCNLights 2017Michael Miles
 
Stunning Glasswing Butterfly - Greta oto
Stunning Glasswing Butterfly  -  Greta otoStunning Glasswing Butterfly  -  Greta oto
Stunning Glasswing Butterfly - Greta otoMakala D.
 
The 2015 Sony world photography awards entries
The 2015 Sony world photography awards entriesThe 2015 Sony world photography awards entries
The 2015 Sony world photography awards entriesMakala D.
 

Viewers also liked (20)

Sample code to create files on your desktop using APEX
Sample code to create files on your desktop using APEXSample code to create files on your desktop using APEX
Sample code to create files on your desktop using APEX
 
Pharma24bd.DELAR QUOTATION
Pharma24bd.DELAR QUOTATIONPharma24bd.DELAR QUOTATION
Pharma24bd.DELAR QUOTATION
 
ICH S4 (Duration of Chronic Toxicity Testing in Animals (Rodent and Non Roden...
ICH S4 (Duration of Chronic Toxicity Testing in Animals (Rodent and Non Roden...ICH S4 (Duration of Chronic Toxicity Testing in Animals (Rodent and Non Roden...
ICH S4 (Duration of Chronic Toxicity Testing in Animals (Rodent and Non Roden...
 
NinjaScript 2010-10-14
NinjaScript 2010-10-14NinjaScript 2010-10-14
NinjaScript 2010-10-14
 
Humanoid robot by mitesh kumar
Humanoid robot by mitesh kumarHumanoid robot by mitesh kumar
Humanoid robot by mitesh kumar
 
Lkti aaaaa
Lkti aaaaaLkti aaaaa
Lkti aaaaa
 
Web 2 - Gastronomia
Web 2 - GastronomiaWeb 2 - Gastronomia
Web 2 - Gastronomia
 
DIGITAL DIVIDE IN THAILAND: ANALYSIS AND RECOMMENDATIONS
DIGITAL DIVIDE IN THAILAND: ANALYSIS AND RECOMMENDATIONSDIGITAL DIVIDE IN THAILAND: ANALYSIS AND RECOMMENDATIONS
DIGITAL DIVIDE IN THAILAND: ANALYSIS AND RECOMMENDATIONS
 
RAJESH - GRAPHIC DESIGNER- RESUME 2016
RAJESH - GRAPHIC DESIGNER- RESUME 2016RAJESH - GRAPHIC DESIGNER- RESUME 2016
RAJESH - GRAPHIC DESIGNER- RESUME 2016
 
ION Bangladesh - ISOC Dhaka Chapter Welcome
ION Bangladesh - ISOC Dhaka Chapter WelcomeION Bangladesh - ISOC Dhaka Chapter Welcome
ION Bangladesh - ISOC Dhaka Chapter Welcome
 
ION Bangladesh - IPv6 Experiences at Sri Lanka Telecom
ION Bangladesh - IPv6 Experiences at Sri Lanka TelecomION Bangladesh - IPv6 Experiences at Sri Lanka Telecom
ION Bangladesh - IPv6 Experiences at Sri Lanka Telecom
 
BSSML16 L3. Clusters and Anomaly Detection
BSSML16 L3. Clusters and Anomaly DetectionBSSML16 L3. Clusters and Anomaly Detection
BSSML16 L3. Clusters and Anomaly Detection
 
Menus 5
Menus 5Menus 5
Menus 5
 
The Flexibility of Drupal 8
The Flexibility of Drupal 8The Flexibility of Drupal 8
The Flexibility of Drupal 8
 
Οι κάψουλες της Ιαπωνίας
Οι κάψουλες της ΙαπωνίαςΟι κάψουλες της Ιαπωνίας
Οι κάψουλες της Ιαπωνίας
 
A Ferrovia em Portugal
A Ferrovia em PortugalA Ferrovia em Portugal
A Ferrovia em Portugal
 
BSSML16 L2. Ensembles and Logistic Regressions
BSSML16 L2. Ensembles and Logistic RegressionsBSSML16 L2. Ensembles and Logistic Regressions
BSSML16 L2. Ensembles and Logistic Regressions
 
The Flexibility of Drupal 8 | DCNLights 2017
The Flexibility of Drupal 8 | DCNLights 2017The Flexibility of Drupal 8 | DCNLights 2017
The Flexibility of Drupal 8 | DCNLights 2017
 
Stunning Glasswing Butterfly - Greta oto
Stunning Glasswing Butterfly  -  Greta otoStunning Glasswing Butterfly  -  Greta oto
Stunning Glasswing Butterfly - Greta oto
 
The 2015 Sony world photography awards entries
The 2015 Sony world photography awards entriesThe 2015 Sony world photography awards entries
The 2015 Sony world photography awards entries
 

Similar to Drupal8Day: Demystifying Drupal 8 Ajax Callback commands

Patterns Are Good For Managers
Patterns Are Good For ManagersPatterns Are Good For Managers
Patterns Are Good For ManagersAgileThought
 
Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016
Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016
Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016Nicolás Bouhid
 
Drupal & javascript
Drupal & javascriptDrupal & javascript
Drupal & javascriptAlmog Baku
 
Serverless archtiectures
Serverless archtiecturesServerless archtiectures
Serverless archtiecturesIegor Fadieiev
 
jQuery and Rails: Best Friends Forever
jQuery and Rails: Best Friends ForeverjQuery and Rails: Best Friends Forever
jQuery and Rails: Best Friends Foreverstephskardal
 
Introduction to Backbone.js for Rails developers
Introduction to Backbone.js for Rails developersIntroduction to Backbone.js for Rails developers
Introduction to Backbone.js for Rails developersAoteaStudios
 
Soa development using javascript
Soa development using javascriptSoa development using javascript
Soa development using javascriptDsixE Inc
 
Dependency Management with RequireJS
Dependency Management with RequireJSDependency Management with RequireJS
Dependency Management with RequireJSAaronius
 
Javascript frameworks: Backbone.js
Javascript frameworks: Backbone.jsJavascript frameworks: Backbone.js
Javascript frameworks: Backbone.jsSoós Gábor
 
Symfony2 - from the trenches
Symfony2 - from the trenchesSymfony2 - from the trenches
Symfony2 - from the trenchesLukas Smith
 
JQuery In Drupal
JQuery In DrupalJQuery In Drupal
JQuery In Drupalkatbailey
 
Viking academy backbone.js
Viking academy  backbone.jsViking academy  backbone.js
Viking academy backbone.jsBert Wijnants
 

Similar to Drupal8Day: Demystifying Drupal 8 Ajax Callback commands (20)

11-DWR-and-JQuery
11-DWR-and-JQuery11-DWR-and-JQuery
11-DWR-and-JQuery
 
11-DWR-and-JQuery
11-DWR-and-JQuery11-DWR-and-JQuery
11-DWR-and-JQuery
 
Patterns Are Good For Managers
Patterns Are Good For ManagersPatterns Are Good For Managers
Patterns Are Good For Managers
 
Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016
Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016
Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016
 
Drupal & javascript
Drupal & javascriptDrupal & javascript
Drupal & javascript
 
Serverless archtiectures
Serverless archtiecturesServerless archtiectures
Serverless archtiectures
 
jQuery and Rails: Best Friends Forever
jQuery and Rails: Best Friends ForeverjQuery and Rails: Best Friends Forever
jQuery and Rails: Best Friends Forever
 
Introduction to Backbone.js for Rails developers
Introduction to Backbone.js for Rails developersIntroduction to Backbone.js for Rails developers
Introduction to Backbone.js for Rails developers
 
Intro to Laravel 4
Intro to Laravel 4Intro to Laravel 4
Intro to Laravel 4
 
Soa development using javascript
Soa development using javascriptSoa development using javascript
Soa development using javascript
 
Dependency Management with RequireJS
Dependency Management with RequireJSDependency Management with RequireJS
Dependency Management with RequireJS
 
Sqlapi0.1
Sqlapi0.1Sqlapi0.1
Sqlapi0.1
 
Javascript frameworks: Backbone.js
Javascript frameworks: Backbone.jsJavascript frameworks: Backbone.js
Javascript frameworks: Backbone.js
 
Rails is not just Ruby
Rails is not just RubyRails is not just Ruby
Rails is not just Ruby
 
React, Flux and a little bit of Redux
React, Flux and a little bit of ReduxReact, Flux and a little bit of Redux
React, Flux and a little bit of Redux
 
Dwr
DwrDwr
Dwr
 
Symfony2 - from the trenches
Symfony2 - from the trenchesSymfony2 - from the trenches
Symfony2 - from the trenches
 
JQuery In Drupal
JQuery In DrupalJQuery In Drupal
JQuery In Drupal
 
Viking academy backbone.js
Viking academy  backbone.jsViking academy  backbone.js
Viking academy backbone.js
 
RequireJS
RequireJSRequireJS
RequireJS
 

More from Michael Miles

Inclusive Design: Thinking beyond accessibility
Inclusive Design: Thinking beyond accessibilityInclusive Design: Thinking beyond accessibility
Inclusive Design: Thinking beyond accessibilityMichael Miles
 
How to Foster Team Success
How to Foster Team SuccessHow to Foster Team Success
How to Foster Team SuccessMichael Miles
 
How to Foster Team Success | Drupalcon 2017
How to Foster Team Success | Drupalcon 2017How to Foster Team Success | Drupalcon 2017
How to Foster Team Success | Drupalcon 2017Michael Miles
 
Inclusive Design: Thinking Beyond Accessibility | NERDSummit 2017
Inclusive Design: Thinking Beyond Accessibility | NERDSummit 2017Inclusive Design: Thinking Beyond Accessibility | NERDSummit 2017
Inclusive Design: Thinking Beyond Accessibility | NERDSummit 2017Michael Miles
 
Inclusive Design: Thinking Beyond Accessibility | DCNL 2017
Inclusive Design: Thinking Beyond Accessibility | DCNL 2017Inclusive Design: Thinking Beyond Accessibility | DCNL 2017
Inclusive Design: Thinking Beyond Accessibility | DCNL 2017Michael Miles
 
INCLUSIVE DESIGN: Going beyond Accessibility
INCLUSIVE DESIGN: Going beyond AccessibilityINCLUSIVE DESIGN: Going beyond Accessibility
INCLUSIVE DESIGN: Going beyond AccessibilityMichael Miles
 
The Flexibility of Drupal
The Flexibility of DrupalThe Flexibility of Drupal
The Flexibility of DrupalMichael Miles
 
Badcamp 2015 - R.E.A.D: Four Steps for Selecting The Right Modules
Badcamp 2015 - R.E.A.D: Four Steps for Selecting The Right ModulesBadcamp 2015 - R.E.A.D: Four Steps for Selecting The Right Modules
Badcamp 2015 - R.E.A.D: Four Steps for Selecting The Right ModulesMichael Miles
 
The Flexibility of Drupal
The Flexibility of DrupalThe Flexibility of Drupal
The Flexibility of DrupalMichael Miles
 
The Flexibility of Drupal
The Flexibility of DrupalThe Flexibility of Drupal
The Flexibility of DrupalMichael Miles
 
Flcamp2015 - R.E.A.D: Four steps for selecting the right modules
Flcamp2015 - R.E.A.D: Four steps for selecting the right modulesFlcamp2015 - R.E.A.D: Four steps for selecting the right modules
Flcamp2015 - R.E.A.D: Four steps for selecting the right modulesMichael Miles
 
R.E.A.D: Four steps for selecting the right modules Midcamp 2015
R.E.A.D: Four steps for selecting the right modules Midcamp 2015R.E.A.D: Four steps for selecting the right modules Midcamp 2015
R.E.A.D: Four steps for selecting the right modules Midcamp 2015Michael Miles
 
How to R.E.A.D: Steps for how to select the correct module @NEWDCamp 2014
How to R.E.A.D: Steps for how to select the correct module @NEWDCamp 2014How to R.E.A.D: Steps for how to select the correct module @NEWDCamp 2014
How to R.E.A.D: Steps for how to select the correct module @NEWDCamp 2014Michael Miles
 
To Patch or Custom: How to decide when to patch a contrib module or go custom...
To Patch or Custom: How to decide when to patch a contrib module or go custom...To Patch or Custom: How to decide when to patch a contrib module or go custom...
To Patch or Custom: How to decide when to patch a contrib module or go custom...Michael Miles
 

More from Michael Miles (15)

Inclusive Design: Thinking beyond accessibility
Inclusive Design: Thinking beyond accessibilityInclusive Design: Thinking beyond accessibility
Inclusive Design: Thinking beyond accessibility
 
How to Foster Team Success
How to Foster Team SuccessHow to Foster Team Success
How to Foster Team Success
 
How to Foster Team Success | Drupalcon 2017
How to Foster Team Success | Drupalcon 2017How to Foster Team Success | Drupalcon 2017
How to Foster Team Success | Drupalcon 2017
 
Inclusive Design: Thinking Beyond Accessibility | NERDSummit 2017
Inclusive Design: Thinking Beyond Accessibility | NERDSummit 2017Inclusive Design: Thinking Beyond Accessibility | NERDSummit 2017
Inclusive Design: Thinking Beyond Accessibility | NERDSummit 2017
 
Inclusive Design: Thinking Beyond Accessibility | DCNL 2017
Inclusive Design: Thinking Beyond Accessibility | DCNL 2017Inclusive Design: Thinking Beyond Accessibility | DCNL 2017
Inclusive Design: Thinking Beyond Accessibility | DCNL 2017
 
INCLUSIVE DESIGN: Going beyond Accessibility
INCLUSIVE DESIGN: Going beyond AccessibilityINCLUSIVE DESIGN: Going beyond Accessibility
INCLUSIVE DESIGN: Going beyond Accessibility
 
Inclusive Design
Inclusive DesignInclusive Design
Inclusive Design
 
The Flexibility of Drupal
The Flexibility of DrupalThe Flexibility of Drupal
The Flexibility of Drupal
 
Badcamp 2015 - R.E.A.D: Four Steps for Selecting The Right Modules
Badcamp 2015 - R.E.A.D: Four Steps for Selecting The Right ModulesBadcamp 2015 - R.E.A.D: Four Steps for Selecting The Right Modules
Badcamp 2015 - R.E.A.D: Four Steps for Selecting The Right Modules
 
The Flexibility of Drupal
The Flexibility of DrupalThe Flexibility of Drupal
The Flexibility of Drupal
 
The Flexibility of Drupal
The Flexibility of DrupalThe Flexibility of Drupal
The Flexibility of Drupal
 
Flcamp2015 - R.E.A.D: Four steps for selecting the right modules
Flcamp2015 - R.E.A.D: Four steps for selecting the right modulesFlcamp2015 - R.E.A.D: Four steps for selecting the right modules
Flcamp2015 - R.E.A.D: Four steps for selecting the right modules
 
R.E.A.D: Four steps for selecting the right modules Midcamp 2015
R.E.A.D: Four steps for selecting the right modules Midcamp 2015R.E.A.D: Four steps for selecting the right modules Midcamp 2015
R.E.A.D: Four steps for selecting the right modules Midcamp 2015
 
How to R.E.A.D: Steps for how to select the correct module @NEWDCamp 2014
How to R.E.A.D: Steps for how to select the correct module @NEWDCamp 2014How to R.E.A.D: Steps for how to select the correct module @NEWDCamp 2014
How to R.E.A.D: Steps for how to select the correct module @NEWDCamp 2014
 
To Patch or Custom: How to decide when to patch a contrib module or go custom...
To Patch or Custom: How to decide when to patch a contrib module or go custom...To Patch or Custom: How to decide when to patch a contrib module or go custom...
To Patch or Custom: How to decide when to patch a contrib module or go custom...
 

Recently uploaded

Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作
Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作
Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作ys8omjxb
 
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一z xss
 
Top 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptxTop 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptxDyna Gilbert
 
Call Girls Near The Suryaa Hotel New Delhi 9873777170
Call Girls Near The Suryaa Hotel New Delhi 9873777170Call Girls Near The Suryaa Hotel New Delhi 9873777170
Call Girls Near The Suryaa Hotel New Delhi 9873777170Sonam Pathan
 
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书rnrncn29
 
Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170
Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170
Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170Sonam Pathan
 
PHP-based rendering of TYPO3 Documentation
PHP-based rendering of TYPO3 DocumentationPHP-based rendering of TYPO3 Documentation
PHP-based rendering of TYPO3 DocumentationLinaWolf1
 
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书zdzoqco
 
Font Performance - NYC WebPerf Meetup April '24
Font Performance - NYC WebPerf Meetup April '24Font Performance - NYC WebPerf Meetup April '24
Font Performance - NYC WebPerf Meetup April '24Paul Calvano
 
SCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is prediSCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is predieusebiomeyer
 
Q4-1-Illustrating-Hypothesis-Testing.pptx
Q4-1-Illustrating-Hypothesis-Testing.pptxQ4-1-Illustrating-Hypothesis-Testing.pptx
Q4-1-Illustrating-Hypothesis-Testing.pptxeditsforyah
 
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书rnrncn29
 
NSX-T and Service Interfaces presentation
NSX-T and Service Interfaces presentationNSX-T and Service Interfaces presentation
NSX-T and Service Interfaces presentationMarko4394
 
Film cover research (1).pptxsdasdasdasdasdasa
Film cover research (1).pptxsdasdasdasdasdasaFilm cover research (1).pptxsdasdasdasdasdasa
Film cover research (1).pptxsdasdasdasdasdasa494f574xmv
 
Contact Rya Baby for Call Girls New Delhi
Contact Rya Baby for Call Girls New DelhiContact Rya Baby for Call Girls New Delhi
Contact Rya Baby for Call Girls New Delhimiss dipika
 

Recently uploaded (17)

Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作
Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作
Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作
 
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一
 
young call girls in Uttam Nagar🔝 9953056974 🔝 Delhi escort Service
young call girls in Uttam Nagar🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Uttam Nagar🔝 9953056974 🔝 Delhi escort Service
young call girls in Uttam Nagar🔝 9953056974 🔝 Delhi escort Service
 
Top 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptxTop 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptx
 
Call Girls Near The Suryaa Hotel New Delhi 9873777170
Call Girls Near The Suryaa Hotel New Delhi 9873777170Call Girls Near The Suryaa Hotel New Delhi 9873777170
Call Girls Near The Suryaa Hotel New Delhi 9873777170
 
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
 
Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170
Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170
Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170
 
PHP-based rendering of TYPO3 Documentation
PHP-based rendering of TYPO3 DocumentationPHP-based rendering of TYPO3 Documentation
PHP-based rendering of TYPO3 Documentation
 
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
 
Font Performance - NYC WebPerf Meetup April '24
Font Performance - NYC WebPerf Meetup April '24Font Performance - NYC WebPerf Meetup April '24
Font Performance - NYC WebPerf Meetup April '24
 
SCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is prediSCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is predi
 
Q4-1-Illustrating-Hypothesis-Testing.pptx
Q4-1-Illustrating-Hypothesis-Testing.pptxQ4-1-Illustrating-Hypothesis-Testing.pptx
Q4-1-Illustrating-Hypothesis-Testing.pptx
 
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
 
Hot Sexy call girls in Rk Puram 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in  Rk Puram 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in  Rk Puram 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Rk Puram 🔝 9953056974 🔝 Delhi escort Service
 
NSX-T and Service Interfaces presentation
NSX-T and Service Interfaces presentationNSX-T and Service Interfaces presentation
NSX-T and Service Interfaces presentation
 
Film cover research (1).pptxsdasdasdasdasdasa
Film cover research (1).pptxsdasdasdasdasdasaFilm cover research (1).pptxsdasdasdasdasdasa
Film cover research (1).pptxsdasdasdasdasdasa
 
Contact Rya Baby for Call Girls New Delhi
Contact Rya Baby for Call Girls New DelhiContact Rya Baby for Call Girls New Delhi
Contact Rya Baby for Call Girls New Delhi
 

Drupal8Day: Demystifying Drupal 8 Ajax Callback commands

  • 6. Callback Structure: JavaScript 01 (function ($, window, Drupal, drupalSettings) { 02 'use strict'; 03 /** 04 * [commandName description] 05 * 06 * @param {Drupal.Ajax} [ajax] 07 * @param {object} response 08 * @param {number} [status] 09 */ 10 Drupal.AjaxCommands.prototype.[commandName] = function(ajax, response, status){ 11 12 // Custom javascript goes here ... 13 14 } 15 16 })(jQuery, this, Drupal, drupalSettings); [module]/js/[javascript].js Example of the structure for the JavaScript half of a callback command as defined in a module.
  • 8. Callback Structure: PHP 01 namespace Drupal[module]Ajax 02 use DrupalCoreAjaxCommandInterface; 03 04 // An Ajax command for calling [commandName]() JavaScript method. 05 class [CommandName]Command implements CommandInterface { 06 07 // Implements DrupalCoreAjaxCommandInterface:render(). 08 public function render() { 09 return array( 10 'command' => '[commandName]', // Name of JavaScript Method. 11 // other response arguments... 12 ); 13 } 14 } [module]/src/Ajax/[CommandName]Command.php Example of the structure for the PHP half of a callback command as defined in a module.
  • 9. Core Example: Remove 01 Drupal.AjaxCommands.prototype = { 02 // ... 03 /** 04 * Command to remove a chunk from the page. 05 * 06 * @param {Drupal.Ajax} [ajax] 07 * @param {object} response 08 * @param {string} response.selector 09 * @param {object} [response.settings] 10 * @param {number} [status] 11 */ 12 remove: function (ajax, response, status) { 13 var settings = response.settings || ajax.settings || drupalSettings; 14 $(response.selector).each(function () { 15 Drupal.detachBehaviors(this, settings); 16 }) 17 .remove(); 18 }, 19 //... misc/ajax.js The JavaScript function for the core 'remove' callback command. It is basically a wrapper for the jQuery 'remove' method.
  • 10. Core Example: RemoveCommand 01 namespace DrupalCoreAjax; 02 use DrupalCoreAjaxCommandInterface; 03 /** 04 * Ajax command for calling the jQuery remove() method. 05 * ... 06 */ 07 class RemoveCommand Implements CommandInterface { 08 // ... 09 /** 10 * Implements DrupalCoreAjaxCommandInterface:render(). 11 */ 12 public function render() { 13 return array( 14 'command' => 'remove', 15 'selector' => $this->selector, 16 ); 17 } 18 } core/lib/Drupal/Core/Ajax/RemoveCommand.php The PHP class for the core 'remove' callback command. Implements CommandInterface, so it must define the method 'render' that returns an associative array.
  • 11. PHP 01 //... 02 public function render() { 03 return array( 04 'command' => 'remove', 05 'selector' => $this->selector, 06 ); 07 } core/lib/Drupal/Core/Ajax/RemoveCommand.php JavaScript 01 //... 02 remove: function (ajax, response, status) { 03 var settings = response.settings || ajax.settings || drupalSettings; 04 $(response.selector).each(function () { 05 Drupal.detachBehaviors(this, settings); 06 }) 07 .remove(); 08 }, misc/ajax.js Can see how the two halfs are tied together. Value on line #4 of PHP matches JavaScript function name defined on line #2 in JavaScript. Passed CSS selector on line #5 in PHP is used on line #4 in JavaScript.
  • 15. Create a Module 01 name: 'Slide Down Command' 02 type: module 03 description: Provides an Ajax Callback command for the jQuery SlideDown method. 04 package: other 05 core: 8.x slide_down/slide_down.info.yml Custom Ajax callback commands must be defined in a module.
  • 16. Create JavaScript Function 01 (function ($, window, Drupal, drupalSettings) { 02 03 'use strict'; 04 05 // Command to Slide Down page elements. 06 Drupal.AjaxCommands.prototype.slideDown = function(ajax, response, status){ 07 // Get duration if sent, else use default of slow. 08 var duration = response.duration ? response.duration : "slow"; 09 // slide down the selected element(s). 10 $(response.selector).slideDown(duration); 11 } 12 })(jQuery, this, Drupal, drupalSettings); slide_down/js/slidedown-command.js Attach a JavaScript function to the AjaxCommands object provided by the Ajax Framework. Accepts the three arguments and is a wrapper for the jQuery method.
  • 17. Create Asset Library 01 slidedown: 02 version: VERSION 03 js: 04 js/slidedown-command.js; {} 05 dependencies: 06 - core/drupal.ajax slide_down/slide_down.libraries.yml In Drupal 8 custom JavaScript files must be added to an asset library to be able to be included on a page.
  • 18. Create PHP Class 01 namespace Drupalslide_downAjax; 02 use DrupalCoreAjaxCommandInterface; 03 04 class SlideDownCommand implements CommandInterface { 05 // ... 06 // Constructs an SlideDownCommand object. 07 public function __construct($selector, $duration = NULL) { 08 $this->selector = $selector; 09 $this->duration = $duration; 10 } 11 12 // Implements DrupalCoreAjaxCommandInterface:render(). 13 public function render() { 14 return array( 15 'command' => 'slideDown', 16 'method' => NULL, 17 'selector' => $this->selector, 18 'duration' => $this->duration, 19 ); 20 } 21 } slide_down/src/Ajax/SlideDownCommand.php Create a PHP class that implements CommandInterface. Must define a 'render' method and return an associative array. In the array, pass the element with key of 'command' and value being the name of the JavaScript function and any repsonse data.
  • 19. To Create a Callback Command: Create a module Attach JavaScript function to 'Drupal.AjaxCommands.prototype' Define an asset library Create PHP class that implements 'CommandInterface'
  • 22. Add Ajax Library to Page 01 use DrupaldblogControllerDbLogController as ControllerBase; 02 03 class DbLogController extends ControllerBase { 04 // Override overview() method. 05 public function overview() { 06 $build = parent::overview(); 07 // ... 08 // Add custom library. 09 $build['#attached']['library'][] = 'ajax_dblog/ajax-dblog'; 10 return $build; 11 } 12 // ... 13 } ajax_dblog/src/Controller/DbLogController.php Need to attach custom library onto page so that custom JavaScript and Ajax Framework is included.
  • 23. 01 ajax-dblog: 02 version: VERSION 03 css: 04 component: 05 css/ajax_dblog.module.css: {} 06 js: 07 js/behaviors.js: {} 08 dependencies: 09 - slide_down/slidedown ajax_dblog/ajax_dblog.libraries.yml 01 slidedown: 02 version: VERSION 03 js: 04 js/slidedown-command.js: {} 05 dependencies: 06 - core/drupal.ajax slide_down/slide_down.libraries.yml Defining a dependency in the library on another library. The other library depends on the Ajax Framework. Drupal will follow chain to include all depended JavaScript files.
  • 24. Add Ajax to Elements 01 namespace Drupalajax_dblogController; 02 use DrupaldblogControllerDbLogController as ControllerBase; 03 04 class DbLogController extends ControllerBase { 05 // Override overview() method. 06 public function overview() { 07 $build = parent::overview(); 08 // Alter the links for each log message. 09 foreach ($build['dblog_table']['#rows'] as &$row) { 10 // ... 11 // Build route parameters. 12 $params = array( 13 'method' => 'nojs', 14 //... 15 ); 16 // Build link options. 17 $ops = array( 'attributes' => array( 18 'class' => array('use-ajax', 'dblog-event-link'), 19 )); 20 // Replace with a new link. 21 $row['data'][3] = Link::createFromRoute($txt,'ajax_dblog.event',$params,$ops); 22 } 23 return $build; ajax_dblogs/src/Controller/DbLogController.php Need to have elements that will trigger an Ajax request. Rebuilding links on page to point to new route (line #21). Links will have the class 'use­ajax' (line #18), which the Ajax Framework will look for.
  • 26. Create Ajax Request Endpoint 01 ajax_dblog.event: 02 path: '/admin/reports/dblog/{method}/event/{event_id}' 03 defaults: 04 _controller: 'Drupalajax_dblogControllerDbLogController::ajaxEventDetails' 05 requirements: 06 _permission: 'access site reports' 07 method: 'nojs|ajax' ajax_dblog/ajax_dblog.routing.yml /admin/reports/dblog/nojs/event/123 /admin/reports/dblog/ajax/event/123 Create an endpoint that will handle Ajax Requests. The ajax framework will replace 'nojs' with 'ajax' on all request. Can use as a check to handle graceful degradation.
  • 27. Return an AjaxResponse of Callback Commands 01 use DrupalCoreAjaxAjaxResponse; 02 use DrupalCoreAjaxAfterCommand; 03 use DrupalCoreAjaxRemoveCommand; 04 use Drupalslide_downAjaxSlideDownCommand; 05 06 class DbLogController extends ControllerBase { 07 // ... 08 public function ajaxEventDetails($method, $event_id) { 09 //... 10 if ($method == 'ajax') { 11 $event = parent::eventDetails($event_id); 12 $event_details = [ ... ]; 13 // Create an AjaxResponse. 14 $response = new AjaxResponse(); 15 // Remove old event details. 16 $response->addCommand(new RemoveCommand('.dblog-event-row')); 17 // Insert event details after event. 18 $response->addCommand(new AfterCommand('#dblog-event-' . $event_id, $event_details 19 // SlideDown event details. 20 $response->addCommand(new SlideDownCommand('#dblog-event-details-' . $event_id 21 } 22 // ... 23 } ajax_dblog/src/Controller/DbLogController.php Have a method that is the endpoint for the Ajax request (line #8). Need to build an AjaxReponse object (line #14). Will add commands to this response using the 'addCommand' method and creating a new instance of the relevant Callback Command class (lines #16, #18, #20).
  • 29. Ajax Response 01 [ 02 { 03 "command":"remove", 04 "selector":".dblog-event-row" 05 }, 06 { 07 "command":"insert", 08 "method":"after", 09 "selector":"#dblog-event-32", 10 "data":"...", 11 "settings":null 12 }, 13 { 14 "command":"slideDown", 15 "method":null, 16 "selector":"#dblog-event-details-32", 17 "duration":null 18 } 19 ] The returned JSON array is parsed by Ajax Framework. Finds JavaScript function to execute and the passes the object as the data for the response argument of the function.
  • 30. To Use Callback Commands Include the Ajax library and commands on the page. Have endpoint that returns an AjaxResponse Add commands to response using 'addCommand'