How to hide/show menu items using Rules based on Organic groups membership?

by user2326637   Last Updated November 16, 2017 12:07 PM

I want to HIDE and/or ENABLE certain menu links that are present on my main navigational bar depending whether users are/ aren't part of any group.

I am currently using the Rules module with these settings but these delete and recreate the links completely! I just want to hide them from view!

I have used the "update menu item" as action.



Answers 4


A better way of doing this might be to use Drupal's built user permissions. You can use a 3rd party module such as https://www.drupal.org/project/content_access so set specific access

E.g. so that only certain roles have access to those certain pages. Then drupal will automatically handle the showing/hiding of the menu links if they depending if the user does/doesn't have access to that page

Leigh Mason
Leigh Mason
January 26, 2016 13:01 PM

I’ve done what you’re asking by using “roles”. In my case, I wanted to hide menu items from users who did not have a particular “role”.

enter image description here

That allowed me to specify the menu access for each menu.

rrirower
rrirower
January 26, 2016 13:52 PM

Simplest solution is the Menu Item Visibility module. That is what is being used in rrirower's solution above.

Squatch307
Squatch307
January 26, 2016 15:53 PM

Since you are already using the Rules module, it should be straight forward to get this to work with the approach detailed below ...

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 whether users are/ aren't part of any group." (as in your question).

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.

Here is a sample Rules Component you could use to start from (which just contains a Rules Condition to verify if the user has a role with role id "4"):

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

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

What this sample 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.

Replace that Rules Condition to implement your "check whether users are/ aren't part of any group." (as in your question). If you're already using Rules with OG, that's a straight forward replacement.

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

Part 3:

Transform your menu for your main navigational bar in multiple similar navigation bar menus. And create 1 for each variation of groups for which you want that bar to be different. E.g.:

  • navbar1 for group A (only).
  • navbar2 for groups B and C (assume they have the same navigation bar).
  • navbar3 for all other cases.

Assume you customized the Rules Component (from Part 2) in something that would fit for group A, say you named it RuleNavbar1. Clone this Rule Component in 2 more similar rules:

  • A rule named RuleNavbar2 (for groups B and C).
  • A rule named RuleNavbar3 (for all others).

Head over to the "Block" settings for each of your menu blocks (1 for each of your menu bars). 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 created those Rules Components RuleNavbar1, RuleNavbar3 and RuleNavbar3 as described above, you just pick the one that fits for each navbarX (X = 1, 2 or 3). Done!

Pierre.Vriens
Pierre.Vriens
April 15, 2016 17:30 PM

Related Questions