5

I need to redirect the user to a destination and show a confirmation/error message. How can I do that? This is my code:

watchdog('webform', 'The user @user has been successfully created', array('@user'=> $mail));
drupal_set_message(t('Settings saved.', array()));
drupal_goto(drupal_get_destination());

This code redirects, but the message is not shown. Do i need some special action in the destination page?

EDIT - the problem is that i'm redirecting to an administrative page and the message is showing in the main theme, i want it to display on the overlay theme. I'll explain a little bit more in depth what i'm doing (this could be totally wrong).

I added a link that the user can click when he is viewing a submission entered through a webform (webform module). i defined a menu voice for that link in this way (i'ts only a callback, no page is shown):

$items ['createuser/%'] = array (
                                    'title' => 'Create users', 
                                    'access callback' => TRUE, 
                                    'page callback' => 'provintegra_createuser', 
                                    'page arguments' => array (
                                                            1 ), 
                                    'type' => MENU_CALLBACK );

Then i defined provintegra_createuser

function provintegra_createuser($sid) {
        //save user
    watchdog ( 'webform', 'L\'utente @utente è stato creato cons successo', array (
                                                                                    '@utente' => $mail ) );
    drupal_set_message(t('Settings saved.', array()));
    drupal_goto(drupal_get_destination());//This redirects you to the page of the webform submission in the overlay
}

I se the message "Setting saved" but it's in the main theme, not in the theme (seven) i'm using for the administrative overlay. Do i need to set some special parameter so that the message is shown in the overlay?

8
  • Just a side note: in Drupal, an action is something different from what you are describing.
    – avpaderno
    Commented Jul 13, 2011 at 16:46
  • i edit my question Commented Jul 13, 2011 at 16:49
  • This should work. Where are you calling this? Are you sure that the $messages variable is showing up on the destination page?
    – acouch
    Commented Jul 13, 2011 at 18:12
  • Did you modify your theme at all and possibly remove the $messages variable from the template?
    – Chaulky
    Commented Jul 13, 2011 at 20:59
  • I'm seeing the message but in the main theme and not in the overlay, i edited my answer with more detail Commented Jul 14, 2011 at 9:00

2 Answers 2

2

I don't think this is possible with the current message system, it would require some crazy workaround do make it work anyways.

The problem is that when you use drupal_set_message(), the message is stored in the session variable and displayed whenever possible. When you redirect to a page with an overlay two things happens.

  1. The main page is rendered and displayed.
  2. The overlay page is rendered and displayed (with an iframe).

This means that whatever message you set, would always be displayed on the main page.

What you could do, is when the messages has been extracted to set them again and remove them. This could happen in your theme somewhere. The problem is that it is probably very hard to determine when you should do this, as you effectively would hide all messages if you always did this. Another side effect would be that you would constantly get more and more messages, possibly leading to a fatal memory error at worse.

1

@googletorp writes:

I don't think this is possible with the current message system, it would require some crazy workaround do make it work anyways.

The problem is that when you use drupal_set_message(), the message is stored in the session variable and displayed whenever possible. When you redirect to a page with an overlay two things happens.

The main page is rendered and displayed. The overlay page is rendered and displayed (with an iframe). This means that whatever message you set, would always be displayed on the main page.

What you could do, is when the messages has been extracted to set them again and remove them. This could happen in your theme somewhere.

I solved this mainly in hook_init(). My scenario was that my moudle's .install created some drupal_set_messages() which would then be obscured by overlay when the user was redirected to admin/modules.

In my .install the last line of my hook_install implementation is:

// Set a flag so that we know installation has just completed.  Process the flag in ucberkeley_cas_init()
$_SESSION['ucberkeley_cas_installed'] = TRUE;

In my .module I have:

/**
 * Implementation of hook_init()
 * If overlay.module is enabled, make sure that our hook_install messages are displayed
 */
function ucberkeley_cas_init() {
  if (!module_exists('overlay') || (!is_array($_SESSION))) {
    return;
  }
  if ((array_key_exists('ucberkeley_cas_install_messages', $_SESSION)) && (!array_key_exists('messages', $_SESSION))) {
    foreach ($_SESSION['ucberkeley_cas_install_messages']['status'] as $msg) {
      drupal_set_message($msg);
    }
    unset($_SESSION['ucberkeley_cas_install_messages']);
  }
  elseif ((is_array($_SESSION['messages'])) && (array_key_exists('ucberkeley_cas_installed', $_SESSION))) {
    $_SESSION['ucberkeley_cas_install_messages'] = $_SESSION['messages'];
    unset($_SESSION['ucberkeley_cas_installed']);
  }
}

Now the messages appear in the the overlay.

(Let's all celebrate the removal of overlay from Drupal 8!)

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.