I have a simple list of events implemented as flex objects. Some of them are recurring events, so the have to be displayed multiple times at the page, even as the only exist once in the storage. How can I achive this?
What I have tried so far is:
- get all recurring events ordered by date with
{% set recurringEvents = collection.filterBy({ recurring: true}).sort({begindate: 'ASC'}).copy() %} - loop over all events
%{ for event in recurringEvents %}and adjust theirbegindateby adding for example one month. - update their property
{% do event.setProperty('begindate', newbegindate %} - add them back to the original collection with
{% do collection.add(event) %}
This works as long as a recurring event does not need to be in the time span (e.g. one year) multiple times. If it would occur multiple times (e.g. every month) the event can be added multiple times to the collection, but only by reference, so all events then all share the properties of the last processed event.
Calling event.clone() as suggested in the documentation does not work, it simply does nothing and event.createCopy() throws an error in twig:
"Cannot create new object (Already exists)"
Maybe I am understanding the documentation wrong. Or does .clone() only work in PHP? I notice that in the documentation there is no flex object method .clone() listed …
I can clone events by copying the whole collection, but this is not what I want to do. Do I have to create a collection of a single object and then clone this one? There must be another way, I guess.
Can someone point me in the right direction?