Drupal

34
Drupal as Backend for Mobile Apps Drupal Camp PHOENIX - March, 2013 Chandi Cumaranatunge

Transcript of Drupal

Page 1: Drupal

Drupal as Backend for Mobile Apps

Drupal Camp PHOENIX - March, 2013

Chandi Cumaranatunge

Page 2: Drupal

Chandi Cumaranatunge

Systems Analyst, SeniorUniversity Technology Office

[email protected]

Page 3: Drupal

Topics

• Drupal as CMS

• Drupal as datastore

• Exposing Drupal data

• Consuming and displaying data

• Building a hybrid mobile app to display data

• Cordova / PhoneGap, client-side templates

Page 4: Drupal

The Code

http:// bit.ly / drupal-mb

is on

Page 5: Drupal

CMS

Create, edit, publish and manage content

Page 6: Drupal

Content in Drupal 7

• Entities

• nodes

• users

• comments

• taxonomy terms

• files

Page 7: Drupal

Drupal as CMS

database

Entitiestheming

web browser

Drupal

HTML, JS, CSS

over HTTP

Page 8: Drupal

Drupal as Datastore

database

Entities

Pure Data

Drupal

JSON, XML

over HTTP

No theming!

Page 9: Drupal

Drupal as Backendto a mobile app

JSON, XML

over HTTP

Page 12: Drupal

CRUDfor entities

CREATE Entity HTTP POST /nodeREAD Entity HTTP GET /node/15.jsonUPDATE Entity HTTP PUT /node/15.jsonDELETE Entity HTTP DELETE /node/15

RESTful Web Services module provides full CRUD support with authentication

Page 13: Drupal

Let’s build an app!Bird Sightings

Drupal backend(datastore & API)

Mobile front-end(client interface)

Page 14: Drupal

Drupal backend first

• Define entities

• Create User (restws_user)

• Define roles and specify access permissions

• Populate content

Page 17: Drupal

Access permissionsrestws module permissions

restws_user permissions to create content

Page 18: Drupal

Mobile front end

• Presentation moved to client-side (theming)

• Application state handling moved to client side (page routing)

database

Entities

Drupal

Har

dwar

eM

obile

OS

Mob

ile

App

Mobile Device

JSON, XML

Page 19: Drupal

Hybrid Mobile Apps

MobileDevice

Hybrid App

Mobile OS

HTML Rendering Engine (WebView)

Hardware

HTML5, CSS3, JS

Page 20: Drupal

Hybrid vs Native Appscriteria that matter

Hybrid App Native App

Skills HTML, CSS, JS Objective-C, Java

UI HTML elements Native UI

Performance Slower Fastest

Deploy Multiple OSs Single OS

Page 22: Drupal

Bird Sightings Mobile Appdesign and implementation decisions

• Hybrid mobile app• Speed not critical, leverage existing HTML, CSS and

JS skills

• jQuery for DOM Manipulation• Known quantity

• jQuery Mobile for Architecture and UI• Simple app without complex views and states

• Availability of required UI components

Page 23: Drupal

Bird Sightings Mobile Appfinal product specs

• Built iteratively ( final iteration index_11.html )

• 150 lines of code ( 1/3 HTML, 2/3 Javascript )

• No CSS

Page 24: Drupal

Cross-site Scripting

#CORS SetEnvIf Origin "^(.*)$" AccessControlAllowOrigin=$0 Header add Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin Header set Access-Control-Allow-Methods "GET, PUT, POST, OPTIONS" Header set Access-Control-Allow-Credentials "true" Header set Access-Control-Allow-Headers "Authorization, Origin, Content-Type, X-CSRF-Token"

• Javascript cross-site AJAX client requests are blocked by web browsers

• JSONP

• CORS

Add the following modifications to the Drupal .htaccess file for CORS support

Page 25: Drupal

jQuery Mobile Basics + Entity Readindex_02.html

<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>Bird Sightings</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.css" /> <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script> </head><body>

<div data-role="page" id="birds"> <div data-role="header"> <h1>Birds</h1> </div><!-- /header --> <div data-role="content"> <p>Page content goes here.</p> </div><!-- /content --> </div><!-- /page -->

<script> var bird_server = 'birds.mamp.chandima.info'; $('#birds').on('pageshow', function(event, ui){ $.getJSON('http://'+bird_server+'/node.json?type=bird', function(data) { console.log(data); }); }); </script> </body></html>

Page 26: Drupal

Client-side Javascript Templatesprevent mixing data with code

Hogan.js is compiled Mustache.js

Page 27: Drupal

Client-side templating with Hogan.jsindex_03.html

<script src="http://twitter.github.com/hogan.js/builds/2.0.0/hogan-2.0.0.js"></script> <!-- bird list template --> <script type="text/template" id="bird-li-tpl">

{{#list}} <li><a href="{{url}}"><h4>{{title}}</h4></a></li> {{/list}} </script>

<div data-role="page" id="birds"> <div data-role="header"> <h1>Birds</h1> </div><!-- /header --> <div data-role="content"> <ul id="bird-list" data-role="listview"></ul> </div><!-- /content --> </div><!-- /page -->

<script> var bird_server = 'birds.mamp.chandima.info'; var birdListTpl = Hogan.compile($("#bird-li-tpl").html()); $('#birds').on('pageshow', function(event, ui){ $.getJSON('http://'+bird_server+'/node.json?type=bird', function(data) { var html = birdListTpl.render(data); $('#bird-list').html(html); $('#bird-list').listview('refresh'); }); }); </script>

Page 28: Drupal

Single-page architecture - Bird list and bird detailsindex_04.html

<div data-role="page" id="birds"> <div data-role="header"> <h1>Birds</h1> </div><!-- /header --> <div data-role="content"> <ul id="bird-list" data-role="listview"></ul> </div><!-- /content --> </div><!-- /page --> <div data-role="page" id="bird"> <div data-role="header"> <h1>Bird details</h1> </div><!-- /header --> <div data-role="content"> <p>Content goes here...</p> </div><!-- /content --> </div><!-- /page -->

Page 29: Drupal

Getting a session token - authenticationindex_09.html

// get session token $.ajax ({ type: 'GET', url: 'http://'+bird_server+'/restws/session/token', async: false, beforeSend: function (xhr){ // base64 encoded user:pass xhr.setRequestHeader('Authorization', 'Basic cmVzdHdzX2VkaXRvcjplYXN5cGFzcw=='); }, success: function (data){ authToken = data; } });

Page 30: Drupal

Getting geolocation and creating location entityindex_10.html

if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(

function(position) { var loc_entity = { 'type':'location', 'field_latitude':position.coords.latitude, 'field_longitude':position.coords.longitude, 'field_bird': {'id':nid}

}; $.ajax ({ type: 'POST', url: 'http://'+bird_server+'/node', contentType: 'application/json', data: JSON.stringify(loc_entity), beforeSend: function (xhr){ xhr.setRequestHeader('X-CSRF-Token', authToken); }, xhrFields: { withCredentials: true }, success: function (data){ console.log(data); }, error: function (xhr, ajaxOptions, thrownError){ console.log(thrownError); } }); },

Page 31: Drupal

Building app with Apache Cordova

Page 32: Drupal

Building apps with PhoneGap Build

Page 33: Drupal

Takeaways

• Hybrid mobile apps leverage HTML, CSS and JS skills.

• Build for multiple mobile platforms from a single code-base.

• Templates allow you to separate data from code.

• REST is the predominant web services design model.

• Drupal is evolving into a complete web services platform.

• Take the code and build some apps!!!!

Page 34: Drupal

Thank you!

Questions?