4

My question is similar to How to redirect the user to a page and show a message on the admin overlay (using drupal_set_message and drupal_goto), but I'm not doing an overlay -- it's just a straight page. This issue has been talked around a bit on this site, but I've never seen a real resolution of it.

My situation is:

  • The user submits a form, through the usual method.
  • The form has a _submit() handler, which runs: it does some stuff, sets a drupal_set_message(), and then redirects somewhere via drupal_goto().
  • The user properly ends up on the intended page, but the message is
    nowhere to be seen.

Any clues on this? I really can't figure out why this is happening.

  • 1
    I tried calling drupal_goto() right after drupal_set_message() in a test form, but the message was shown in the page where I was redirected. drupal_goto() should not be used in a form submission handler, but that is not what causes the message not to appear. – kiamlaluno Nov 20 '12 at 2:56
4

First of all, keep in mind that drupal_goto() in a form submit handler is bad. The reason is that drupal_goto() shuts down Drupal right after sending the location header, so the rest of your site functionality will not work.

To make a redirect, pass $form_state as reference in your submission handler (FORM_ID_submit($form, &$form_state)), and then set $form_state['redirect'] to where you want to send the user, for example with $form_state['redirect'] = 'node/add';.

|improve this answer|||||
  • Ah, I knew that... :) Once, anyway. So, (a) thanks!, but (b) although the redirection is happening correctly, the message is still not appearing. Any further thoughts? :) – Jim Miller Nov 20 '12 at 2:49
  • Can you show us your code? I usually drupal_set_message() just above setting the $form_state['redirect'] and it works. – AyeshK Nov 20 '12 at 2:59
5

I tried in my test site, and the message appears, even using drupal_goto(). I used the following code.

function code_testing_date_form($form, &$form_state) {
  $form['expire'] = array(
    '#type' => 'date_select',
    '#title' => t('Expire Date'),
    '#default_value' => date('Y-m-d H:i:s'),
    '#date_format' => 'Y-H:s',
    '#date_year_range' => '0:+20',
    '#required' => TRUE,
  );

  $form['weight'] = array(
    '#type' => 'weight',
    '#title' => t('Weight'),
    '#description' => t('Optional. In the menu, the heavier items will sink and the lighter items will be positioned nearer the top.'),
    '#default_value' => 5,
    '#delta' => 10,
    '#access' => FALSE,
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );

  return $form;
}

function code_testing_date_form_submit($form, &$form_state) {
  drupal_set_message('You have been redirected.');
  drupal_goto('user');
}

screenshot

What could cause the issue you are seeing could be:

  • There is code that clears the content of $_SESSION['messages'], used to contain the messages set with drupal_set_message().
  • There is code that invalidate the current session. In this case, the logged-in user should be logged out.
  • You are rendering the form in a template file.
  • The template file for the page to which the users are redirected doesn't have code to handle the $messages variable. The default node.tpl.php file contains the following code; if the template doesn't print $messages, the messages set with drupal_set_message() will be not seen.

    <?php if ($breadcrumb): ?>
      <div id="breadcrumb"><?php print $breadcrumb; ?></div>
    <?php endif; ?>
    <?php print $messages; ?>
    <div id="main-wrapper"><div id="main" class="clearfix"> 
    
|improve this answer|||||
  • Hmm. I'm not rendering the form in a template file, but the page that I'm redirecting to is handled by a template file -- I'm redirecting to 'user/123', which is rendered by page--user.tpl.php. I put some watchdog statements in the template file, and I'm seeing that it gets called twice. The first time, $messages has a value that is presumably displayed, but (of course, I guess) the second time the value has gone away. Any thoughts on where the second invocation is coming from, and how to stop it from happening? – Jim Miller Nov 20 '12 at 6:45
  • FWIW, other watchdog statements indicate that the _form_submit() handler is only being called once. – Jim Miller Nov 20 '12 at 6:46
  • One more update before giving up for the night: More watchdog statements show that the template's "print render($page['content'])" line, which is rendering the user page, causes the page--user.tpl template to be invoked for a second time. It appears that it's the results of this second invocation that actually shows up in the browser, with $messages having been cleared by the first invocation. Sigh. – Jim Miller Nov 20 '12 at 7:09
  • FinalFinal update (yes, I lied.) If I redirect to some page other than user/123 -- the template page -- the messages come up correctly. – Jim Miller Nov 20 '12 at 7:22
  • 1
    FinalFinal closing out: I tracked down the "something else going on", and now understand what was happening -- it was indeed on my end. – Jim Miller Nov 27 '12 at 19:08

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.