Overwriting the node has been created/updated drupal set messages
This is quite a common request with two paths depending on how complex we need the replacement messages to be.
The easy way:
If you only need to change the basic text, e.g. "@type %title has been created." to "Thanks, the @type called %title has been added." then the easiest solution is to install the String Overrides module which allows for simple replacement of text. Note: after install you may need to flush your sites cache for the module setting page to appear. Once installed go to yoursite.com/admin/settings/stringoverrides.
To alter the node created message, in the 'Original' field enter
@type %title has been created.
and then your alternative version the replacement field. The original string for the updated message is "@type %title has been updated.".
The above approach is fine for simple replacement, but should you need say different messages per content type then you'll need to go the slightly harder route.
The slightly harder route:
This method requires that you create a custom module, should you not already have one (if you're new to this, there's plently of info in the handbook http://drupal.org/node/508). Here'll we will be implementing hook_form_alter() to alter the submission function for our node form, pointing to our own version of the function which will do our different created/updated messages.
Start by adding the hook_form_alter to your custom module, replacing
MODULENAME
with the name of your custom module. First you need to establish which content types you wish to alter, in this case we will be changing the messages for page & news nodes. To target the appropriate form, first we'll find the form id by adding the following code and navigating to each of the create node forms.
/** * Implementation of hook_form_alter(). */ function MODULENAME_form_alter(&$form, $form_state, $form_id) { drupal_set_message($form_id); }
This will output the ids of the node forms we want to change the submit message for, in our case 'page_node_form' and 'news_node_form'. Next we need to find where, and to what submission functions the form is pointing to. To do this we will update our hook form alter to this:
/** * Implementation of hook_form_alter(). */ function MODULENAME_form_alter(&$form, $form_state, $form_id) { switch ($form_id) { case 'page_node_form': case 'news_node_form': print_r($form); break; } }
What the above will do is output the full array that composes the form. Typically the submit button which points to the submission function we want to change can be found within the array index "buttons", the default full so
$form['buttons']
, but this can vary depending on modules or modifications you could make to your site. The button we are looking for looks like this in our print_r output:
[submit] => Array ( [#type] => submit [#access] => 1 [#value] => Save [#weight] => 5 [#submit] => Array ( [0] => node_form_submit ) )
The function node_form_submit is where the standard created/updated messages are output, so in our hook form alter we will change this to point to a new function we will create shortly called
MODULENAME_node_form_submit
. So, our final hook form alter should look like this:
/** * Implementation of hook_form_alter(). */ function MODULENAME_form_alter(&$form, $form_state, $form_id) { switch ($form_id) { case 'page_node_form': case 'news_node_form': $form['buttons']['submit']['#submit'][0] = 'MODULENAME_node_form_submit'; break; } }
Now we will create our own copy of the node_form_submit function within our custom module as such.
/** * Overridden version of node_form_submit(). */ function MODULENAME_node_form_submit($form, &$form_state) { 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)); } else { watchdog('content', '@type: updated %title.', $watchdog_args, WATCHDOG_NOTICE, $node_link); drupal_set_message(t('@type %title has been updated.', $t_args)); } 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'); } }
Now we will modify it to produce two different messages by checking the node type.
/** * Overridden version of node_form_submit(). */ function MODULENAME_node_form_submit($form, &$form_state) { 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); if ($node->type == 'page') { $message = t('The @type %title has been added.', $t_args); } elseif ($node->type == 'news') { $message = t('The @type item "%title" has been created.', $t_args); } drupal_set_message($message); } else { watchdog('content', '@type: updated %title.', $watchdog_args, WATCHDOG_NOTICE, $node_link); if ($node->type == 'page' ) { $message = t('The @type %title has been saved.', $t_args); } elseif ($node->type == 'news') { $message = t('The @type item "%title" has been updated.', $t_args); } drupal_set_message($message); } 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'); } }
Now when we save or update either a page or news node, we will now get our custom messages, instead of the standard Drupal output.