GithubHelp home page GithubHelp logo

drupal.api.js's Introduction

The Drupal JavaScript API Library

This library acts as an object oriented JavaScript wrapper around the Drupal services module ( http://drupal.org/project/services ). This makes is extremely simple to work with Drupal services from within a web application that uses JavaScript (JSON) for communication to the Drupal RESTful server.

A great use case for this library is to use it along with the PhoneGap platform when building a mobile application that may need to interact with your Drupal CMS backend.

Installation & Configuration

  • You will need a Drupal 7 site with Services 3.x and CORS modules (http://drupal.org/project/cors) installed.
  • Install the modules: Services, REST Server, CTools, CORS
  • Go to admin/structure/services and +Add a new Services endpoint.
  • Give it a name of "rest" and a Path to Endpoint of "rest".
  • Select the REST as the Server.
  • Enable "Session Authentication"
  • Press "Save" to save your endpoint.
  • Now click on "Edit Resource" in the services overview page next to "rest"
  • Enable "node", "system", and "user" resource (use version 1.0 for login and logout) and then press "Save"
  • Click on the "Server" tab, and then enable "jsonp" and then check "application/x-www-form-urlencoded" for application parsing and then press "Save".
  • Now go edit your CORS configuration @ admin/config/services/cors, and put the following in your CORS configuration.
rest|*|POST,GET,PUT,OPTIONS,DELETE|Content-Type,Authorization,X-CSRF-Token|true
  • Save the CORS configuration.
  • Now run the tests by simply typing the following in the command line.
npm install
grunt test

Usage

Note: This module currently requires jQuery. To use this library, simply add the bin/drupal.api.min.js file to your HTML page.

<script type='text/javascript' src='bin/drupal.api.min.js'></script>

Next, you will now need to tell the library about your endpoint URL.

<script type="text/javascript">
  drupal.endpoint = 'http://drupal.org/api';
</script>

You can then start using the following API provided from the Examples below.

For Drupal Services versions below 3.5: For older versions of services, you must also tell this library to not use the Token authentication.

<script type="text/javascript">
  drupal.useToken = false;
</script>

Examples

  • To create a new node.
(new drupal.node({
  title:"My new Node"
})).save();
  • To create a new node with fields. Use the "fields" parameter in the JSON object.
(new drupal.node({
  title: "My fielded node.",
  fields: {
    field_a: {
      'value': 'The value of this field.'  // <-- maps to node.field_a['und'][0]['value']
    },
    field_b: {
      'value': 'The value of this field.'  // <-- maps to node.field_b['und'][0]['value']
    }
  }
})).save();
  • To update an existing node.
new drupal.node({nid:10}, function(node) {

  // Change title then save.
  node.title = "Change the Title";
  node.save();
});
  • To get a list of nodes of type='page'
drupal.node.index({type:'page'}, function(nodes) {

  // This is a list of nodes of type 'page'.
  console.log(nodes);
});
  • To delete a node on the server.
(new drupal.node({nid:10})).remove();
  • To connect to the drupal server.
new drupal.system(function(system) {

  // The currently logged in user.
  console.log(system.user);
});
  • To create a new user.
(new drupal.user({
  name:"travist":
  pass:"testing":
  mail:"[email protected]"
})).save();
  • To load an existing user.
new drupal.user({uid:10}, function(user) {

  // The logged in user.
  console.log(user);
});
  • To delete a user.
(new drupal.user({uid:10})).remove();
  • To get a variable on Drupal.
(new drupal.system()).get_variable("variable", "default", function(value) {

  // The value of the variable.
  console.log(value);
});
  • To set a variable on Drupal.
(new drupal.system()).set_variable("variable", "value", function() {

  console.log("done");
});
  • To delete a variable on Drupal.
(new drupal.system()).del_variable("variable", function() {

  console.log("done");
});

Running Tests

This library is tested with QUnit. You can run the tests by typing the folling in the console.

npm install
grunt test

drupal.api.js's People

Contributors

travist avatar

Stargazers

 avatar Brian Cannard avatar Rod Kisten (Costa) avatar Sean Dietrich avatar Angus H. avatar  avatar Theodoros Ploumis avatar R.J. (Steinert) Corwin avatar Rocco Russo avatar Josh Goldsmith avatar  avatar Andrey Petukhov avatar Adrienne avatar bob stoute avatar György Chityil avatar Brian Tofte-Schumacher avatar ForeverGlory avatar snowdream avatar Danny Montalvo avatar yāλu avatar  avatar joe beuckman avatar Arvin Singla avatar Patrick Dawkins avatar LaminarRainbow avatar Julien Verkest avatar Daniel Primo avatar Attila Fekete avatar Noah Adler avatar Ataa avatar Chao Chen avatar Mike Kadin avatar JT5D avatar Sean avatar Mojzis avatar Mateu Aguiló Bosch avatar Ryan Merritt avatar Chris Weber avatar Jeff Linwood avatar Tyler Frankenstein avatar Maksim Pecherskiy avatar Chris Charlton avatar Matthew Connerton avatar Timani Tunduwani avatar Fredrik Lassen avatar Per Porserot avatar  avatar Ferdi Alimadhi avatar Sebastian Daniel avatar Chris Hinkley avatar David Lanier avatar Kyle Mathews avatar SeeVa avatar Bicherele avatar Daniel Rosengren avatar

Watchers

Sebastian Daniel avatar  avatar Danny Montalvo avatar James Cloos avatar Luis Sánchez avatar  avatar Jaime Guzman avatar  avatar

drupal.api.js's Issues

fyi similar projects

You probably know about those projects but in case someone finds this (extremly promising!) project of yours he or she might be interested to know that there are other similar projects to look at.

In the long run I think it could make sense to share a comon drupal javascript core service and domain layer among such projects with plugable system for io. e.g. gui and specific hardware connection (database, mobile device) layers like one to work with jQuery mobile, PhoneGap or Titanium.

Services needs csrf token now

Hi Travis

I do not see any support for the new csrf token (added in Services 3.4).
For some background and implementation ee https://drupal.org/node/2013781#comment-7507759
Any plans for adding that to your library ? Other then this I really would to use your library. I currently use my own custom library. Its simple and effective (and includes csrf) but I think it is not very structured. Your oo library might be the best alternative.

Handling errors on node creation (eg failed validation)

Hello,

I wonder how I could access the error message from the server that is sent back during node creation for example (eg the node failed validation).

For example

  (new drupal.node({title:"",type:"page"})).save(function(node) {
    console.log(node);
    //console.log(xhr.statusText);
  });

This call will fail of course as title is empty. It seems we only get a console.log from the API, but I want to show xhr.responseText to the user that contains the entire server message. How can I get the error message sent back from the server?

I notice this message is present in
drupal.api.js:194 (xhr)

    error: (function(api) {
      return function(xhr, ajaxOptions, thrownError) {
        api.loading(false);
        console.log(xhr.statusText);
        if (callback) {
          callback(null);
        }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.