1

I'm using the Element API to grab entries and their data (via this answer). What I want to do is accept a URL parameter (eg. ?location=foo) and filter the returned entries by a Matrix field called fundingProgramme, where its sub-field area (a dropdown) has a value matching the URL query parameter.

I can't work out how to do this using the Element API. Here's what I've got:

'content/<locale:en|cy>/programs.json' => function($locale) {

    $siteId = ($locale === 'en' || !$locale) ? 1 : 2;

    return [
        'elementType' => Entry::class,
        'criteria' => [
            'section' => 'fundingProgrammes',
            'siteId' => $siteId
        ],
        'transformer' => function(Entry $entry) {
            $request = Craft::$app->getRequest();
            $location = $request->getParam('location');
            return [
                'title' => $entry->title,
                'test' => $entry->fundingProgramme[0]->area->label
            ];
        },
    ];

(the test key at the end is me proving that I can retrieve the field programmeTitle from the Matrix field fundingProgramme. There's only one stored against each entry, though).

How can I query against this field? Would I have to do this in the transformer rather than the criteria?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.