2

Is there a good solution to CRUD a user's Profile2 fields via Services? I assume this can be done with Services Entity API, but I wonder if there is a more direct integration?

With Services Entity API, I enabled the entity_profile2 resource and can load an index of profiles. I need to disable the ability to index the profiles but allow a user to retrieve their own profile.

From the docs, this URL should be able to find user with uid=296's profile:

api/entity_profile2.json?parameters[uid]=296

But the result is an empty page.

3 Answers 3

1

I found that I had to use pid: /api/entity_profile2?parameters[pid]=1

1
  • This does answer the question at least partially, the Services Entity API is a good CRUD solution, the OP was just using the wrong format to request data...
    – DrCord
    Commented Apr 15, 2014 at 18:20
0

For anyone still looking for this, I had to enable the index option (not just retrieve), and create an alias (without a number in it) under entity_profile2 for this to work.

0

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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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