Neither of the above answers worked for me. Hitting api/entity_profile2.json
, whether you add query parameters or not, is going to get the index of profiles, which you will have switched off and therefore will return a 404 "resource not found" error. You need to hit the retrieve route with an id like with all other services, but you'll need to get the profile id first. You want to GET api/entity_profile2/1.json
, where 1 is the profile id- not the user id.
It is possible of course to get the index and then filter down based on uid, but this isn't the best RESTy option and seems a bit convoluted. Also, a big issue is that when you hit api/user/1.json
(for example), it doesn't give any details about user fields, including any associated profile2 profile ids.
I decided the best way was to hook into the service call postprocess in a custom module and add the profile2 data to it. You could either add just the profile ids to the user object, then from that use the entity_profile service as mentioned above, or you can just tack the whole entity onto the user object and not have to even bother with services_entity at all. Code as follows:
function mymodule_services_request_postprocess_alter($controller, $args, &$results) {
if (isset($results->uid) && $results->uid > 0) {
$profile2 = profile2_load_by_user($results);
if ($profile2) {
$results->profile2 = array_values(profile2_load_by_user($results));
}
}
}
... which results in something like this when getting api/user/1.json
{
"uid": "1",
"name": "admin",
"mail": "[email protected]",
"theme": "",
"signature": "",
"signature_format": null,
"created": "1441810121",
"access": "1450698782",
"login": "1450695398",
"status": "1",
"timezone": "UTC",
"language": "",
"picture": "0",
"init": "[email protected]",
"data": false,
"uuid": "82919e2f-e431-44e6-b4b1-8d13c5555c94",
"roles": {
"2": "authenticated user"
},
"profile2": [
{
"pid": "1",
"type": "main",
"label": "Main profile",
"uid": "1",
"created": "1450696479",
"changed": "1450696479",
"field_first_name": {
"und": [
{
"value": "Matt",
"format": null,
"safe_value": "Matt"
}
]
},
"field_last_name": {
"und": [
{
"value": "Fletcher",
"format": null,
"safe_value": "Fletcher"
}
]
}
}
]
}
How you format it is up to you of course, there are many ways of doing things; but this was the easiest method for me and doesn't require as many REST calls as other solutions :)