So, I know that you can do this on the backend. But is there a way to change the author of an entry from a frontend form? I'm making a vacation request system and I'm wanting managers to be able to add new requests for other users.
1 Answer
You can use a custom entry form that posts to the entries/save-entry
controller action. The first link includes a full Twig example. To edit an existing entry, make sure to include the entryId
(for example, as a hidden input). Then include an authorId
parameter with the ID of the new author to assign the entry to. This will also work for newly created entries.
Here's a stripped down example for a form to edit an existing entry and select a new author from a select field:
<form method="post" accept-charset="UTF-8">
{{ csrfInput() }}
{{ actionInput('entries/save-entry') }}
{# replace with the ID of the entry that's being edited, unless it's a new one #}
{{ hiddenInput('entryId', 123) }}
{% set authors = craft.users().all() %}
<select name="authorId">
{% for user in authors %}
<option value="{{ user.id }}">{{ user.fullName }}</option>
{% endfor %}
</select>
<button type="submit">Save new author</button>
</form>
Check the links above for more examples and a list of supported parameters.
-
So this didn't actually change the author of the entry. It still posted the entry as myself when I chose the author as a different user. Commented Mar 9, 2022 at 21:04
-
I tried exactly what you said and it doesn't appear to work. Reading the documentation you linked, it makes sense to me that this would work, but I am not having success with this approach. Each time I submit the form, I am the author even when I select another user. Commented Mar 9, 2022 at 21:30
-
@KeatynGarton Hm, difficult to say why it's not working. Do you have the permission to assign entries to different users? Also, are the users you're trying to assign the post to not suspended or locked? Maybe post the Twig code for the form in your question, then I can take a look. Commented Mar 10, 2022 at 8:47