0

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.

1 Answer 1

2

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.

3
  • 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?
    – Baud
    Commented Nov 24, 2021 at 16:44
  • @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
    – Clive
    Commented Nov 24, 2021 at 17:07
  • @Clive I have updated my question... I am not sure it is clear enough.
    – Baud
    Commented Nov 24, 2021 at 17:48

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.