Creating custom services

By Anonymous (not verified), 11 April, 2016
web services

 

Web services allow us to provide information to a device over an internet connection, such as lists of contents, entities, users, and more. In Drupal, we use the Services Module to perform these actions. You can explore an example in this blog: [Creando contenido con Rest Services].

 

In this guide, we will create a custom module to define a service that returns a list of users.

 

web services

 

Module Definition: rootstack_services_custom.info

 
name = Rootstack Custom Services description = My custom Rootstack Drupal module service. package = Rootstack core = 7.x dependencies[] = services dependencies[] = rest_server

 

Module Implementation: rootstack_services_custom.module

The module begins by defining permissions using hook_permission():

/** * Implements hook_permission(). */ function rootstack_services_custom_permission() {  return array(    'rootstack get users' => array(      'title' => t('Retrieve a list of users.'),    ),  ); }

 

Service Resources: rootstack_services_custom.services.inc

Define the custom service using hook_services_resources():

/** * Implementation of hook_services_resources(). */ function rootstack_services_custom_services_resources() {  return array(    'rootstack_services_users' => array(      'retrieve' => array(        'file' => array(          'type' => 'inc',          'module' => 'rootstack_services_custom',          'name' => 'rootstack_services_custom.resources',        ),        'help' => t('Gets a count of users.'),        'callback' => 'rootstack_services_custom_get_user',        'args' => array(          array(            'name' => 'uid',            'type' => 'string',            'optional' => TRUE,            'source' => array('param' => 'uid'),            'description' => t('User ID.'),          ),        ),        'access arguments' => array('rootstack get users'),      ),    ),  ); }

 

Callback Function: rootstack_services_custom.resources.inc

The callback function retrieves the list of users, with an optional filter by user ID (uid):

/** * Callback function to retrieve the list of users. */ function rootstack_services_custom_get_user($uid) {  $query = db_select('users', 'f');  $query->fields('f', array('name', 'mail'));  if ($uid) {    $query->condition('f.uid', $uid, '=');  }  return $query->execute()->fetchAll(); }

 

Configuration Steps

  1. Activate the Module
    Navigate to Admin → Modules and activate the custom module.
  2. Assign Permissions
    Go to Admin → People → Permissions and assign the rootstack get users permission.
  3. Generate the Service
    • Navigate to Admin → Structure → Services.
    • From the Resources tab, select the newly created resource.

 

Creating custom services in Drupal is a powerful and straightforward process. It allows not only database queries but also integration with tools like Solr indexing. Furthermore, it provides fine-grained permission control for each service, enhancing both flexibility and security.

 

This approach is an essential skill for extending Drupal's functionality to meet unique project needs.

 

We recommend you this video

Thumbnail
Image
web services
Weight
0
Hero
Title
Creating custom services
Image
Image
Text Color
White
Text Alignment
Left
Size
Medium
Overlay effect
Hide overlay effect
Date
Premium
No