Under 'Error messages to display' I can hide errors (as is recommended for production sites). However is it possible to hide errors for everyone except a certain role?

  • yes it is, there are some modules around for that. But what you are suggesting also hides form errors too, as I stated below. – reptilex Sep 11 '15 at 11:28
  • Feature request here drupal.org/node/2743119 – giorgio79 Jun 6 '16 at 7:58
up vote 16 down vote
+500

Preface: This answer was first posted as an answer to "How do I suppress a warning or error message for site users?".

Disable Messages module

You could 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, Drupal 7 or Drupal 8.

About using regular expressions

If you're using its 'regular expression' feature, you have to make sure that you use a regular expression that "catches" the error message you want to intercept, as explained also in the Community documentation.

Below "Tip: Keep it simple." on that page, there is a great sample, which looks like so:

Be aware that something that appears very straightforward like:

Notice: Undefined index: field_txt_student_id in ctools_entity_form_field_content_type_render() (line 96 of [...]entity_form_field.inc).

Has a lot more unseen characters in it than one might expect:

<em class="placeholder">Notice</em>: Undefined index: field_txt_student_id in <em class="placeholder">ctools_entity_form_field_content_type_render()</em> (line <em class="placeholder">96</em> of <em class="placeholder">[...]entity_form_field.inc</em>).

So just wrap the meat of the grep with a simple "any character" regex: ".+" as in:

.+Notice.+Undefined index:.+ctools_entity_form_field_content_type_render.+entity_form_field.inc.+

So the "clue" to come up with a correct message, is to shrink the error message you want to catch, and putting ".+" characters where appropriate.

Talking about shrinking, note that the field_txt_student_id has been omitted in the regular expression also (to make it NOT depend on the field name).

Example

As another example, assume you want to catch this error message:

PDOException: in dblog_watchdog() (line 160 of /home/dbuser/public_html/yoursitesname.com/modules/dblog/dblog.module).

In this case a possible regular expression should look something like so:

.+PDOException.+in dblog_watchdog().+(line 160 of.+/modules/dblog/dblog.module).+

Note how I intentionally did not include part of the path before /modules/dblog/dblog.module (so that it can be reused in another site where the very same error occurs ...).

Also note that you have to give it some thought to yes or no include the 160 part of it: if the module ever changes a bit so that in the error message it (eg) becomes line 161, your error will not be intercepted anymore. To avoid that, omit the 160 part of it, if that's what you prefer (personally I would rather first see the error coming back then, and then re-adapt my regular expression).

If your regular expression doesn't work at first (= the message still appears), then it might be because there is some typo in the regular expression. To pinpoint such typo, first try with a less restrictive regular expression, even if it was only to try with something like so (to catch ALL messages containing PDOException and dblog_watchdog):

.+PDOException.+dblog_watchdog.+

Or even like so (to catch ALL messages containing PDOException):

.+PDOException.+

These last 2 expressions would probably suppress way too much messages, so you'd want to narrow it down to that single one you're after.

Unstable alternative

There is also the Better Messages module as an alternative. However, for D7 (which is what this question is about), it only has an Alfa version.

You can check the user role by using the in_array() function. In your template:


<?php global $user; ?>
<?php if (!in_array('YOUR_ROLE', $user->roles)): ?>
  <?php if ($messages): ?>
    <div id="messages">
      <?php print $messages; ?>
    </div> </div> 
  <?php endif; ?>
<?php endif; ?>

Or you can do it in your preprocess function (not sure is the message variable is called correctly but it can give you an idea)


function MYTHEME_preprocess_page(&$variables, $hook) {
  global $user;
  if (!in_array('YOUR_ROLE', $user->roles)){
    // This has to be checked, could be something similar.
    unset($variables['page']['content']['messages']);
  }
}

  • My understanding is that I cant hide <?php print $messages; ?> from users as it shows erros that users need to see as well as database type errors, eg if you try to create a node and leave out a mandatory field. Thanks – Evans Jul 6 '12 at 12:08
  • 1
    You're right, you could try to find out how the error messages differ from default messages and create a hook_permission for it. Good luck! – JurgenR Jul 6 '12 at 12:15

Actually this is not really doable.

You can hide all messages for users or roles but the main problem lies in the way drupal categorizes errors.

A form error is the same as a database exception for the messages function, so if you hide errors you also hide the error messages of a comment form.

I would love for someone to prove me wrong because I have not managed to achieve hiding php notices but showing form notices at the same time.

To remove notice from a Drupal website please follow below steps:

  • Go to admin/config/development/logging.
  • You have three choices:
    1. None will disable all error reporting.
    2. Errors and warnings will display on the most serious problems.
    3. All messages will display all problems and is probably only useful for developers.

Another way to disable notice and error message through code. Add following code to the settings.php file:

$conf['error_level']=0;

You can also configure Drupal to only log the errors following these instructions: go to Administration > Configuration > Development > Logging and errors or go directly to the path /admin/config/development/logging and set "Error messages to display" to none. Drupal then still logs errors but doesn't display them to users.

If you require more control use the Disable Messages module.

  • This is about drupal_set_message, not error messages. – njp Aug 27 '15 at 13:54
  • @njp - they all wander into $messages. Check your templates. – leymannx Sep 5 '15 at 12:12

You can use the better message module. It offers role based configuratoin and/or regular expression matching to filter out some notifications.

  • Better messages does not have a role based configuration for showing messages, only for showing the modules type of messages – reptilex Oct 29 '14 at 10:15

You may to comment this code in your theme

<?php if ($messages): ?>
    <div id="messages"><div class="section clearfix">
      <?php print $messages; ?>
    </div></div> <!-- /.section, /#messages -->
  <?php endif; ?>

or status_messages theme element serves this you may write own theme function in template.php

MYTHEME_status_messages(&$variables){}

This could effect only to notice and warning errors

  • 1
    But if $messages section is commented it will not display for admins too. – GoodSp33d Jul 6 '12 at 11:25
  • My understanding is that I cant hide <?php print $messages; ?> from users as it shows erros that users need to see as well as database type errors, eg if you try to create a node and leave out a mandatory field. Thanks – Evans Jul 6 '12 at 11:47
  • api.drupal.org/api/drupal/includes!theme.inc/function/… here is how core prepares a warning messages for rendering you may copy this logic and add your own logic in mythemename_status_messages() in your themes template.php file – werqious Jul 6 '12 at 12:21

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.