Fork me on GitHub

Often times, we'll need custom Services on our Drupal site for our app to communicate with. Using the Services 3.X API and a custom Drupal module, we can accomplish this.

1. Create a Custom Drupal Module

Create a directory for your custom Drupal module on your Drupal site:


Create these empty files in the new directory:

  • my_module.module

Place this code in the file:

name = My Module
description = My custom Drupal module.
package = Other
core = 7.x
dependencies[] = services
dependencies[] = rest_server


Place this code in the my_module.module file:


 * Implements hook_ctools_plugin_api().
function my_module_ctools_plugin_api($owner, $api) {
  if ($owner == 'services' && $api == 'services') {
    return array(
      'version' => 3,
      'file' => ''

Place this code in the file:


function my_module_services_resources() {
  $resources = array(
    'my_module_resources' => array(
      'actions' => array(
        'get_user_count' => array(
          'help' => t('Gets a count of users.'),
          'file' => array(
            'type' => 'inc',
            'module' => 'my_module',
            'name' => 'my_module.resource',
          'callback' => 'my_module_get_user_count',
          'args' => array(),
          'access callback' => '_drupalgap_resource_access',
          'access callback file' => array(
            'type' => 'inc',
            'module' => 'drupalgap',
            'name' => 'drupalgap.resource',
          'access arguments' => array('access user profiles'),
          'access arguments append' => TRUE,
  return $resources;

Place this code in the file:


function my_module_get_user_count() {
  return db_query("SELECT COUNT(uid) FROM {users}")->fetchField();

2. Enable the Custom Drupal Module

Go to admin/modules on your Drupal site, and enable My Module.

3. Give Anonymous Users Access to the Resource

On your Drupal site:

  1. Go to admin/people/permissions
  2. Check the box next to View user profiles under the Anonymous User column
  3. Click the Save permission button.

4. Enable the Custom Service Resource

On your Drupal site:

  1. Go to admin/structure/services/list/drupalgap/resources
  2. Expand the my_module_resources under the Resource column
  3. Check the box next to get_user_count
  4. Click the Save button at the bottom of the form.
  5. Flush all of Drupal's Caches at admin/config/development/performance

5. Test the Custom Service Resource

Our new Custom Service Resource will be available at a URL like this:

Let's use FireFox Poster to test it out:

  1. Open FireFox
  2. Logout of your Drupal site (if you're logged in)
  3. Open FireFox Poster under Tools -> Poster
  4. Enter the URL listed above into the URL field, replacing with your domain
  5. Click the Body from Parameters button
  6. Click the POST button

If all goes well, we'll get a 200 OK response, and we'll be returned the numbers of users on our site, inside a JSON array. For example, if our site had 33 users:


If you get a "CSRF validation failed" response, you need to log out of your Drupal site in FireFox before using Poster, or you can use this technique to attach the X-CSRF-Token header.

That's it! You now have a Custom Service Resource on your Drupal website that the mobile app can communicate with.