5

I created a view and put an access role of anonymous user. When I log in with normal user account I don't see it, but when I log in with my admin account I see the view.

I would like to not see it. Is there a way to stop it from displaying to admins?

BTW, I tried un-checking Bypass views access control in the permissions, but it didn't work.

3
  • 2
    If you are talking of the user with ID equal to 1, that by-passes all the user permission checking by design.
    – apaderno
    Apr 13, 2014 at 3:06
  • How is that view shown to admins? Is it like the site's homepage (implemented as a view), or something else (like a block created via a view and shown somewhere on your page)? Also, what should such admin see "instead"? FYI: it's true that user/1 has all privileges, but that doesn't mean you can't "hide" things for a specific user and/or selected roles ... Feb 28, 2016 at 19:15
  • @Pierre.Vriens it was a block view which had a flag on it. Since my anonymous users were not allowed to use flags, I clone the block view and created a fake flag, which links to '/user/login'. So when anonymous user's click the fake flag, they got sent to the login page. So I was wondering why I (user 1, admin) could see both block views when I had set the cloned view to Access: anonymous only.
    – No Sssweat
    Feb 28, 2016 at 21:17

5 Answers 5

5

Below is an answer to what I think this question is really about, i.e. "I would like to not see it. Is there a way to stop it from displaying to admins?". And to make the challenge a bit tougher, not for a user with role "Administrator", but assuming "... you are talking of the user with ID equal to 1 ...".

My short answer to it is "Yes you can" ... read on for more details ...

Part 1: Rules block visibility

You should be able to get this to work using the Rules block visibility module. Here is a quote from its project page:

The Rules block visibility module allows Rules components to be used to control block visibility. This provides Drupal administrators and developers extreme flexibility in controlling when blocks should be displayed on their websites, in addition to the default visibility options provided by Drupal.

The general idea is that if you can do it with Rules, you can use it to control block visibility, so the possibilities are limitless:

  • Need to show a block only for users registered more than a month ago?

  • Perhaps you have a block that must be shown only between 8am-5pm on weekdays?

  • What about displaying or hiding a block based on current weather conditions?

All of this can be done by using Rules block visibility.

With that, and as per the "if you can do it with Rules, you can use it to control block visibility" above, you've reduced your question to making Rules intercept the actual user who's using the site, i.e. if it is YES or NO (a boolean, right?) the user with uid = 1.

Part 2: Create an appropriate Rules Component

The Rules block visibility module doesn't have a lot of documentation, except in the README.txt that comes with this module. Here is what the crucial part of it is (to get the idea):

Configuration is done on a per-block basis. To control a block visibility using a rule component, go to the block settings page, scroll down to the "Rules" tab, and select the Rules component that you want to use.

Notice that to be able to be used by this module, a Rules component must be constructed in a very specific way. See the next section for more information.

This module comes with 2 sample Rules Components, that should help to understand the KeepItSuperSimple concept behind it.

By looking at these samples, I was able to create another Rules Component which looks like so:

{ "rules_block_visibility_hide_block_for_user_1" : {
    "LABEL" : "Hide block for user 1",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : {
      "module" : { "label" : "Module", "type" : "text" },
      "delta" : { "label" : "Delta", "type" : "text" },
      "result" : { "label" : "Result", "type" : "boolean", "parameter" : false }
    },
    "IF" : [
      { "NOT data_is" : { "data" : [ "site:current-user:uid" ], "value" : "1" } }
    ],
    "DO" : [ { "data_set" : { "data" : [ "result" ], "value" : "1" } } ],
    "PROVIDES VARIABLES" : [ "result" ]
  }
}

You should be able to import this rule in your own environment.

What this Rules Component does is "only" return a boolean (1 or 0), based on the condition if the user's uid is YES or NO equal to 1.

With that, we're coming close to an actual answer to your question. But note that, as a variation, you could also have used a rules condition like "does this user yes or no have a role like administrator".

Part 3:

Head over to the "Block" settings for the block you want to hide for the user with uid = 1. At the bottom of it's settings, within the typical "Visibility settings", there is now (after you enabled the Rules block visibility module as in Part 1) an extra tab labeled "Rules". Using that tab will allow you to select a "Rules Component" that will do what's documented below that selection list field, ie: "Show this block only if the selected rule returns a positive value. Important: to be listed here, a block visibility rule must have specific parameters and return values.".

And guess what, in my case I just selected my Rules Component from Part 2

Notes:

  • It doesn't matter what type of block it is (created by a view, or something else), it works in all cases.
  • This answer applies to D7.
2
  • 1
    Thank you for your detailed answer. I thought there was no solution to hide my block view from user 1 aka superadmin, but this works like a charm.
    – No Sssweat
    Mar 1, 2016 at 22:03
  • Merci for the kudos! This question seemed too challenging for me ... it reminded me about "Allen Bean" (Apollo 12 astronaut), when he was talking during some keynote session in which he described their various challenges (in the 60s ...) to prepare for going to the moon ... And for each challenge they ran into, they kept saying "There has to be a way to do it" ... Sounds like building a site using Drupal, right? In which case the answer is often like "There has to be some rule to do it" ... Mar 1, 2016 at 22:11
4

The Bypass views access control permission is indeed the one that should not be given, to avoid users bypass the access control for a view, but the user account created when installing Drupal (whose ID is 1) is granted all the permissions, by design. In fact, user_access() uses the following code.

global $user;
if (!isset($account)) {
  $account = $user;
}

// User #1 has all privileges:
if ($account->uid == 1) {
  return TRUE;
}

Future Drupal releases will not grant every permissions to user #1, once Remove the special behavior of UID#1 is marked as Fixed. (On June 2, 2023, that issue is still marked as Needs work.)

1

This is quite simple you are using views so why not filter on the role.

In your view:

  1. Select *Contextual filters*
  2. Global: *Null*
  3. Provide default value: *User ID from logged in user*
  4. Select: *When the filter value IS in the URL or a default is provided*
  5. With the Specify validation criteria of *USER*, *Allow both numeric UIDs* and *string usernames* and *Restrict user based on role* (Select your roles you DO want to access your view)
  6. Select Action to take if filter value does not validate and Select show page not found or another Validation.
0

A little late, but you could do with PHP filter.

In the block settings, go to "Pages on which this PHP code returns TRUE (experts only) and write:

if ($account->uid == 1) {
  return TRUE;
} else {
  return false;
}
1
0

Just in case you're looking for something similar in D8, you may want to try using the Block Visibility Groups module. Some details about it (from its project page):

... allows the site administrator to easily manage complex visibility settings that apply to any block placed in a visibility group. The visibility settings for all blocks in the group can be edited on one administration form.

This module works in conjunction with the Drupal 8 core's block administration system. It is a simpler alternative to modules like Panels.

The following modules provide additional conditions that can be used with Block Visibility Groups:

  • CTools: Provides various conditions including bundle for each entity type.
  • Menu Condition provides a condition based on menu position. For example, you can use it to specify that a block should only show for a particular menu item and all its children.
  • Term Condition provides a simple Condition plugin which checks to see if the current node has a specific taxonomy term.
  • Token Conditions creates a simple Token matching condition plugin. Vocabulary Condition provides a vocabulary visibility condition.

Your Answer

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

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