3

I am creating REST API using Services Module in Drupal 7.

I have created a View to Get lists of Groups. In this views, I have added a Field privacy which will tell if the group is Private or Public.

I am getting this Output if group is Private:

privacy":{"value":"1"},

and if the group is Public :

"privacy":[]

How can I edit the output of this field so that it become easier for app developer ? I want to have something like this in output :

For Private : privacy":{"value":"1"}, 
For Public  : privacy":{"value":"0"},

Or something like this which look good.

  • 2
    Give this a try: In views click on your privacy field and in NO RESULTS BEHAVIOR put 0 if that doesn't work then try putting {"value":"0"}, – No Sssweat Apr 24 '16 at 1:16
  • 1
    which module are you using for Field privacy? Is that the User Field Privacy module? – Krishna Mohan May 4 '16 at 10:46
0

From the code you can use hook_services_request_postprocess_alter() to alter whatever you want from your response.

You can always use arguments to make your changes more specific:

function my_module_services_request_postprocess_alter($controller, $args, &$result) {
  // In my response, I include the request string in the '_links' array.    
  // I can use this now to determine which resource was requested.  
  if (strpos($result->_links['self']['href'], 'api/1.0/widget')) {
    // Now I know I'm dealing with a widget request, so I'll
    // add the random string here...
    $result->String = "This is a string that I'm adding to the widget response";
  }
}

As result you can see in your response:

Array (
  [_links] => Array (
    [self] => Array (
      [href] => /api/1.0/widget/12345
    )
  )

  [count] => 1
  [_embedded] => Array (
    [0] => Array (
      [id] => 12345
      [type] => widget
      [path] => /node/157601
    )
  )
  [String] => 'This is a string that I'm adding to the widget response'
)

Read more Drupal: Alter API response with Services API, where is this example was taken from.

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.