I have a cck node that will be available to the public for submission of new content. I have made a module that changes the form to my liking with form_alter. It is beautiful and works great. The only thing I have left to accomplish is that after the form is submitted the anonymous (not logged in) user gets sent to the home page with a message that displays "Your Page has been created."

I know that I can redirect folks to any node I want using $form['#redirect'] but I just want to alter the message that displays on the home page to say whatever I want. There's also a drupal_set_message piece that looks promising but I can't figure out how to make my module display that only when the form is submitted. I tried the obvious sounding things like ['#message'] to no avail, checked out the reference API pages and searched exhaustively for everything I could think of that relates.

I know this is doable and it's probably simple. Any help would be appreciated massively. Thank you!

Comments

greylogic’s picture

use $form['#submit'] to add your custom submit function to the form processing logic. Remember a form can have multiple submit functions. it will call all of them. If form['#redirect'] is not set, then the return value of the submit function is used for redirection. In case of multiple submit functions the return value of the last submit function is used.

always add an element to form['#submit'] instead of setting it.

$form['#submit']['custom_submit_form'] = array();

your function will look like

function custom_submit_form($form_id, $form_values) {
if(some logic)
drupal_set_message('FOO');

return 'the drupal path to be redirected';
}

--------------------------------------------------------------
My attempt with Drupal - Jaanlo.com

jacobmn’s picture

Thanks! That was perfect. The only thing I have to figure out now is how to unset the old message "Your [content_type] has been created." because as of now it shows that and my new message as an unordered list.

I was able to remove it by using:

unset ($form['#submit']);

before my

$form['#submit']['custom_submit_form'] = array();

The only problem is that seems to unset whatever it is that alerts drupal to the fact that new content has been created, as I seem to be unsetting the whole standard submit function. As such my workflow-ng action that I have set up stops working and does not e-mail the user notifying them of new content being created. It may be causing other problems as well that are not immediately apparent.

Any idea how I can get more specific by using unset ($form['#submit']['specific_message']); where specific_message is whatever displays the original message.

Does that make sense? Thanks again for your help on this, I am still working on figuring out how to understand the system a little more and learn how to find these things without bothering people on the forums.

greylogic’s picture

Hi,

take a look at http://api.drupal.org/api/function/drupal_get_messages/5
I guess invoking this function with $clear_queue set to true can solve your issue.

unset ($form['#submit']);

you can never do this. As i mentioned before a form can have multiple submit functions.
when you do this the other submit functions may not get called. So the form may not be even submitted to the node module which saves it to the DB.

You work flow might depend on hooks provided by the node_api. so if node itself is not getting submitted your other hooks may not run.

--------------------------------------------------------------
My attempt with Drupal - Jaanlo.com

jacobmn’s picture

So when I use

drupal_get_messages(null, true);

$form['#submit']['custom_submit_form'] = array();

I get no errors or anything but I still get both messages, in list form. As far as I can tell I have called drupal_get_messages properly, but it doesn't seem to be working. I hate to keep bothering about this but I am not sure where to go from here. Thanks again.

greylogic’s picture

Hi,

what is the order of the messages in your browser.
I mean, does the message "Your [content_type] has been created." displayed below or above your message?

Could you do print_r(drupal_get_messages(null, true)) and post the output here.

If your submit function gets called before the other (node modules submit), then increasing the weight of your module may help.

--------------------------------------------------------------
My attempt with Drupal - Jaanlo.com

jacobmn’s picture

Not sure how to use the print_r(drupal_get_messages...etc.) - When I add it my module it breaks the site, no errors, just white blank pages. The system message for the content type being created shows up first in the list. I had already changed the weight of the module because it wasn't working at all until I set it to run after the node module.

Thanks for your continued interest / help, it truly is appreciated.

yo-l’s picture

Ok a bit late reply but i hate all these unsolved treads.
Hopefully someone will find this somewhat useful.

When var_dump:ing $form you will find that the submit button calls node_form_submit.
In here.
$form['buttons']['submit']['#submit']

This function is located in the node module(node.pages.inc).
The entire function is also posted in the api http://api.drupal.org/api/function/node_form_submit/6
So what we need is a similar function for your node to be saved but with altered message text.

The easiest, no need to think solution is to change this function to call like this.

$form['buttons']['submit']['#submit'] = array(0 => 'custom_form_submit');

And the copy the entire function from the node module(or from the api reference) and paste in your module.
Change its name to the above set function name like this and change the messages posted.

function custom_form_submit($form, &$form_state) {  // <---- Change the function name to match your set above!
  global $user;

  $node = node_form_submit_build_node($form, $form_state);
  $insert = empty($node->nid);
  node_save($node);
  $node_link = l(t('view'), 'node/'. $node->nid);
  $watchdog_args = array('@type' => $node->type, '%title' => $node->title);
  $t_args = array('@type' => node_get_types('name', $node), '%title' => $node->title);

  if ($insert) {
    watchdog('content', '@type: added %title.', $watchdog_args, WATCHDOG_NOTICE, $node_link);
    drupal_set_message(t('@type %title has been created.', $t_args)); // <---- Post whatever message you like
  }
  else {
    watchdog('content', '@type: updated %title.', $watchdog_args, WATCHDOG_NOTICE, $node_link);
    drupal_set_message(t('@type %title has been updated.', $t_args)); // <---- Post whatever message you like
  }
  if ($node->nid) {
    unset($form_state['rebuild']);
    $form_state['nid'] = $node->nid;
    $form_state['redirect'] = 'node/'. $node->nid;
  }
  else {
    // In the unlikely case something went wrong on save, the node will be
    // rebuilt and node form redisplayed the same way as in preview.
    drupal_set_message(t('The post could not be saved.'), 'error'); // <---- Post whatever message you like
  }
}

Thats it...

shv_rk’s picture

Hi there,

i am creating a simple module to change the message on creating specific node type.
it is working all fine. but i still get the old message:

  • my new message
  • node has been created successfully
  • i really dont understand your post sorry!!! i added : drupal_get_messages(null, true); but not working
    this is my module

    <?php
    function customMessage_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
        if ($op == 'insert' && $node->type == 'comment') {
            drupal_get_messages(null, true);
            drupal_set_message(t(' Thank you for your comment. It has been queued for moderation by site administrators and will be published after approval.'));
        }
    
    }
    ?>
    
    

    some guiding plz?

    tmsimont’s picture

    where are you altering this:

    $form['buttons']['submit']['#submit']

    It seems that by trying to do this in theme_node_form($form) in template.php, all I am doing is altering a reference.

    I can then only change the display value of the button, and not the values that are read after the form is submitted.

    When the form is posted, all of the values revert back to what they were before I tried to modify them in theme_node_form($form) --

    so where do I change $form['buttons']['submit']['#submit']?

    warobushek’s picture

    hook_form_alter?