Howdy. Hopefully this won't be so bad. I'm just trying to customize a system message (drupal_set_message) on redirect after submitting a form for creating a node. The redirect is working fine and I've located the message that I want to alter. It is set in node_form_submit with this function:
function node_form_submit($form_id, $form_values) {
global $user;
// Fix up the node when required:
$node = node_submit($form_values);
// Prepare the node's body:
if ($node->nid) {
node_save($node);
watchdog('content', t('@type: updated %title.', array('@type' => t($node->type), '%title' => $node->title)), WATCHDOG_NOTICE, l(t('view'), 'node/'. $node->nid));
drupal_set_message(t('The %post has been updated.', array('%post' => node_get_types('name', $node))));
}
else {
node_save($node);
watchdog('content', t('@type: added %title.', array('@type' => t($node->type), '%title' => $node->title)), WATCHDOG_NOTICE, l(t('view'), "node/$node->nid"));
drupal_set_message(t('Your %post has been created.', array('%post' => node_get_types('name', $node))));
}
if ($node->nid) {
if (node_access('view', $node)) {
return 'node/'. $node->nid;
}
else {
return '';
}
}
// it is very unlikely we get here
return FALSE;
}
I tried using hook_form_alter to change the submit handler based on some information I found here http://drupal.org/node/58689 and here http://symbiotix.net/articles/how-add-one-or-more-custom-submit-handlers.... I basically copy pasted the above function and changed the drupal_set_message to what I wanted and used this syintax $form['#submit']['my_very_own_submit'] = array();
to change the submit function from node_form_submit to my version of node_form_submit that has the altered message. It worked except it seems to run BOTH submit functions, as the message show up twice (once normal and once with my changes) and the node is created twice.
Would someone show my how you can write this so that only the alternate submit function is run, not both?
Comments
You could alter the $_SESSION['messages']
Rather then try alter the submit function which as you can see won't really work the way you want, just use your submit function to alter the $_SESSION['messages'][$type] variable. something like this may work:
I've not tested this out but am pretty sure it would work.
Adam A. Gregory
_____________________________
Drupal Developer and Consultant
https://goo.gl/QSJjb2
Acquia Certified Developer-Back end Specialist
That worked beautifully.
That worked beautifully. Thank you very much. Apparently I was barking up the wrong tree even though I was in the right forest. :)
I've only just recently started doing var_dump on forms and using hook_form_alter to customize some things here and there. I've been looking at the form api reference for help. I've never used a technique like you showed me here by changing the contents of variables. Is there any documentation available on this? Thanks again for the help.
The above suggestion will not
The above suggestion will not work because array_search will not find the sub string "has been created." Also you can create a more universal solution with nodeapi.
For Drupal 6 Try:
for a more specific solution geared to altering form submits see: http://drupal.org/node/325003#comment-2119298
And the above also will not work, when redirection is used
This will not work, when the user is redirected, after node is submitted. This is because the node could be not shown on the page, user is redirected to. But you rely on this (in case 'view').
So, another solution (for D6):
This is the solution I'm
This is the solution I'm going with.
...
If you just want to unconditionally change the message, see the comment about "String overrides" in [default.]settings.php (for D6 and up).
Thanks
Thanks, it is really a useful feature.
Although, as i understand, it can't be used to change messages per node type.
Also http://drupal.org/project/stringoverrides can be used
custom messages
http://drupal.org/project/drupal_tweaks
You can write your own custom rules to change default drupal set messages.
Override on your module
Guys, I don`t know if this is the best solution...but worked for me.
On module I made a function like this
and on the function MYMODULE_form_alter I override the call of function
Hope this helps.
Thanks @sitael. After trying
Thanks @sitael. After trying several approaches, yours was the one that worked. Had to change "buttons" in
to: