craftcms / element-api

Create a JSON API/Feed for your elements in Craft.
MIT License
498 stars 57 forks source link

Is there a way to set the criteria based on a custom field? #8

Closed sandy-abrightmind closed 8 years ago

sandy-abrightmind commented 8 years ago

Is there any way to set the criteria based on a custom field?

Channel: Offers Custom Field: Users

Can I query based on the Users which is a field that I added to my channel of "Offers"?

timkelty commented 8 years ago

users is a "Users" field? Do you want it do match ANY offers which have users?

sandy-abrightmind commented 8 years ago

Yeah, basically, I have a channel of Offers. And each offer can be assigned to multiple users.

Then I want to query by “user” all the offers that it has. Is that possible with the plugin?

Thanks,

Sandy Davis

Sandra Davis A Bright Mind, Inc. Biloxi, MS

From: Tim Kelty notifications@github.com<mailto:notifications@github.com> Reply-To: pixelandtonic/ElementAPI reply@reply.github.com<mailto:reply@reply.github.com> Date: Monday, April 4, 2016 at 11:25 AM To: pixelandtonic/ElementAPI ElementAPI@noreply.github.com<mailto:ElementAPI@noreply.github.com> Cc: Sandra Davis sandy@abrightmind.org<mailto:sandy@abrightmind.org> Subject: Re: [pixelandtonic/ElementAPI] Is there a way to set the criteria based on a custom field? (#8)

users is a "Users" field? Do you want it do match ANY offers which have users?

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHubhttps://github.com/pixelandtonic/ElementAPI/issues/8#issuecomment-205375796

sandy-abrightmind commented 8 years ago

And Yes, I’m using a “Users” field type for the custom field Users.


Sandra Davis A Bright Mind, Inc. Biloxi, MS

From: Tim Kelty notifications@github.com<mailto:notifications@github.com> Reply-To: pixelandtonic/ElementAPI reply@reply.github.com<mailto:reply@reply.github.com> Date: Monday, April 4, 2016 at 11:25 AM To: pixelandtonic/ElementAPI ElementAPI@noreply.github.com<mailto:ElementAPI@noreply.github.com> Cc: Sandra Davis sandy@abrightmind.org<mailto:sandy@abrightmind.org> Subject: Re: [pixelandtonic/ElementAPI] Is there a way to set the criteria based on a custom field? (#8)

users is a "Users" field? Do you want it do match ANY offers which have users?

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHubhttps://github.com/pixelandtonic/ElementAPI/issues/8#issuecomment-205375796

timkelty commented 8 years ago

In that case, something like this should work:

<?php
namespace Craft;

return [
    'endpoints' => [
        'users/<userId:\d+>/offers.json' => function($userId) {
            return [
                'elementType' => 'Entry',
                'criteria' => [
                  'section' => 'offers',
                  'relatedTo' => $userId,
                ],
                'transformer' => function(EntryModel $entry) {
                    return [
                        'title' => $entry->title,
                        'url' => $entry->url,
                        'summary' => $entry->summary,
                        'body' => $entry->body,
                    ];
                },
            ];
        },
    ]
];

Your endpoint would be /users/[userID]/services.json

sandy-abrightmind commented 8 years ago

It’s not returning any data. =(


Sandra Davis A Bright Mind, Inc. Biloxi, MS

From: Tim Kelty notifications@github.com<mailto:notifications@github.com> Reply-To: pixelandtonic/ElementAPI reply@reply.github.com<mailto:reply@reply.github.com> Date: Monday, April 4, 2016 at 1:00 PM To: pixelandtonic/ElementAPI ElementAPI@noreply.github.com<mailto:ElementAPI@noreply.github.com> Cc: Sandra Davis sandy@abrightmind.org<mailto:sandy@abrightmind.org> Subject: Re: [pixelandtonic/ElementAPI] Is there a way to set the criteria based on a custom field? (#8)

/users/[userID]/services.json

timkelty commented 8 years ago

Try it again, I was missing a comma. Also, try turning on devMode, it would catch errors like that.

sandy-abrightmind commented 8 years ago

No I mean the JSON has no data. It's not finding any related content. Even though, there are entries that have the user name "test".

I even changed it to look it up based on the username handle for the "related to" and endpoint configuration & that didn't work either.

I'm not sure what it's not finding the entries. :(

Get Outlook for mobilehttps://aka.ms/b4fz91


From: Tim Kelty notifications@github.com<mailto:notifications@github.com> Sent: Monday, April 4, 2016 2:43 PM Subject: Re: [pixelandtonic/ElementAPI] Is there a way to set the criteria based on a custom field? (#8) To: pixelandtonic/ElementAPI elementapi@noreply.github.com<mailto:elementapi@noreply.github.com> Cc: Sandy Davis sandy@abrightmind.org<mailto:sandy@abrightmind.org>

Try it again, I was missing a comma. Also, try turning on devMode, it would catch errors like that.

You are receiving this because you authored the thread. Reply to this email directly or view it on GitHubhttps://github.com/pixelandtonic/ElementAPI/issues/8#issuecomment-205464145

brandonkelly commented 8 years ago

Usage questions should be posted on http://craftcms.stackexchange.com (tagged with plugin-elementapi). This is not actually an issue with the plugin.

sandy-abrightmind commented 8 years ago

Sorry. I saw a label for questions so I thought I could post the question on the project page. I’ll post it on the stack exchange link below.

Thank you!

Sandra Davis A Bright Mind, Inc. Biloxi, MS

From: Brandon Kelly notifications@github.com<mailto:notifications@github.com> Reply-To: pixelandtonic/ElementAPI reply@reply.github.com<mailto:reply@reply.github.com> Date: Monday, April 4, 2016 at 5:51 PM To: pixelandtonic/ElementAPI ElementAPI@noreply.github.com<mailto:ElementAPI@noreply.github.com> Cc: Sandra Davis sandy@abrightmind.org<mailto:sandy@abrightmind.org> Subject: Re: [pixelandtonic/ElementAPI] Is there a way to set the criteria based on a custom field? (#8)

Usage questions should be posted on http://craftcms.stackexchange.com (tagged with plugin-elementapi). This is not actually an issue with the plugin.

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHubhttps://github.com/pixelandtonic/ElementAPI/issues/8#issuecomment-205529735