1

I'm trying to add an RSVP button for users to register their attendance at my events. Once they click the button, I would like them to be added to a field called eventAttendees on the event entry.

My guess is this could be done as a form submission, and this is as far as I've gotten before getting a bit lost. I'm not sure how to pass the user ID to the entry.

<form method="post" accept-charset="UTF-8">
  {{ csrfInput() }}
  {{ hiddenInput('entryId', event.id) }}
  {{ hiddenInput('userId', currentUser.id) }}
  <input type="hidden" name="fields[eventAttendees][]">
  <button type="submit" class="btn btn-primary">RSVP</button>
</form>

Would appreciate any help!

2

For a form that would do both RSVP and cancel RSVP, you could do the following:

{% set currentUserId = currentUser.id %}

{% set currentAttendees = event.eventAttendees.ids() %}

{# Is the current user attending? #}
{% set attending = currentUserId in currentAttendees %}

{# If the current user is attending, we remove its ID from the attendees (cancel action) #}
{# If the current user is not attending, we add its ID to the attendees (rsvp action) #}
{% set attendees = attending ? currentAttendees | filter(v => v != currentUserId) : currentAttendees | merge([currentUserId]) %}

<form method="post" accept-charset="UTF-8">
    {{ csrfInput() }}
    {{ actionInput('entries/save-entry') }}
    {{ hiddenInput('entryId', event.id) }}
    <input type="hidden" name="fields[eventAttendees][]">
    {% for attendee in attendees %}
        <input type="hidden" name="fields[eventAttendees][]" value="{{ attendee }}">
    {% endfor %}
    <button type="submit" class="btn btn-primary">{{ attending ? 'Cancel RSVP' : 'RSVP' }}</button>
</form>
| improve this answer | |
  • Wow, amazing. If you have some time to help more, I have two issues. First is if the user is logged out, I get Impossible to access an attribute ("id") on a null variable. Second, unfortunately the 'Cancel RSVP' doesn't actually remove the user from the entry. Very much appreciate any further help you can give. – tommysvr Jan 28 at 21:27
  • 1
    @tommysvr This would only work for actual users and would require them to be registered and logged in otherwise Craft/you wouldn't know who is trying to RSVP for an event. In regard to cancelation, I just tried and (with a registerer, logged in user) it seems to work fine. Are you having issues with a logged in user? – Oli Jan 29 at 8:13
  • Ah. Of course - I will add a check for logged in. Yeah, logged in I am able to RSVP ok but when that changes to Cancel, nothing happens after clicking the button. This my full event query: gist.github.com/tommysvr/1d512eaf8528daec9c097a7622dfd8ac – tommysvr Jan 29 at 12:26
  • 1
    @tommysvr Ooops... Just spotted the error and I have edited the code above (new line after hiddenInput). It should work fine now. – Oli Jan 29 at 13:00
  • Amazing. Thanks so much - seriously appreciate it! – tommysvr Jan 29 at 13:10
2

You could do this:

<form method="post" accept-charset="UTF-8">
    {{ csrfInput() }}
    {{ actionInput('entries/save-entry') }}
    {{ hiddenInput('entryId', event.id) }}
    {% for user in event.eventAttendees %}
        <input type="hidden" name="fields[eventAttendees][]" value="{{ user.id }}">
    {% endfor %}
    <input type="hidden" name="fields[eventAttendees][]" value="{{ currentUser.id }}">
    <button type="submit" class="btn btn-primary">RSVP</button>
</form>

The for loop in there is to ensure that you do not remove any value (users) that have already RSVP. This is similar to Uploading Files from Front-End Entry Forms

| improve this answer | |
  • Perfect! Thanks @Oli, that did the job! – tommysvr Jan 28 at 11:46
  • I wonder if it would be too cheeky to now get a cancel RSVP version :D I suppose that would have to check if the user.id exists in the field and then show a different form to do the opposite... – tommysvr Jan 28 at 11:51

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.