I have the following scenario:
- 2 Channels:
events
andusers
{user_id}
holds the{entry_id}
to the currently logged in member since members are stored in a channel rather than using the native member module.- In the event list, I have an "add to my events" safecracker form which should inject the currently logged in
{user_id}
into the Playa field (cf_event_participants
) on theevents
entry currently being edited.
I can read and write to the correct entry and can set the definite playa selection using the hidden field method like so:
<input type="hidden" name="cf_event_participants[selections][]" value="{user_id}">
The problem I am having is that the above cf_event_participants
field is a multi selection field and the effect of the above is that all existing relationships already stored in that field are cleared upon submitting the safecracker form and replaced only with the submitted logged in {user_id}
.
Is there any way to just add a new relationship to the field without disturbing the existing ones already stored inside the [selections]
array?
One more caveat: Because multiple users might be "saving" an event into their list simultaneously the following work around will not work as by the time the user submits this form, another user might have already added themselves into the cf_event_participants[selections]
array.
// loop through existing values of the relationship field and store as hidden
// fields with the entry_id as value.
{cf_event_participants}
<input type="hidden" name="cf_event_participants[selections][]" value="{entry_id}">
{/cf_event_participants}
// add new member
<input type="hidden" name="cf_event_participants[selections][]" value="{user_id}">