1

I'm using a frontend form to create an entry with {{ actionInput('entries/save-entry') }} and would like to be able to use this entry immediately upon redirecting from the submit. From the docs I see that the redirectInput() function only accepts a url string. Is it possible to get my hands on the newly created entry's id in order to use it in the redirect?

For context, I have a page with a user-operated form which, when submitted, creates an entry that represents the customer's order. And upon submission I want to immediately head to a review page where it displays the new order's details back to them. Is there a better way to go about this flow? The only thing I can think to do is use a hidden field to set a unique hash on the slug which I also use in the url put in the redirect.

4

I think the answer is as simple as providing the new Entry's URI to the redirectInput function:

<form method="POST">
  {{ actionInput('entries/save-entry') }}
  {{ redirectInput('{uri}') }}

  <input type="text" name="title">
  <button>Save!</button>
</form>

Craft looks at the string you pass in, and evaluates it as an "object template," wherein you can use the short-hand syntax here ({uri}) to access properties of the saved Entry.

Keep in mind! The behavior here differs based on what Controller you're using—sometimes it'll be a different kind of Element, which may or may not have a useful/predictable uri (i.e. if you're updating an Asset).

The "template" you pass in can be as complex as you want. For example, if the form was part of your dashboard, you might want to prepend with a path of some kind:

{{ redirectInput('my-account/orders/{id}') }}

Similarly, you can use regular Twig syntax here—Craft just does some of these easy substitutions for you:

{{ redirectInput('my-account/orders/{{ object.id }}') }}

Note that the "template" won't get rendered in the actual request, so you'll see exactly what you typed here (well, plus a hash of the string, so users can't manipulate it)—it's only evaluated when submitted.

For more info, check out the redirectToPostedUrl($object) method!

1
  • 1
    Lot of great info here. Thanks. – Brimby Jul 10 '20 at 18:32

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.