3

I want to list all the related entries to the entry I am displaying using the Element API plugin.

I have a channel called series and another called episodes, the episodes have a relationship with 1 series.

I am pulling in the series ok:

'api/series.json' => [
    'elementType' => 'Entry',
    'criteria' => ['section' => 'series'],
    'transformer' => function(EntryModel $entry) {
        return [
            'title' => $entry->title,
            'url' => $entry->url,
            'jsonUrl' => UrlHelper::getUrl("api/series/{$entry->id}.json")
        ];
    },
],

Then I am pulling in a series:

'api/series/<entryId:\d+>.json' => function( $entryId) {
        return [
            'elementType' => 'Entry',
            'criteria' => [
                'id' => $entryId
            ],
            'first' => true,
            'transformer' => function(EntryModel $entry) {
                return [
                    'title' => $entry->title,
                    'url' => $entry->url,
                    'image' => $entry->seriesHero,
                    'description' => $entry->seriesDescription
                ];
            },
        ];
    },

In the endpoint above I want to be able to output an array of the episodes related to this series. The relationship field in the episode channel is called series.

What is the best way to do this?

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.