2

I've got a form that starts a batch job. What's the right way to redirect them after it finishes? Calling drupal_goto() in my finish function?

4 Answers 4

3
drupal_goto('your/hookmenu/link', array('query' => array('destination', $_GET['destination'])));
2
  • Yeah, I know how drupal_goto() works I'm just curious if there's a better way, something like $form_state['redirect']
    – drewish
    Commented Aug 7, 2012 at 14:42
  • I don't know a better one, but an alternative is header('Location: index.php?q=node"); But I will not use it personally. $form_redirect['redirect'] will not work outside a form context. So drupal_goto is the only robust way to do it.
    – Minty
    Commented Aug 8, 2012 at 13:49
6

You can set 'redirect' in hook_batch_alter(). For example:

$batch['form_state']['redirect']['path'] = 'cart';
1
  • 1
    Do you have a sample for how that would work? It doesn't seem like the batch is a form.
    – drewish
    Commented Sep 14, 2012 at 16:42
2

If you know the redirect path in the moment you start the batch, you can

batch_process('my/destination/path');

See https://api.drupal.org/api/drupal/includes!form.inc/function/batch_process/7

4
  • 1
    What if we have query string as well in the path. For example my/destination/path?destination=admin/content Commented Nov 19, 2015 at 7:03
  • For complex cases, it's better to use custom redirect callback.
    – Leksat
    Commented Nov 19, 2015 at 7:42
  • Can you please elaborate how to do that? Commented Nov 19, 2015 at 8:04
  • Yes. 1) Read docs: api.drupal.org/api/drupal/includes!form.inc/function/… 2) Pay attention to $redirect_callback ;)
    – Leksat
    Commented Nov 19, 2015 at 8:31
0

i'm create link to form (created by form api) that runs the batch with get-parameter "?destination=myRedirectPath".

batch lanch on function form_submit() and take info about redirect after batch from url.

without "goto".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.