Skip to main content
Tune Rules component
Source Link
Pierre.Vriens
  • 36k
  • 40
  • 50
  • 182

By looking at these samples, I was able to create anotherHere is a Rules Component which looks like soyou could use for a role with role id "4", assuming one of those three roles have "4" as role id, change that digit to whatever role id that matches for your case:

{ "rules_block_visibility_hide_block_for_user_1""rules_block_visibility_show_menu_block_for_role_4" : {
    "LABEL" : "Hide"Show menu block for userrole 1"4",
    "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""user_has_role" : { "data""account" : [ "site:current-user:uid""account" ], "roles" : { "value" : "1"{ "4" : "4" } } } }
    ],
    "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 isuser YES or NO has the role with role ID equal to 14.

With that, we're coming close to an actual answer to your question. All that needed is to massage your roles-logic in an equivalent (set of) Rules(s) (+ use that instead of the sample condition in my sample above), and then continue withas explained in Part 3 below.

So if you create 1 such Rules Component for each of your menu blocks from Part 2, you just pick the one that fits for each role. Done.

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

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" ]
  }
}

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. All that needed is to massage your roles-logic in an equivalent (set of) Rules(s) (+ use that instead of the sample condition in my sample above), and then continue with Part 3 below.

So if you create 1 such Rules Component for each of your menu blocks from Part 2, you just pick the one that fits for each role. Done.

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

Here is a Rules Component you could use for a role with role id "4", assuming one of those three roles have "4" as role id, change that digit to whatever role id that matches for your case:

{ "rules_block_visibility_show_menu_block_for_role_4" : {
    "LABEL" : "Show menu block for role 4",
    "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" : [
      { "user_has_role" : { "account" : [ "account" ], "roles" : { "value" : { "4" : "4" } } } }
    ],
    "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 YES or NO has the role with role ID equal to 4.

With that, we're coming close to an actual answer to your question, as explained in Part 3 below.

So if you create 1 such Rules Component for each of your menu blocks from Part 2, you just pick the one that fits for each role. Done!

Source Link
Pierre.Vriens
  • 36k
  • 40
  • 50
  • 182

Part 1: Rules block visibility

Have a look at 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 implement the logic to check your roles.

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 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. All that needed is to massage your roles-logic in an equivalent (set of) Rules(s) (+ use that instead of the sample condition in my sample above), and then continue with Part 3 below.

Part 3:

Head over to the "Block" settings for each of your menu blocks you want to conditionally show. At the bottom of its 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.".

So if you create 1 such Rules Component for each of your menu blocks from Part 2, you just pick the one that fits for each role. Done.

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