1

When looping through all events in Twig, repeated events will continuously come in. On a weekly event, for instance, it pulls one for this week, one for next week, etc. Lets say I have a meeting event set to repeat weekly, it would come back like this:

{% for event in craft.calendar.events %} ...

Meeting Event
Meeting Event
Meeting Event
...

I'm looking to load events asynchronously with the Element API plugin but I'm only able to get back each event once (even the repeated ones). How do I achieve the same behavior? Here is the basic setup:

<?php
namespace Craft;

return [
  'endpoints' => [
    'api/events.json' => function() {
      return [
        'elementType' => 'Calendar_Event'
      ];
    }
  ]
];

And it only returns:

Meeting Event
3

Unfortunately this won't be possible with Solspace Calendar since we are extending ElementCriteria with Calendar_EventCriteriaModel, which has a layer on top of it allowing us to build recurring events and return a padded list of events. We also return Event objects instead of EventModel objects, because working with Craft Dates makes it difficult/impossible for Calendar to have the dates it needs. Sorry :/

  • 1
    FWIW, in Craft 3 we've stop extending PHP's Date class with one of our own. – Brad Bell Dec 23 '16 at 18:26
1

While not the ideal solution, there seems to be enough meta information to build out the recurring events manually.

'transformer' => function(Calendar_EventModel $event) {
  $exceptions = [];
  foreach($event->exceptions as $exception) {
    $exceptions[] = $exception->date;
  }

  return [
    'id' => $event->id,
    'title' => $event->title,
    'calendarId' => $event->calendarId,
    'slug' => $event->slug,
    'startDate' => $event->startDate,
    'endDate' => $event->endDate,
    'freq' => $event->$freq,
    'interval' => $event->interval,
    'exceptions' => $exceptions,
    'byMonth' => $event->byMonth,
    'byYearDay' => $event->byYearDay,
    'byMonthDay' => $event->byMonthDay,
    'byDay' => $event->byDay
  ];
}
0

Looks like you've got the Element API syntax wrong. Try this:

return [
    'endpoints' => [
        'api/events.json' => [
            'elementType' => 'Calendar_Event',
            'transformer' => function(Calendar_EventModel $event) {
                return [
                    'title' => $event->title,
                ];
            },
        ],
    ]
];

Note that I don't have the Calendar plugin, so I can't test this, and I'm guessing that their event model class is called Calendar_EventModel, so you'll want to verify that. You can return any of the properties they have listed here: https://solspace.com/craft/calendar/docs/event/

  • The code I have gives the same result as yours (with different fields of course as I didn't specify a transformer..), if there's an error I don't see it. The issue isn't with the fields, it's the number of records returned. – aw04 Dec 22 '16 at 18:20
  • I found the event model class looking in the craft_elements table, didn't see it in the documentation but it's the only option I see related to the Calendar plugin so I assume it is correct – aw04 Dec 22 '16 at 18:21
  • fyi I'm using the function syntax because I have a parameter, I removed some code for the sake of a simple example :) – aw04 Dec 22 '16 at 18:25
  • Updated the question to hopefully be more clear – aw04 Dec 22 '16 at 18:29
  • Ahh, my bad. Forgot you could specify a closure for an endpoint. Will see if I can get a copy of the plugin from Solspace. – Brad Bell Dec 22 '16 at 18:39

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.