Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By mabster on
Is it possible to display a message on a page that you are redirected to?
I would like to do something like this:
drupal_set_message("Thank you for rating this node.");
drupal_goto("node/view/" . $node->nid);
... except the drupal_goto seems to 'flush' the drupal_message call, and the user never sees the message.
Am I going about this the right way?
Ta,
mabster
Comments
Not possible atm
But we should really keep messages in sessions. This would allow us to do a drupal_goto after submissions (comment, node, ...) to prevent people from refreshing a page, and unknowingly resubmitting their item.
Yeah, the 'resubmission' thin
Yeah, the 'resubmission' thing is where I was coming from.
Perhaps adding a $message parameter to drupal_goto, which it passed on to drupal_set_message? Would that work?
Caching
More than once I've wanted to show a message on a page where one has been
drupal_goto()
-ed. The problem is that you can only show a message to anonymous users on a HTTP POST request and not on a HTTP GET request. If you set a message on a HTTP GET request, the message might get cached and risks being shown to other anonymous users. Maybe we/you can think of a way to fix this behavior though. Seepage_set_cache()
andpage_get_cache()
inincludes/bootstrap.inc
for details.a few thoughts ...
first, i can't think of any places in core where we actually need to use drupal_goto(). most of these can be replaced by calling menu_set_active_item() [or setting $_GET['q']], and then showing then calling menu_execute_active_item(). I have been doing this recently and now we don't do a redirect after posting a node or comment (CVS Drupal).
In the case where we *have* to redirect, you can just append a message in the queryingstrign such as q=user?message=congratulations+bob. this page will be cached but the key includes the querystring so noone else will see it ... obviously you need more code to recognize that 'message' querystring param and display it.
I am going to roll a patch
for adding a message to drupal_goto....
this functionality is used a lot in the new install system, and i think it's the cleanest way to implement this.
Sessions
It's probably simplest to store an array of messages in the session, which gets filled with messages, and emptied whenever they are displayed/fetched.
I am going to assume that
I am going to assume that this issue still exists? Is it simply to difficult to remove things from a session var the second they are displayed? Im not sure I follow the caching logic as sessions are not cached. I have a working solution that uses my own queryable_variables module to accomplish passing the session to drupal_goto'd pages that has never run into caching issues since it's not cached and is cleared out everytime it's called. Should I continue to run with this solution or is there a better solution that has already been implemented?
The problem as far as I could see is that everytime you run drupal_goto, the session var's are completely destroyed. So to save that from happening, I simply dump the Session var to queryable_variables right before the drupal_goto and then re-load that session whenever the user object is loaded (which would be at the destination page anyways). It is all based on the Session ID. Drupal's message system sees the session var with the messages array and displays things properly it seems and then nukes the messages array values as they are displayed. So it works, but it's kinda wonkyish if you ask me since it requires an outside module for help. This seems like something drupal_goto should be doing imho.