2

How do I show a message (notice or alert) after the node form is submitted and saved?

I'm writing a module that synchronizes the node content with a remote endpoint. All working good. But, if for any reason the sync fails, I want to show a message on the page the user is redirected to.

My problem is that I don't find the hook or function which is invoked after the form is saved to databases but before the redirect happens, where I hopefully can set the message I want.

6
  • Hello. Have you tried anything? Is there any code we can see? And have you seen the api docs for drupal_set_message?
    – Mołot
    Commented Nov 8, 2013 at 15:24
  • My problem is that I don't know where to put a call to drupal_set_message. I'd like to know if there's a hook I can call after the form has been saved to database but before the redirect. Commented Nov 8, 2013 at 15:27
  • So you want some kind of a confirmation step? Please see if my answer is sufficient or not yet.
    – Mołot
    Commented Nov 8, 2013 at 15:32
  • @FabrizioRegini See hook_node_insert() and hook_node_update() for node API methods, or hook_form_alter() if you'd rather alter the submission of the node add/edit form itself
    – Clive
    Commented Nov 8, 2013 at 15:34
  • I did already @Clive but what I understood is that they are triggered either during a transaction or before the query is saved to db. Commented Nov 8, 2013 at 15:35

1 Answer 1

4

You can create an additional submission callback. Not sure if it will run late enough for you. May be worth a shot.

Create a form alter hook using hook_form_alter(); Within there add an additional submit function to the #submit array

Example:

hook_form_alter(&$form, &$form_state, $form_id) {
  $form['#submit'][] = 'custom_submit_function';
}

function custom_submit_function($form, &$form_state) {
  drupal_set_message('your message');
}

You could try going 1 step further and adding the submit hook to #submit in the #after_build. This will ensure it is the very last submit callback (unless other higher weighted modules also use the afterbuild)

Example:

hook_form_alter(&$form, &$form_state, $form_id) {
  $form['#after_build'][] = 'custom_afterbuild_function';
}

function custom_afterbuild_function($form, &$form_state) {
  $form['#submit'][] = 'custom_submit_function';
}

function custom_submit_function($form, &$form_state) {
  drupal_set_message('your message');
}

I hope this helps

[update] added drupal_set_messages to code examples.

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.