| 5 bootstrap.inc | drupal_set_message($message = NULL, $type = 'status') |
| 6 bootstrap.inc | drupal_set_message($message = NULL, $type = 'status', $repeat = TRUE) |
| 7 bootstrap.inc | drupal_set_message($message = NULL, $type = 'status', $repeat = TRUE) |
| 8 bootstrap.inc | drupal_set_message($message = NULL, $type = 'status', $repeat = TRUE) |
Sets a message which reflects the status of the performed operation.
If the function is called with no arguments, this function returns all set messages without clearing them.
Parameters
$message: The message to be displayed to the user. For consistency with other messages, it should begin with a capital letter and end with a period.
$type: The type of the message. One of the following values are possible:
- 'status'
- 'warning'
- 'error'
$repeat: If this is FALSE and the message is already set, then the message won't be repeated.
262 calls to drupal_set_message()
File
- includes/
bootstrap.inc, line 1767 - Functions that need to be loaded on every Drupal request.
Code
function drupal_set_message($message = NULL, $type = 'status', $repeat = TRUE) {
if ($message) {
if (!isset($_SESSION['messages'][$type])) {
$_SESSION['messages'][$type] = array();
}
if ($repeat || !in_array($message, $_SESSION['messages'][$type])) {
$_SESSION['messages'][$type][] = $message;
}
// Mark this page as being uncacheable.
drupal_page_is_cacheable(FALSE);
}
// Messages not set when DB connection fails.
return isset($_SESSION['messages']) ? $_SESSION['messages'] : NULL;
}
Login or register to post comments
Comments
Use drupal_get_messages to clear the current messages
You can use
drupal_get_messages('status');to clear all status messages and leave errors intact.
Translate messages first
This function does not automatically translate messages so it is important to translate them first, e.g.:
<?phpdrupal_set_message(t("Don't panic!"), 'warning');
?>
It'd be nice if t() was built in
It'd be nice if drupal_set_message had t() built in a la watchdog.
drupal_set_message($message = NULL, $args = NULL, $type = 'status', $repeat = TRUE)
drupal_set_message('Hello :name', array(':name' => $name));
Just a thought.
Optional args
I'd agree, maybe this should be raised as a feature request. As drupal_set_message is fairly widely-used it's unlikely to be changed in a minor release, but maybe a future major version could incorporate this?
As both 'type' and 'args' would be optional, maybe it be worth moving over to a single url() style 'options' argument:
<?phpdrupal_set_message($message = NULL, $options = array()) {
// Merge in defaults.
$options += array(
'args' => array(),
'type' => 'status',
'repeat' => TRUE,
);
// ...
}
?>
Custom message types and theming
Note that the $type parameter can be anything you like and is passed directly through to the rendered div's class attribute. For example the following:
drupal_set_message(t('Something horrible just happened.'),'nuke');generates this HTML:
<div class="messages nuke"> Something horrible just happened.</div>Which can be styled with the following CSS:
div.nuke, table tr.nuke {background-color: #FF5EBE;
}
div.nuke, .nuke {
color: #6B3A61;
}
div.nuke {
background-image: url("nuke.png");
border-color: #FFFA00;
}
Be sure the background image "nuke.png" above is 24x24 pixels which will ensure the borders and padding work out nicely.