I have node content type for booking and created booking page in drupal 7 and same page is displaying error, confirmation and booking page. I am trying to split into three different pages(nodes) using same node--booking.tpl.php content type. I have custom booking.class.php that manages the booking process using two steps function prebooking and postbooking

After booking, I would like to redirect to xyz.com/booking-confirm if it's successfully booking and if it's error xyz.com/booking-error and currently, using xyz.com/booking for all actions.

What should I do? I am not sure, which option works. I have found this using some articles and stuck at this point.

  1. Should I use header to redirect it?

    header('Location: /booking-confirm'); 
    header('Location: /booking-error'); 
  2. Should I hook them in template.php?

    function MYMODULE_user_register_form($form, &$form_state) { $form_state['redirect'] = 'path/to/somewhere'; }

  3. Should I use drupal_goto to redirect it? it's in booking.class.php class?

  4. Should I use hook_init? It's in template.php or booking.class.php ?

I am not sure, which is correct option to figure it out. I would like to split the URL using same content type. Any approach to solve the issue.

  • "I am not sure, which option works" Well have you tried any of them? If not, why not? – Beebee Jun 22 '18 at 13:42
  • I have few but does not helps me. I thought would have been ask to make sure..I am on right path. Do you have option? Thanks! – Nisarg Patel Jun 22 '18 at 13:57

You should use form state to do redirects since users are interacting with forms. header is not really want you want and neither is drupal_goto.

The class(es) should return either a success/fail style response to the caller (in form submit) and based on that, you should handle the response and redirect with form_state redirect. The only job of your booking class should be to create a booking, it does not need to handle redirects or worry about any of that logic.

Also, no, absolutely don't do redirects from hook_init.

Vanilla example:

function myform_submit($form, &$form_state) {
  $book = new MyBookingClass();
  $form_data = $form_state['values'];

  // createBooking() returns either TRUE or FALSE

  if ($book->createBooking($form_state['values'])) {
    $form_state['redirect'] = '/booking-confirm';
  } else {
    // There was some sort of error.
    $form_state['redirect'] = '/booking-error';
  • Great! let me review this your answer on my end. Thank you Kevin. – Nisarg Patel Jun 22 '18 at 14:00
  • Is this function goes in booking class or custom display module? – Nisarg Patel Jun 22 '18 at 14:58
  • Wherever your form code is for the booking form? It does not go in the class. – Kevin Jun 22 '18 at 15:00
  • The thing with this answer is though, @Kevin you're assuming he's used Form API and proper FAPI processes to do this, which OP doesn't mention anywhere in his question. – Beebee Jun 22 '18 at 15:21
  • Oh, well, that will certainly make it a lot more harder than it needs to be. – Kevin Jun 22 '18 at 15:45

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.