I'm developing a lot of complex rules in D7 and for debugging reasons it is very handy to have different messages (the standard Drupal message) within the website. For this I use "showing a message on the site".

If a message of this starts with "Debug : " then I want it to only be displayed for the role of administrator. I do not want to add a condition to every message. That's is just to much.

How to achieve this?

Note: I wrote my first "Hello world" module last month, so I have a custom module in the site. I have to find the correct hook for this, so I looked into https://api.drupal.org/api/drupal/includes%21module.inc/group/hooks/7. But I cannot find a good hook for this.

  • You need to write your own module for this perhaps. Drupal does not offer such feature/filter out of the box. – Bhavin Joshi May 12 '15 at 5:05
  • I wrote myfirst Hello world module last month :-), so I have a custom module in the site. I have to find the correct hook for this so I looked into api.drupal.org/api/drupal/includes%21module.inc/group/hooks/7. But i cannot find a good hook for this? Any suggestion? – Justme May 12 '15 at 5:26
  • Dankjewel for your (late) accept! PS: did you get my re-forwarded eMail reply? – Pierre.Vriens Aug 20 '16 at 7:14
  • Welcome ☺ nop did not got a reply. Retry? – Justme Aug 20 '16 at 11:08
up vote 10 down vote accepted

Option 1: Use the Disable Messages module

Use the Disable Messages module for this. Here is a quote from its project page:

Gives a site owner options to disable specific messages shown to end users. The core drupal message system as offered by drupal_set_message is an excellent way for modules to send out messages to the end users. However not all drupal site owners are keen to show all the messages sent out by drupal core and all modules to their users. This module gives site administrators a reasonably powerful way to filter out messages shown to the end users.

Some of its features are:

  • Filter out messages that match a full text string exactly.
  • Filter out messages that match a regular expression.
  • Permissions to specifically hide all messages of a given type from any role.
  • Disable all filtering for specific users.
  • Disable all filtering for specific paths.
  • Apply filtering only for specific paths.
  • Debug system to get messages in the HTML without showing it to the end users.

Available for either Drupal 6 or Drupal 7 (and a DEV version for Drupal 8).

Option 2: Write your custom code

Use a variation of the approach as explained on Hook into and Preprocess Drupal Messages? Here is a quote from it:

The following example hides all error/warning messages from anonymous users and user's who don't have the 'administer nodes' permission. You can easily add your own conditions to filter out which message types should be displayed to who.

function mytheme_status_messages ($display = NULL) {

  $output = '';
  foreach (drupal_get_messages($display) as $type => $messages) {

    // skip all error/warning messages for anonymous users
    if (!user_is_logged_in() && ($type == "error" || $type == "warning")) { continue; }

    // skip error/warning messages for users who don't have the 'administer nodes' permission
    else if (!user_access('administer nodes') && ($type == "error" || $type == "warning")) { continue; }

    ...

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.