1

I currently try to use the Element API to build a search. Unfortunately I'm kind of stuck.

I have a baseEntry, that has a field that references entries (let's call them subEntries) of the section subEntries, where subEntries have a field that references other entries (let's call those categoryEntries), where categoryEntries (for simplicity) only have a title.

Now I'm trying to output all baseEntrys that contain one ore more subEntrys which have one or more categoryEntrys with the title of my query... I tried to do this via endpoint.json?section=baseEntry&q=subEntryHandleFromBaseEntry:*:categoryEntryHandleFromSubEntry:mySearchTerm but that didn't work using

'endpoint.json' => function() {
    return [
        'elementType' => Entry::class,
        'paginate' => false,
        'one' => true,
        'transformer' => function(Entry $entry) {

            $items = [];
            $section = Craft::$app->getRequest()->getParam('section');
            $param = Craft::$app->getRequest()->getParam('q');
            if ($param && $section) {


                $entries = \craft\elements\Entry::find()
                ->search($param)
                ->section($section)
                ->all();

                foreach ($entries as $entry) {
                    $items[] = UrlHelper::url("{$entry->type->handle}/{$entry->id}.json");
                }
            }

            return [
                $items
            ];
        },
    ];
},

as endpoint.

Also tried narrowing it down like:

$entries = \craft\elements\Entry::find()
->section($section)
->all();

foreach ($entries as $entry) {
    $items = \craft\elements\Entry::find()
    ->search($param)
    ->relatedTo($entry)
    ->all();

    foreach ($items as $item) {
        $items[] = UrlHelper::url("{$item->type->handle}/{$item->id}.json");
    }
}

didnt work either. I'm kind of lost. I'm looking into the docs but can't seem to get it working.

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.