drupal-client
A Javascript client for Drupal 7 / Services Module
Requirements
-
An installation of Drupal 7.x and Services Module
-
REST Server module enabled, an endpoint defined and appropriate permissions (system, user, node, etc.). In server settings, enable only response formatter
json
the request parsing mime typesapplication/json
,application/x-www-form-urlencoded
andmultipart/form-data
. -
A Javascript project - node.js or Titanium are known to work.
Installation
node.js
npm install drupal-client
Titanium/Alloy
Copy lib/drupal.js and lib/field.js into your app/lib/ folder.
Usage
Configure the client for your installation of Drupal+Services. Note that the URL includes the trailing slash.
var Drupal = ;var drupal = ; drupal;
Create a Service and enable (at least) the Resources called "system" and "user".
Get a session
drupal;
Create an account
var user = name: 'my_new_username' pass: 'my_new_password' mail: 'my_email@drupal.js'; drupal;
Login
var my_username = "<DRUPAL USERNAME>";var my_password = "<DRUPAL PASSWORD>"; var userObject; drupal;
Modify User Info
This updates an account profile on the server. userObject
is a user object that may have been received from a login request (see above).
drupal;
Upload A File
var filename = "uploaded_file.png";var data = ;var base64data = data;var filesize = datalength; drupal;
Create a New Node
var node = type: "my_content_type" title: "My New Node" body: drupalfield field_bool: drupalfield field_decimal: drupalfield field_float: drupalfield field_integer: drupalfield field_multiple: drupalfield field_file: drupalfield field_date: field; drupal;
Make Requests
The workhorse function of the interface is makeAuthenticatedRequest(config, success, failure, headers)
. There are a few helper functions included for posting/getting nodes, getting views, uploading files, etc. They all construct a call to makeAuthenticatedRequest()
. This function should facilitate most things that people want to do with Drupal in a mobile environment. It's also easy to use `makeAuthenticatedRequest' to make requests against custom Services. The short-term roadmap includes calls to the services supporting entities.
Tests
To run the tests, rename test/config.js.example
to test/config.js
and replace strings with the url of your Drupal install and your service endpoint.
npm install
npm test