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

CC BY-SA 3.0
2016-12-23T18:26:24.260Z
FWIW, in Craft 3 we've stop extending PHP's Date class with one of our own.
Brad Bell
CC BY-SA 3.0
2016-12-23T16:31:45.763Z
2016-12-23T16:31:45.763Z
Solspace
<p>While not the ideal solution, there seems to be enough meta information to build out the recurring events manually.</p>

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

  return [
    'id' =&gt; $event-&gt;id,
    'title' =&gt; $event-&gt;title,
    'calendarId' =&gt; $event-&gt;calendarId,
    'slug' =&gt; $event-&gt;slug,
    'startDate' =&gt; $event-&gt;startDate,
    'endDate' =&gt; $event-&gt;endDate,
    'freq' =&gt; $event-&gt;$freq,
    'interval' =&gt; $event-&gt;interval,
    'exceptions' =&gt; $exceptions,
    'byMonth' =&gt; $event-&gt;byMonth,
    'byYearDay' =&gt; $event-&gt;byYearDay,
    'byMonthDay' =&gt; $event-&gt;byMonthDay,
    'byDay' =&gt; $event-&gt;byDay
  ];
}
</code></pre>

CC BY-SA 3.0
2016-12-23T18:50:08.147Z
2016-12-23T18:50:08.147Z
aw04
<p>Looks like you've got the <a href="https://github.com/theolampert/ElementAPI#setup" rel="nofollow noreferrer">Element API syntax</a> wrong.  Try this:</p>

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

<p>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 <code>Calendar_EventModel</code>, so you'll want to verify that.  You can return any of the properties they have listed here: <a href="https://solspace.com/craft/calendar/docs/event/" rel="nofollow noreferrer">https://solspace.com/craft/calendar/docs/event/</a></p>

CC BY-SA 3.0
2016-12-22T18:20:37.373Z
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
CC BY-SA 3.0
2016-12-22T18:21:31.770Z
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
CC BY-SA 3.0
2016-12-22T18:25:14.890Z
fyi I'm using the function syntax because I have a parameter, I removed some code for the sake of a simple example :)
aw04
CC BY-SA 3.0
2016-12-22T18:29:40.260Z
Updated the question to hopefully be more clear
aw04
CC BY-SA 3.0
2016-12-22T18:39:28.413Z
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
CC BY-SA 3.0
2016-12-22T20:50:20.873Z
Oh cool thanks. There's a param loadOccurrences to the template function here, but I'm not sure how it relates to the element api. It doesn't seem to respect it. https://solspace.com/craft/calendar/docs/calendar.events
aw04
CC BY-SA 3.0
2016-12-22T18:10:35.233Z
2016-12-22T18:10:35.233Z
Brad Bell
<p>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:</p>

<pre><code>{% for event in craft.calendar.events %} ...

Meeting Event
Meeting Event
Meeting Event
...
</code></pre>

<p>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:</p>

<pre><code>&lt;?php
namespace Craft;

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

<p>And it only returns:</p>

<pre><code>Meeting Event
</code></pre>

<plugin-elementapi><plugin-solspacecalendar>
Get repeated events with the Solspace Calendar and Element API plugins
# Get repeated events with the Solspace Calendar and Element API plugins

(Asked by: username_0 on 2016-12-22)

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:
 [
    'api/events.json' => function() {
      return [
        'elementType' => 'Calendar_Event'
      ];
    }
  ]
];

And it only returns:
Meeting Event

**Tags:** plugin-elementapi, plugin-solspacecalendar

---

## 3 answers

### Answer by username_1 on 2016-12-23. Score: 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 :/

> **Comments:**
> **username_2**: FWIW, in Craft 3 we've stop extending PHP's Date class with one of our own.  

---

### Answer by username_0 on 2016-12-23. Score: 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
  ];
}


---

### Answer by username_2 on 2016-12-22. Score: 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/

> **Comments:**
> **username_0**: 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.  
> **username_0**: 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  
> **username_0**: fyi I'm using the function syntax because I have a parameter, I removed some code for the sake of a simple example :)  
> **username_0**: Updated the question to hopefully be more clear  
> **username_2**: 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.  
> **username_0**: Oh cool thanks. There's a param loadOccurrences to the template function here, but I'm not sure how it relates to the element api. It doesn't seem to respect it. https://solspace.com/craft/calendar/docs/calendar.events  

---

