Remove or alter a message from drupal_set_message() sessions

Public

When you need to alter or remove a system/contrib generated method without adding more modules.

</> CopyGet raw version
php
  1. <?php
  2. function mymodue_site_exit() {
  3. if (!empty($_SESSION['messages']['status'])) {
  4. $message_text_to_remove = "The text message that you want to remove";
  5. $key = array_search($message_text_to_remove, $_SESSION['messages']['status']);
  6. if ($key !== FALSE) {
  7. unset($_SESSION['messages']['status'][$key]);
  8. // Remove the empty status message wrapper if no other messages have been set.
  9. if (empty($_SESSION['messages']['status'])) {
  10. unset($_SESSION['messages']['status']);
  11. }
  12. }
  13. }
  14. }
  15. ?>

Comments

pari's picture

This code provides support for partial match and array of strings as input parameter

  1. <?php
  2. /**
  3.  * Implements hook_exit().
  4.  */
  5. function hook_exit() {
  6. $remove_strings = array('has been added', 'has been updated');
  7. if (!empty($_SESSION['messages']['status'])) {
  8. foreach ($_SESSION['messages']['status'] as $key => $message) {
  9. foreach ($remove_strings as $string) {
  10. if(strpos($message, $string) !== FALSE) {
  11. unset($_SESSION['messages']['status'][$key]);
  12. }
  13. }
  14. }
  15. // Remove the empty status message wrapper if no other messages have been set.
  16. if (empty($_SESSION['messages']['status'])) {
  17. unset($_SESSION['messages']['status']);
  18. }
  19. }
  20. }
  21. ?>