2

I know that for restricting blocks to only certain pages you can do a path such as: blog/*

I'd like to know if anyone knows a way to do a path such as: */blog

Basically, due to views not being able to use contextual links as the first URL argument (www.example.com/%/events is not a valid URL but www.example.com/events/% is) - I would like to be able to have an * first since I plan on having multiple contextual views that would have different first terms, but the same second term. It would just allow me to write one line of restrictions vs. a line or 5 for each view.

Anyone know if this is possible in a Drupal 7 environment?

Thanks!

1

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 block is only shown on those pages that have a path ending with "/blog").

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

{ "rules_block_visibility_show_on_selected_blog_pages" : {
    "LABEL" : "Show block on selected blog pages",
    "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" : "\/blog",
          "operation" : "ends"
        }
      }
    ],
    "DO" : [ { "data_set" : { "data" : [ "result" ], "value" : "1" } } ],
    "PROVIDES VARIABLES" : [ "result" ]
  }
}
  • Thanks Pierre, I haven't gotten around to being able to try this yet as other more pressing matters have come up, but I will get to it. – Dan Oct 17 '17 at 21:36
  • Hey Pierre, I got it working. It required a rule set, as I had to set the default value to true first, then add the rule that you wrote to set the result to false. In my case I actually wanted the block to not show up on certain pages, but that was my fault for not being clear enough. Thanks for the help! – Dan Oct 26 '17 at 15:51
  • @Dan Great, good news! Now you also know how interesting this "hidden gem" module is (there are dozens of usecases for it). PS: merci for the accept/upvote also! – Pierre.Vriens Oct 26 '17 at 15:55

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .