0

I have a bunch of blocks that I would like to have hidden by default and only shown to specific users that will be determined by a view.

Is this possible? If so, how?

1

Under the block settings in views, there is an option called "access," which you can use to allow access based off of role (anonymous user, registered user, etc). If your users have specific user roles that you can filter off of, you can use this to do what your looking for.

0

Probably not the best way to go about this, as you really don't want to put logic in the theme... but if you are using some other way to single out users other than roles, you could always use a check in the theme to make sure the user should have the block displayed, if so, print the contents in views-view--myview.tpl.php.

0

Part 1:

Enable 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?) a user with a uid that matches one of the users you're looking for.

Part 2:

Here is a Rules Component which you can use as an example (you should be able to import this rule in your own environment if you have the Rules UI enabled):

{ "rules_block_visibility_show_block_for_some_uid" : {
    "LABEL" : "Show block for some uid",
    "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" : [
      { "data_is" : { "data" : [ "site:current-user:uid" ], "value" : "1" } }
    ],
    "DO" : [ { "data_set" : { "data" : [ "result" ], "value" : "1" } } ],
    "PROVIDES VARIABLES" : [ "result" ]
  }
}

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. Adapt the value of the uid to fit your needs, and/or add other uids you'd need via an additional (OR) Rules Condition.

With that, we're coming close to an actual answer to your question.

Part 3:

Head over to the "Block" settings for the block you want to show only for the user with (eg) 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.".

All you have to do is to select the Rules Component from Part 2.

Note: it doesn't matter what type of block it is (created by a view, or something else), it works in all cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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