2

I know that having multiple path aliases to a single node is discouraged, but it is a situation I have to deal with.

My node is accessible via /news/node-1, /baseball/news/node-1 and /basketball/news/node-1. It's the same node, just three different aliases.

I have a 3 menu blocks, only 1 of which should be shown at any given time. I've configured the Page restrictions on the blocks so that they are restricted to certain url path patterns.

The "baseball" menu is restricted to baseball/*, the "basketball" menu is restricted to basketball/* and the 3rd menu is everything else, so I've set it to show on all pages except for baseball/* and basketball/*.

However, my problem is that when I'm browsing to baseball/news/node-1, the 3rd "everything else" menu is the one being shown. My -guess- is that Drupal is looking behind the scenes and seeing that the node is also considered to be /news/node-1 and therefore the path being used to choose my menu block is choosing the 3rd "everything else" menu.

a) Is there a way to see what path Drupal is using when determining which blocks to show based on path?

b) Is there a hook I could implement so that I can directly control which block to show on each request?

c) Any other ideas how I can make sure the correct block is shown?

  • I think it's a little more base/core than that in that these nodes are really still node/# and the block system uses that system path for restrictions, not the alias(es). – Jimajamma Jun 6 '13 at 1:07
  • The page restrictions on the blocks definitely respect path aliases. – Kenny Wyland Jun 6 '13 at 1:14
2

Try setting the baseball menu to show on baseball/* and baseball/*/*. Likewise for the basketball menu, then set the Everything Else menu not to show on all four paths.

  • I had high hopes this might work, but alas, no. I tried it and the Everything Else menu still shows when I browse to /baseball/news/node-1 – Kenny Wyland Jun 6 '13 at 3:04
  • Have you tried baseball/news/* ? – Triskelion Jun 6 '13 at 3:11
  • I haven't, I figured that should have been caught be either baseball/* or by baseball/*/* but I'll go try that right now. – Kenny Wyland Jun 6 '13 at 3:13
  • Alas, no, that didn't work either. :( – Kenny Wyland Jun 6 '13 at 3:16
  • I finally found a way to get the path it thinks it is showing and it still thinks it is showing /news/node-1 even though that's not what the current url is in the browser. – Kenny Wyland Jun 6 '13 at 3:20
1

Sure there is a way to answer your question, and without PHP in the block settings (as suggested in of the comments), and without the need for any hooks (custom code): you can use the Rules block visibility module for this. 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 "check the path of the current page" (so that the appropriate menu block is shown on the appropriate pages).

For an illustration of how to use this module, refer to my answer to "How to stop a Views block from displaying to admins, such as user/1?". It includes a Rules Component (in Rules export format), which you should replace by the variations below to make it match your specific question.

For your 1st menu block (baseball):

Within the block visibility settings of this block, use this Rules Component:

{ "rules_block_visibility_show_menu1" : {
    "LABEL" : "Show block containing menu 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" : [
      { "text_matches" : {
          "text" : [ "site:current-page:path" ],
          "match" : "baseball\/",
          "operation" : "starts"
        }
      }
    ],
    "DO" : [ { "data_set" : { "data" : [ "result" ], "value" : "1" } } ],
    "PROVIDES VARIABLES" : [ "result" ]
  }
}

For your 2nd menu block (basketball):

Within the block visibility settings of this block, use this Rules Component:

{ "rules_block_visibility_show_menu2" : {
    "LABEL" : "Show block containing menu 2",
    "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" : [
      { "text_matches" : {
          "text" : [ "site:current-page:path" ],
          "match" : "basketball\/",
          "operation" : "starts"
        }
      }
    ],
    "DO" : [ { "data_set" : { "data" : [ "result" ], "value" : "1" } } ],
    "PROVIDES VARIABLES" : [ "result" ]
  }
}

For your 3rd menu block (everything else):

Within the block visibility settings of this block, use this Rules Component:

{ "rules_block_visibility_show_menu3" : {
    "LABEL" : "Show block containing menu 3",
    "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" : [
      { "AND" : [
          { "NOT text_matches" : {
              "text" : [ "site:current-page:path" ],
              "match" : "baseball\/",
              "operation" : "starts"
            }
          },
          { "NOT text_matches" : {
              "text" : [ "site:current-page:path" ],
              "match" : "basketball\/",
              "operation" : "starts"
            }
          }
        ]
      }
    ],
    "DO" : [ { "data_set" : { "data" : [ "result" ], "value" : "1" } } ],
    "PROVIDES VARIABLES" : [ "result" ]
  }
}

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.