1

I currently have a freeform which allows logged in users to create an entry from the front end. I'm looking to build an edit page for each of these entry's so the user can come back and edit it if they've made a mistake.

I'm getting confused on how to do this on a per entry basis. e.g. how do I make a link on the front end for each entry that links to it's own /entry_slug/edit?

3

Andrea's answer is part of the solution!

You'll need to set up a route, either via routes.php, or via the admin. I tend to prefer PHP routes, so you never have to worry about the functionality being removed from the app due to actions in the CP.

return [
    'my/path/<entrySlug:{slug}>' => ['template' => '_my/path/edit-entry']
];

Create a form in templates/_my/path/edit-entry that contains all the fields you want a user to be able to edit.

This doesn't actually restrict which fields they can update, but you don't have to display everything, if you don't want or need to. The only way to control which fields are updatable is via a custom module/plugin controller.

The edit page should include, at minimum:

{# Find the entry by slug and author: #}
{% set entry = craft.entries({
    slug: entrySlug,
    authorId: currentUser.id
}).one() %}

{# If the entry wasn't found, or didn't belong to the current user, throw up the 404 page: #}
{% unless entry %}
    {% exit 404 %}
{% endunless %}

{# Create a small form with your attributes: #}
<form method="post">
    {{ csrfInput() }}
    <input type="hidden" name="action" value="entries/save-entry">
    <input type="hidden" name="entryId" value="{{ entry.id }}">

    <input type="text" name="title" value="{{ entry.title }}">
    <input type="text" name="fields[myFieldHandle]" value="{{ entry.myFieldHandle }}">
</form>
2

In my _entry.html (or _entry.twig), I have the following:

<a href="{{ entry.url }}/edit">Edit entry</a>

This would look like: http://example.com/my-section/my-entry/edit

EDIT:

<a href="{{ url(entry.url ~ '/edit') }}">Edit entry</a> may be considered a better practice.

3
  • This comes with /edit template not found? Where do you place your template for edit?
    – JMKelley
    Nov 28 '18 at 1:22
  • or do you create a route allowing each entry to be edited?
    – JMKelley
    Nov 28 '18 at 1:26
  • I have a folder for my section called "Blog", and in blog I have my _entry & edit templates. Also a route shown in screenshot: cl.ly/5ef8e668052a Nov 28 '18 at 21:00

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.