2

I've enabled the REST server (plus services) and can retrieve user profile data at www.example.com/endpoint/user/123

How can I customize the data that's being retrieve? I want to not include the email address and add a couple of other fields.

When I make a login request at www.example.com/endpoint/user/login I would like to have those additional fields included as well.

  • how you enable REST server module while its not available or Drupal 7? "the Drupal 7 port of the REST Server is already packaged with the Services module" – Mohammad Ali Akbari Apr 21 '12 at 1:44
5
+50

All you need is to define your Resource, take a look at files in site/all/module/services/resources, you can find some examples, but for simplicity I will take you and example:

endpoint_user moduel:

endpoint_user.info file:

name = endpoint_user
description = endpoint_user
core = 7.x

endpoint_user.module file:

<?php
function endpoint_user_services_resources() {
  $endpoint_user_resource = array(
      'endpoint_user' => array(
          'retrieve' => array(
              'callback' => '_endpoint_user_resource_retrieve',
              'args' => array(
                  array(
                      'name' => 'uid',
                      'optional' => FALSE,
                      'source' => array('path' => 0),
                      'type' => 'int',
                      'description' => 'The uid of the endpoint_user to get',
                  ),
              ),
              'access callback' => '_endpoint_user_resource_access',
              'access arguments' => array('view'),
              'access arguments append' => TRUE,
          ),
      ),
  );
  return $endpoint_user_resource;
}

function _endpoint_user_resource_access($op = 'view', $args = array()) {
  return TRUE;
}

function _endpoint_user_resource_retrieve($uid) {
  $endpoint_user = user_load($uid);
  $endpoint_user->custom_field = 'Mohammad Ali Akbari';
  if ($endpoint_user) {
    $uri = entity_uri('user', $endpoint_user);
    $endpoint_user->path = url($uri['path'], array('absolute' => TRUE));
    // Unset uri as it has complete entity and this
    // cause never ending recursion in rendering.
    unset($endpoint_user->uri);
  }
  return $endpoint_user;
}

then create new Service in admin/structure/services and select endpoint_user.

open your browser and go to your service URL, like endpoint/endpoint_user/1

you will see:

<custom_field>Mohammad Ali Akbari</custom_field>

This result comes from $endpoint_user->custom_field = 'Mohammad Ali Akbari'; in _endpoint_user_resource_retrieve

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.