3

I have the following scenario:

  • 2 Channels: events and users
  • {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 the events 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}">
  • Strangely enough I'm working on something very similar. This is not really an answer to your question, but I set this up using a combination of Matrix and Playa: each event contains an attendee matrix, where the first column is a Playa relationship (single select) to the users channel. In this way, each row represents a single attendee. I then have a simple custom add-on that adds/removes a matrix row when the logged-in member registers/de-registers for the event. (While an admin has full access to the matrix via safecracker). – James Smith Mar 23 '13 at 9:16
  • Would you be able to share your code and how you've gone about the add/remove add-on? I'm not particularly savvy with PHP so writing a custom add-on isn't an option for me at this stage. I actually hacked a semi-viable (but far from ideal) solution together in JavaScript which queries a JSON url for the 'current' list of that playa field, builds up the correct hidden inputs with existing members as values just before the submit but it's not 100% safe to use obviously so your option sounds like a safer, better way to go about it. – Jannis Mar 23 '13 at 10:51
  • Sure... it's a little tricky since it's all embedded in a large custom-built module that does lots of other stuff too, but I've tried to break out the relevant parts here: gist.github.com/thisisjamessmith/9c01faeb65d11d641bb4 - though if you're not familiar with php this could be a problem! Important points: This uses the Matrix Relations add-on instead of Playa: github.com/moresoda/ft.matrix_relations.ee_addon (this is a simpler way, since it just stores the related entry_id). It also relies on the Profile:Edit add-on. Please get in touch if you need help with this. – James Smith Mar 24 '13 at 12:28
  • Also, this gist requires you to supply the column id of your attendee field (to be found in exp_matrix_cols) and the field id of your attendee matrix (to be found in exp_channel_data). – James Smith Mar 24 '13 at 12:33
  • Thanks a lot James, I will take a look through everything and see if I can piece things together to something I can use in the context of my site. Thanks again. – Jannis Mar 24 '13 at 20:59
2

(as requested by @GDMac - just copying my previous comments into a real answer here)...

Strangely enough I'm working on something very similar. This is not really a direct answer to your question, but I set this up using a combination of Matrix and Playa Matrix Relations: each event contains an attendee matrix, where the first column is a relationship to the users channel. In this way, each row represents a single attendee. I then have a simple custom add-on that adds/removes a matrix row when the logged-in member registers/de-registers for the event. (While an admin has full access to the matrix via safecracker).


I've tried to break out the relevant parts of my larger module here: gist.github.com/thisisjamessmith/9c01faeb65d11d641bb4 - though if you're not familiar with php this could be a problem! Important points: This actually uses the Matrix Relations add-on instead of Playa: github.com/moresoda/ft.matrix_relations.ee_addon (this is a simpler way, since it just stores the related entry_id). It also relies on the Profile:Edit add-on. Please get in touch if you need help with this.

Also, this gist requires you to supply the column id of your attendee field (to be found in exp_matrix_cols) and the field id of your attendee matrix (to be found in exp_channel_data).

| improve this answer | |

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.