Score:0

Drupal::messenger() not working in shutdown function

sa flag

Some time I need to use a shutdown function. Here is a stupid example:

...
drupal_register_shutdown_function('display_message');
...

function display_message() {
  Drupal::messenger()->addMessage('Hello world');
}

Is it possible to display this message as for now, it is not working (even at the nex page refresh)?

As requested I will explain why I need to use shutdown function.

My use case:

We are in the presave hook of node A which just got a new custom status (which needs to be saved), let say status 5.

When this kind of node reach status 5, I need to save/update another node B which contains a reference to node A.

In the presave hook of node B, calculations are made using the value of this status... this means the status of node A must be saved before starting the calculations.

This is why I am postponing the saving of node B with a shutdown function but all the end user messages are not displayed.

You can argue that in the presave hook of node A, I can call a function with the status and the node A as parameter which will do the job; but there are many exchanges/references like this one and to avoid a spaghetti code, I put all my business model code in the presave functions of those two kinds of nodes.

Score:2
cn flag

This is kind of pointless. You can't render the message, the page is already rendered. And you can't transfer the message to the next page refresh without a session. If a session existed, it would be closed by now. If there was no session, you couldn't open one now and send a session cookie after the response is already finished.

Baud avatar
sa flag
This is the first time I use this feature (for example to save a node after the end of all DB transactions). What you are saying looks clever. Do you know another way to run some code just before the start of the rendering but after the end of the DB transactions?
cn flag
@Baud It sounds like you might be suffering from XY problem syndrome - if I were you I'd take a step back and try to explain what your end goal is, why you feel you need to add this message at a specific point in the page lifecycle rather than let it follow the normal flow, that sort of thing. Otherwise you'll probably only receive answers like this, and while it's accurate and useful, it doesn't solve your problem because the problem isn't fully understood
Baud avatar
sa flag
@Clive I have updated my question... I am not sure it is clear enough.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.