4

How should I go about passing multiple parameters to the Element API?

The scenario is that I would like to display all my categories for a given section in a form, and allow the user to select categories, and send them to the API. I would like to receive back a json object of all the entries that have the categories assigned.

I am aware that I can dynamically catch url segments, but I'm not understanding how I would pass something like a GET parameter as a segment.

Thanks for the help!

7

You can fetch GET parameters using HttpRequestService::getQuery() or getRequiredQuery():

'entries-by-category' => function() {
    $categoryIds = craft()->request->getRequiredQuery('categoryIds');

    return [
        'elementType' => 'Entry',
        'criteria' => [
            'relatedTo' => ['targetElement' => $categoryIds],
        ],
        // ...
    ];
},
| improve this answer | |
  • 1
    Is there a way to require that the returned entries have all the categories assigned to them? With the code above it returns all entries that have at least one of the specified categories. (Should I be asking this as a new SE question?) Thanks! – Jesse Schutt Sep 8 '15 at 19:58
  • Because I'm a little slow, can anyone provide an example of a full endpoint config using this method? I'm trying to do something similar but can't figure out how to implement this function. – anguiac7 Feb 24 '17 at 17:36
  • @JesseSchutt (sorry just a little late here :) - yes, just set 'and' as the first item in the $categoryIds array. – Brandon Kelly Feb 27 '17 at 19:24
  • 1
    @anguiac7 that is the full endpoint config – entries-by-category is the endpoint’s URI pattern. If you need additional help it will probably be easier if you post as a new question. – Brandon Kelly Feb 27 '17 at 19:26
  • @BrandonKelly ya know, that makes complete sense now. I made a new question here. I'll give it a shot now that I know that's the full config, though, and close the Q if that solves it. – anguiac7 Feb 27 '17 at 19:27
1

For those curious how to make sure that all categories match (and instead of or):

'entries-by-category' => function() {
    $categoryIds = craft()->request->getRequiredQuery('categoryIds');

    return [
        'elementType' => 'Entry',
        'criteria' => [
            'relatedTo' => ['targetElement' => ['and', 123, 234, 567]
        ],
        // ...
    ];
},

You can even have more related conditions going on and add the and relationship as well like that:

'entries-by-category' => function() {
    $categoryIds = craft()->request->getRequiredQuery('categoryIds');

    return [
        'elementType' => 'Entry',
        'criteria' => [
            'relatedTo' => [
                'and',
                ['targetElement' => ['and', 22, 33]],
                ['targetElement' => ['and', 123, 234]]
            ]
        ],
        // ...
    ];
},
| improve this answer | |

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.