0

I'm in Drupal 8. After activating the module rest, and try and receive a response from the route /user/login?_format=json I get this:

{
    "current_user": {
        "uid": "4",
        "name": "cravushedal"
    },
    "csrf_token": "49TlM-VLnQcErR1W5vW7losJPLHXPpqE2UpLh9_bZzg",
    "logout_token": "fkBNWh8iLoTbXPO4Elha86PVe_0I8L1JKIxXQ2KemMo"
}

Now I want to craete a view with all the users, but something like this:

{
    "current_user": {
        "uid": "3",
        "name": "blablabla"
    }     
}
{
    "current_user": {
        "uid": "4",
        "name": "cravushedal"
    }     
}

In my rest view, I have the name, uid, and a Global custom text. The name and the uid are hidden, and I am calling both fields in the global custom text. This is my result:

[{"current_user":"1josue"},{"current_user":"2slucuthivili"}]

It doesnt recognize the label, so I had try to include them manually, but the json add \n or \what ever and that destroy my json.

Any idea about how can I achieve this?? thanks!

1 Answer 1

2

When you query /user/login it defaults to currently logged-in user.

I suppose you need to create a view with a "REST export" display. The view defaults to "Entity" row style, change it to "Fields".

display settings

In "Fields" settings, you can rename the labels that are output in "Alias" fields:

Alias settings

In this "REST Export" view you will have set a path. i.e. /rest/people

Querying /rest/people?_format=json should output something like this:

[
  {
    "user_name": "admin",
    "user_id": "1",
    "user_customtext": "Custom text"
  },
  ...
]

You can use the Global - Custom text field to concat multiple fields as tokens, then hide those fields from being displayed:

enter image description here

3
  • Yes, but i want a "current_user", and inside it, the name and the uid, the custom text is for group them.
    – ValRob
    Apr 2, 2018 at 18:42
  • @ValRob I updated answer with a new screenshot. You can use the replacement tokens to achieve desired output.
    – osman
    Apr 2, 2018 at 18:52
  • Great answer, I wonder how to make it return an object instead of an array? My use case is that my view will return only one user (the current). Thanks.
    – Yaazkal
    Jan 16, 2020 at 23:44

Your Answer

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

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