1

How do I use the elementapi to get the list of entries for the logged in user?

I've tried a couple of options to no avail, like

'api/v1/user/<userId:\d+>.json' => function($userId) {

            $author = craft()->users->getUserById($userId);

            return [
                'elementType' => 'Entry',
                'criteria' => [
                    'section' => 'jobs',
                    'author' => $author
                ],
...

as an example of one I thought should have worked but it just returns everything.

1 Answer 1

2

If you really want the entries for the currently logged in user, having a parameter for userId in your endpoint URL doesn't really make sense.

Not tested, but something like this should work:

'api/v1/user.json' => function () {

    $author = craft()->userSession->getUser();

    if (!$author) {
        HeaderHelper::setHeader(array('status' => 404));
        craft()->end();
    }

    return [
        'elementType' => ElementType::Entry,
        'criteria' => [
            'section' => 'jobs',
            'authorId' => $author->id,
        ],
    ];
}

Notice that the above example endpoint would be available at http://example.com/api/v1/user.json, where your example code would create an endpoint with a required userId parameter, i.e. http://example.com/api/v1/user/12345.json.

Also notice that the currently logged in user is pulled via the UserSessionService (craft()->userSession).

Finally, note that you should use 'authorId' => $author->id in your criteria – not 'author' => $author.

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.