How to hook drupal's actual service resources

sarath.rajan's picture

Hi Friends,

Am a newbie to service module and having a small doubt in it. I want to check an additional data while creating a node. I am using drupal's resource 'service_name/node' for creating node. But before creating I want to check the user's role also. I know that we can have our own resource using hook_service_resources and node_save(). But i want to know how to use drupal's defualt resource.

Also I want to send some additional data in this result. Normally drupal will send the newly created node link as result for this service. I want to know how can I hook this result and add some additional data with this result.

Thanks in advance for your help..


RESTServer and Services hooks

lhridley's picture


There are two hooks that exist that let you alter requests:

hook_services_request_preprocess_alter( $controller, &$args, $options) -- lets you alter arguments before they are passed to the services callback processing routine (in other words, after the request is submitted from the client, but before it is handed off to the routine that saves the data for a create or modify request). You can potentially check the user role here.

hook_services_request_postprocess_alter($controller, $args, &$results) -- lets you alter the results of the services call. You can modify the results returned from the callback routine here.

If you are using the RESTServer module of services, you can also modify header information in hook_rest_server_headers_parsed_alter(&$headers), or errors in hook_rest_server_execute_errors_alter(&$error_alter_array, $controller, $arguments).

My experience is that you don't always get the controller or arguments information from the error routine hook (it depends on when the error occurs), or the opportunity to modify the headers all the time. If processing falls to the error routine, it typically doesn't pass through hook_services_request_postprocess_alter(), so you may need to check both places, depending on what you want to alter.

These hooks are documented (sparsely) in the docs directory of the services module. I typically set up a temporary module that calls all the hooks in the Services module and use dd() to send the output of the arguments along with the function name to drupal_debug.txt when I want to hook into Services to see exactly when routines are getting called during an API call, so I can determine the best point to make my changes.

If the role checking is also occurring on the UI side, your hook to check the role before a node save may already be processing that for you, since data submitted through the services module passes through drupal form validation routines for processing, where I believe roles and permissions will be checked as well, so any hooks you have for UI forms processing may already be doing your permissions checking for you.


sarath.rajan's picture

Hi lhridley ,

Great...! This is what I was looking for. Thanks a lot for your help.

Again one more query.. From this hook, hook_services_request_preprocess_alter( $controller, &$args, $options) , I am getting the arguments. But how can i get the header value here (for example, the X-CSRF-Token from the header)? Currently I am using apache_request_headers() for getting the header values here. But is there any other solution?

Hi lhridley, Thanks a ton for

sarath.rajan's picture

Hi lhridley,

Thanks a ton for ur help.. Really what I was looking for. Anyway I will check these hook functions and let you know the result. Hope this will solve my issue. Role checking is just for an example. Actually I want to know how it can be done (preprocess alter and post process alter)..

You may have to try a couple

lhridley's picture

You may have to try a couple of different things, see what works for your use case.

drupal_get_http_header() may give you what you need.


Group organizers

Group events

Add to calendar

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: