1

I am trying to edit an entry automatically when you press a button. Is there a way to do this?

A bit more detail: I have listed up a few entries, each with their own button. When I press the button on a specific entry, I want the currentUser to be added to the User Field of that entry.

Currently I have managed to get this semi-working with a form where the user can edit the entry through input fields etc., changing their value, then submiting (as outlined under Entry Form in the docs). This is the prossess I want automated. When you press the button in the front-end the entry is edited with values set in the code and submitted.

Thanks!

  • Just trying to wrap my head around what you're going for. You've displaying a list of entries on the front-end. Each entry has a button. When a logged in user clicks one of the buttons, you want that user added to a Users field on that entry. – Brad Bell Oct 14 '16 at 19:43
  • That is correct! – Filip Iversen Oct 15 '16 at 6:24
3

I managed to find a solution, using hidden input fields :) Thank you for all the help :)

Edit: Ended up solving this by using tables and saving the username and ID in that. Used a plugin called Super Tables, which gives you a lot more control over the table.

<input type="hidden" name="action" value="entries/saveEntry">
<input type="hidden" name="userId" value="{{ currentUser.id }}">
<input type="hidden" name="redirect" value="/#{{ annonse.title }}{{ annonse.annonsePremie }}">
<input type="hidden" name="sectionId" value="{{ annonse.sectionId }}">
<input type="hidden" name="entryId" value="{{ annonse.id }}">
<input type="hidden" name="enabled" value="1">

{# Start with the top-level Super Table field #}
<input type="hidden" name="fields[{{ fieldHandle }}]" value="">

{# Ensure any existing rows in your Super Table field are saved #}
{% for block in annonse[fieldHandle] %}
                            <input type="hidden" name="fields[{{ fieldHandle }}][{{ block.id }}][type]" value="{{ blocktype }}">
<input type="hidden" name="fields[{{ fieldHandle }}][{{ block.id }}][enabled]" value="1">
{% endfor %}

{# Add a new row of data - note the `new1` #}
<input type="hidden" name="fields[{{ fieldHandle }}][new1][type]" value="{{ blocktype }}">
<input type="hidden" name="fields[{{ fieldHandle }}][new1][enabled]" value="1">

{# Repeat for all your fields in your Super Table field #}
<input type="hidden" name="fields[{{ fieldHandle }}][new1][fields][bruker]" value="{{ currentUser }}">
<input type="hidden" name="fields[{{ fieldHandle }}][new1][fields][brukerId]" value="{{ currentUser.id }}">

<button class="button submit" type="submit">
    <p>Submit</p>
</button>
  • 1
    Would you mind sharing your solution? Might be helpful for someone in the future! – Brad Bell Oct 15 '16 at 18:02
  • I have been trying for days to correctly save to my matrix field from a front end form. The code you provided in your own answer finally gave me the missing piece!! Thank you! – Steven Thate Sep 29 '17 at 19:40
3

Looks like you want to implement a feature to vote for entries. Have a look at these plugins, they make this really easy for you.

If you want to implement something yourself, I would look into adding the button behavior using JavaScript, listen for click events and send the data over XMLHttpRequests to the entries/saveEntry controller.

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.