Services
Blog

How to Create Custom Web Services for Drupal 7

Web based service applications/web services integrate different network platforms, applications, softwares and various other services to help you enjoy a seamless experience. Custom Web services allow you to display content from one site to another using the Services module.

In one of the recent project, we need to exposes value of filed collection items of a node via API. But default services provides only node values. THere Default services module does not provide the values from field collection items of a node while written custom web services can be used to fetch paired values of field collection items. I have created a Custom Web Service to get the Field Collection item value. I began with creating one content type “Housing Scheme” with the fields listed below :

# Title (Text)
# About Scheme ( Long Text)
# Distance from the City (Text)
# Nearest Major City ( Text)
# Locality Review( Long Text)
# Property Description ( Field Collection)

For this purpose, I used Drupal 7 and the Services module. I have written a custom module to handle the back end. The first step would be to download and install the Services module and enable the REST Server module that comes together with it.

Step I : Add service

To create a service you have to navigate to Service UI (admin/structure/services) and then click add service.

Add service

Step II : Open the Resources Page

The Open Resources Page will show a service list after you have saved a new service in it.

Now edit resources.

Service list page

Resources are available in default setup where all resources come hard-coded with the Services module.

Drupal Resources List

Step III : Create Custom Module

As my resources are not showing yet I have created a custom module for the same:

# Created custom module “housing_schemes_services”.
# Created directory for custom Drupal module in my Drupal site: /www/sites/all/modules/housing_schemes_services

I have empty files in my module directory:

  • housing_schemes_services.info
  • housing_schemes_services.module
  • includes/housing_schemes.services.inc

Step IV: Create the .info file

I added this code in housing_schemes.info file:

name = Housing Schemes Services
description = WebService APIs for Housing Schemes.
core = 7.x
package = Housing Schemes
project = housing_schemes
dependencies[] = services
dependencies[] = rest_server

 

Step V : Create the .module file

Then I added this code in housing_schemes_services.module:

/**
 * @file
 * Module file for Housing Schemes Services.
 * Contains the resource declarations for the service APIs
 * and other commons functions/hooks. if necessary
 */

/**
 * Implements hook_services_resources().
 * Create service resource for APIs to return the necessary json data.
 */
function housing_schemes_services_services_resources() {
  $resources = array();

//Include the necessary inc files.
  module_load_include('inc', 'housing_schemes_services', 'includes/housing_schemes.services');

//Service Apis for contents.
  $resources += housing_schemes_services_resource();

  return $resources;
}

Step VI : Create the .inc file

Finally I added this code in the housing_schemes.services.inc file:

function housing_schemes_services_resource() {
  $api = array(
	'housing_schemes' => array(
  	'operations' => array(
    	'retrieve' => array(
      	'help' => 'Retrieves housing schemes info.',
      	'file' => array(
        	'type' => 'inc',
        	'module' => 'housing_schemes_services',
        	'name' => 'includes/housing_schemes.services',
      	),
      	'callback' => 'housing_schemes_services_resource_retrieve',
      	'access callback' => 'user_access',
      	'access arguments' => array('access content'),
      	'access arguments append' => FALSE,
      	'args' => array(
        	array(
          	'name' => 'nid',
          	'type' => 'int',
          	'description' => 'Function to perform',
          	'source' => array(
            	'path' => '0'
          	),
          	'optional' => TRUE,
          	'default' => '0',
        	),
      	),
    	),
      	),
	),
  );

  return $api;
}

Created callback function retrieve

/**
 * [housing_schemes_services_resource_retrieve] definition.
 * Returns the information about a housing scheme.
 * @param $nid
 *   The nid of the housing_scheme node.
 * @return array
 *	The node processed information array.
 */
function housing_schemes_services_resource_retrieve($nid) {
  $node = node_load($nid);
  //Check if the node type is housing schemes.
  if ($node->type == 'housing_schemes') {
//Get PROPERTY DESCRIPTION data from field collection.
$property_description = $node->field_property_description[LANGUAGE_NONE][0][‘value’];
// here we are loading field collection
$pd_item = field_collection_item_load($property_description);

//getting field collection item value.
$pd_item_values = array(
  ‘scheme_type’ => $pd_item->field_scheme_type[LANGUAGE_NONE][0][‘value’],
  ‘area’ => $pd_item->field_area[LANGUAGE_NONE][0][‘value’],
  ‘property_type’ => $pd_item->field_property_type[LANGUAGE_NONE][0][‘value’],
  ‘budget’ => $pd_item->field_budget[LANGUAGE_NONE][0][‘value’],
);

	//PROPERTY DESCRIPTION ENDS.



//Create return object.
$return_obj = array(
  'scheme_name' => $node->title,
  'scheme_id' => $node->nid,
  'created_date' => $node->created,
  'about_scheme'=>$node->field_about_scheme[LANGUAGE_NONE][0]['value'],
  'distance_from_the_city'=>$node->field_from_city[LANGUAGE_NONE][0]['value'],
  'nearest_major_city' => $node->field_nearest_city[LANGUAGE_NONE][0]['value'],
  'locality_review' =>$node->field_locality_review[LANGUAGE_NONE][0]['value'],  
'property_description' => $pd_item_values,
	);
  }

  return $return_obj;
}

Step VII : Enable the Custom Drupal Module

Enable custom Module "Housing Scheme Services".

Enabling housing scheme services

Step VIII : Enable the Custom Service Resource

On Drupal site:

  1. Go to admin/structure/services/list/housing_scheme/resources
  2. Expand the "housing_schemes" under the "Resource" column then check the box and click save.

Custom resource

Test the Custom Service Resource

After enabling the resource you will be able to query the service by navigating to this path :

http://localhost/drupal/cf_api/housing_scheme/{{nodeid}}.json

It will return the Housing Scheme’s node details so that you are able to get field collection item values in your Services. You should be able to use JSON dump to consume the data in your application.

We at Valuebound are committed to creating exceptional web experiences and solutions based on your unique business needs. For further information on our service offerings, please Contact Us.