0

In almost all the times I searched for literature about disabling system errors\bad-notifications ONLY for anonymous/non-admin users, the result was large-scale discussions with seemingly pretty different answers involving honorable code changes. But me, I have yet to acquire serious knowledge in PHP... Is there really no SIMPLE solution out there for someone like me? One that will include an external module or pasting a simple code snippet in one of the TPL files?

I do not want to go to `admin/config/development/logging' and then click "none", because this option there cancels errors FOR ALL users, and not just for anonymous\non-admin ones.

Do you have such a simple, detailed explanation of how to do it?

This is a typical error message I would like to hide:

enter image description here

And here are some more details about it:

enter image description here

2

2 Answers 2

1

Accidently, there is a great answer in the question to How can I hide a specific error in Drupal for everybody? (not from me though) ... It starts with something like "Just open template.php and paste this cool snippet;"

I trust you know how to tweak it to make it fit for your case ... Especially this parts:

  • change phptemplate_ in the function name with your theme name.
  • replace the test for is_admin to an equivalent test for "is user logged in" (aka is it NOT anonymous). Something like so:

    if(user_is_logged_in()){ print "Logged In"; }

The slightly adapted code to hide errors for anonymous users looks like so:

function phptemplate_status_messages($display = NULL) {
    $output = '';
    global $user;
    foreach (drupal_get_messages($display) as $type => $messages) {
      if (($type == 'error' && user_is_logged_in()) || $type != 'error') {
        $output .= "<div class="messages $type">\n";
        if (count($messages) > 1) {
          $output .= " <ul>\n";
          foreach ($messages as $message) {
            $output .= '  <li>'. $message ."</li>\n";
          }
          $output .= " </ul>\n";
        }
        else {
          $output .= $messages[0];
        }
        $output .= "</div>\n";
      }
    }
    return $output;
  }

So open template.php and paste the above code in it. Remember to change phptemplate_ (in the function name in the above code) with your theme name (machine name).

10
  • Can you explain WHY "that solution did not work" for you (as in your priori comment here)? Commented Jul 31, 2015 at 20:16
  • You need to tweek it a bit for role "anonymous" ... wanna retry? Commented Jul 31, 2015 at 20:44
  • Pierre, I deleted my last comments, I was wrong --- Cocoskin's solution is not enought for me since the "Error: The website encountered an unexpected error. Please try again later" problem persists. I didn't fully understand where to implement the PHP changes you mentioned... Can you please detail a bit more?
    – user16289
    Commented Aug 4, 2015 at 12:58
  • 1
    OK, your turn now to QA-test it, ok? Let me know the outcome of it ... Soon? Commented Aug 5, 2015 at 19:46
  • 1
    The exact code I included in my answer should be pasted in the file named "template.php" (eg at the very end), Just don't forget to change the "phptemplate" in my sample. That file can be found in the root of the theme that you are using. And you do not need that "is_admin" anymore, because it is replaced in my pasted code with that user_is_logged_in() function. Understand? Commented Aug 5, 2015 at 20:51
2

I think the Disable Messages module is what you need.

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.

Features

  • 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.

-- HOW DOES IT WORKS? --
1. install and enable the Disable Messages module
2. Go to "/admin/config/development/disable-messages", and check the "Enable filtering"
3. Go to "/admin/people/permissions", and give the permission who should "View status messages", "View warning messages", View error messages", and "Exclude from message filtering" if desired.

-- Updated July 31st 2015, 2:23pm --
CSS trick
to not display for anonymous user(not login)

body.not-logged-in div.messages.error { display: none; }


to not display for logged in user(you should not do this for admin user, but here is the trick)

body.logged-in div.messages.error { display: none; }
6
  • Cocoskin, Your answer helped me very much, this was exactly what I need - I knew the module you mentioned, it was already installed in my site, but I did not know that the action itself is done from the permissions mechanism. BUT, now I remain with one last problem, the heading <H1>ERROR</H1> and it's underlying <p> The website encountered an unexpected error. Please try again later.</p> still appear (I've uploaded an image that displays this situation, please see upwards). This is problematic because it keeps alive the aesthetic problem in the site --->
    – user16289
    Commented Jul 31, 2015 at 17:06
  • CSS is not an option to treat this secondary <H1 ERROR><P Message> problem I described one comment above, since even in DOM there isn't anything suitable for "Display: none" (please see second image)...
    – user16289
    Commented Jul 31, 2015 at 17:08
  • 2
    @benous you should find out what causing that error. The css trick should work in this situation. See my update.
    – CocoSkin
    Commented Jul 31, 2015 at 18:29
  • I am sorry @CocoSkin... :\ The error still appear (without the red notification)... I've tried to play with the CSS you mentioned a bit but without change... Can you suggest something else? Ben.
    – user16289
    Commented Aug 2, 2015 at 23:46
  • @benous the CSS trick I provided is based on your screenshot with Developer Tool. The new error message you mentioned may different. You should find out what causing that error first.
    – CocoSkin
    Commented Aug 3, 2015 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.