craftcms / element-api

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

Events elementType returns Null even if there are events #155

Closed DowleyDeveloped closed 2 years ago

DowleyDeveloped commented 2 years ago

Im using the Verbb Events plugin and trying to return all events from the CMS using this code:

use Craft;
use verbb\events\elements\Event;
use verbb\events\models\EventType;

class Events {

    public static function index()
    {
        return [
            'elementType' => Event::class,
            'criteria' => [
                'type'     => Craft::$app->request->getQueryParam('section') ? : 'event',
                'offset'   => Craft::$app->request->getQueryParam('offset') ? : '0',
            ],
            'elementsPerPage' => Craft::$app->request->getQueryParam('items') ? : 10,
            'transformer' => function(Event $entry) {
                return [
                    'title' => $entry->title,
                ];
            },
        ];
    }
}

However I get no entries back. If I run $events = Event::find()->type($type)->all(); this does return information so wondering why the above doesn't work?

brandonkelly commented 2 years ago

The endpoint config should go in config/element-api.php using the format shown in this example: https://github.com/craftcms/element-api#setup

So something like:

use verbb\events\elements\Event;
use verbb\events\models\EventType;

return [
    'endpoints' => [
        'events.json' => function() {
            return [
                'elementType' => Event::class,
                'criteria' => [
                    'type' => Craft::$app->request->getQueryParam('section') ?? 'event',
                    'offset' => Craft::$app->request->getQueryParam('offset') ?? 0,
                ],
                'elementsPerPage' => Craft::$app->request->getQueryParam('items') ?? 10,
                'transformer' => function(Event $event) {
                    return [
                        'title' => $event->title,
                    ];
                },
            ];
        },
    ],
];
DowleyDeveloped commented 2 years ago

Hi,

Apologies I set up the endpoint config set up and the above goes into another file:

use config\endpoints\Collections; use config\endpoints\Events;

return [
    'endpoints' => [
        'api/collections.json' => function() {
            return Collections::index();
        },
        'api/events.json' => function() {
            return Events::index();
        },
    ]
];

I've done this to keep the file clean. The collection endpoint gets entry data and works fine, however the Events does not

brandonkelly commented 2 years ago

Ah ok. And you’re sure that $type in your working code is getting set to event or whatever the section param is set to?

DowleyDeveloped commented 2 years ago

Yes, however even without I should be seeing something as the fallbacks all exist

brandonkelly commented 2 years ago

Try removing the criteria array, and see if it works without setting any event query params.