2

Is there a way to save a new entry with a template form, then pass the ID of that new entry along to the redirect location?

The use case is, I'm interacting with a scheduling service and want to pass along the new craft entry ID in a query string to their API. The service has a webhook that fires on successful scheduling (and provides an appointment ID).

Currently, I'm getting the user's email address returned from the API, then assuming that user's most recent section/entry in craft is the one I'm looking for. After that, I can unambiguously handle reschedules and cancellations by referencing the appointment ID.

The form has the following inputs

<input type="hidden" name="action" value="entries/saveEntry">
<input type="hidden" name="redirect" value="/schedule">

and I'd like the /schedule page to get a entryId passed into it

4

If you wanted to do this without having to write your own plugin, you can achieve this with a simple custom route and an extra parameter in your template.

Your hidden fields could look like:

<input type="hidden" name="action" value="entries/saveEntry">
<input type="hidden" name="redirect" value="schedule/{id}">

And then in config/routes.php put

'schedule/(?P<id>\d+)' => 'scheduleTemplate',

When craft saves the entry, it will redirect you to schedule with the ID of the entry that was just saved as the last segment. The custom route will pick up any digit on the end of the url and assign it to a variable called id which you can then use in your templates.

Some behind the scenes clearance, based on your comment

No they aren't twig variables as the replacement is doing through php (although apparently you can use Twig tags).

When you save an entry the new entry object gets passed in redirectToPostedUrl which, if there is an object passed will call the function renderObjectTemplate in the templates service, this will swap out any {single} or {{twig}} tags with the appropriate attribute and you will get the finished URL returned.

Here are two links to the class reference so you can see what I mean.

The redirectToPostedUrl function

The renderObjectTemplate function

| improve this answer | |
  • Thanks! Didn't know you could pass along entry parameters with single curly brackets. Doesn't look like they're twig variables, so is this getting handled by Craft's saveEntry action? – David Rampersad Jun 11 '15 at 19:00
  • @DavidRampersad I've edited my answer with a couple of details about how it works, would have been too long for a comment otherwise and could have been hard to read! – Alec Ritson Jun 12 '15 at 16:21
3

In a custom plugin you can hook into the saveEntry event:

craft()->on('entries.saveEntry', function(Event $event)]
{

    $entry = $event->params['entry'];

    // GET ENTRY ID
    $entryId = $entry->id 

    // DO YOUR STUFF
    ...

}
| improve this answer | |
  • Is there a way to limit the conditions where your event hook kicks in? I only want this to fire with a certain sectionId, if it's new, if it was posted by a certain form. – David Rampersad Jun 9 '15 at 18:57
  • 1
    Not really, but there's no overhead using the craft->on() event binder method, and you can easily test for all of the things you mention inside the event handler. – Mats Mikkel Rummelhoff Jun 9 '15 at 20:46

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.