I have a block which act as a menu. And the block is created dynamically like that:

$menu_block = array(
    'module' => 'menu',
    'delta' => 'clientmenu' . $account->uid, // the id of the block
    'theme' => 'MYTHEME', // the current theme
    'title' => '<none>', // dispaly no title
    'visibility' => 1, // it is displayed only on those pages listed in $block->pages.
    'region' => 'menu',
    'status' => 1,
    'pages' => $onlythispages, // display the menu only for this client

drupal_write_record('block', $menu_block);

And content of the variable $onlythispages is:

// page to display the menu
$onlythispages = 'client/' . $account->name . '
client/' . $account->name . '/*' . '

I created one menu for each created user. Each user has personal (only he can access) pages like:

client/' . $account->name 
client/' . $account->name . '/*'

And each user has some common pages which share with all users, pages like:


So the problem is that when I go to common pages I see all the "menu blocks" from all the users. So is there a way to show a block only if a specific user id, which actually is the currently logged in user?

I use hook_node_access to handle the access to a node but, how can I do it handle the access to block?


2 Answers 2


If only a block needs to be shown for each user, then what I would do is to implement the block in a module that changes the content of the block basing on the currently logged-in user. The visibility settings are also used for blocks implemented by third-party modules.

  • 2
    Agreed, I would definitely not make a new block for each user. I would make one block, and change the content per-user.
    – Entendu
    Sep 3, 2011 at 19:13

Below is an answer to what I think this question is really about, i.e. "So is there a way to show a block only if a specific user id, which actually is the currently logged in user?".

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 a specific uid. What follows details the steps for a block to only be shown to the user with uid = 1 (replace the 1 in it for any other value for uid).

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. By looking at these samples, I was able to create another Rules Component which looks like so:

{ "rules_block_visibility_show_block_for_user_1" : {
    "LABEL" : "Show block for user 1",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
      "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" ]

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.

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

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 and acknowledge you have read our privacy policy.

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