3

I have built a view with exposed filters. There is a 'popular content' block on this page which I am controlling using the Context module (https://www.drupal.org/project/context)

I would like it so that the popular content displays, but only if no filters are being applied to the view. If a filter is applied then the block should not show.

I can see that the Context module has a query string condition option which I've tried to apply but without success. I've tried adding things like ~?title=* or ~*?title=** here but I can't seem to get the behaviour I'm after.

Any help would be appreciated.

UPDATE

I've used the answer from @Pierr.Vriens to create a rule component. However, the block now isn't showing on the /MyViewPath page (which is /courses on my site). I've put the export of the rule I created below. I'm surprised that it's not showing on the page now at all. The block is being set in the correct region, the 'rules' component has been added to the block settings itself and I've disabled the context module from messing with it too.

Here is an export of the rule I created:

{ "rules_block_visibility_popular_courses" : { "LABEL" : "Hide block for filtered course view", "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:url" ], "match" : "courses " } } ], "DO" : [ { "data_set" : { "data" : [ "result" ], "value" : "1" } } ], "PROVIDES VARIABLES" : [ "result" ] } }

UPDATE 2

So it was the space at the end of "courses " that was causing the problem. The exported rule below did the trick:

{ "rules_block_visibility_popular_courses" : { "LABEL" : "Hide block for filtered course view", "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:url" ], "match" : "courses", "operation" : "ends" } } ], "DO" : [ { "data_set" : { "data" : [ "result" ], "value" : "1" } } ], "PROVIDES VARIABLES" : [ "result" ] } }

5
  • Can you add an example of how your relative paths look like for a case with filters applied, and another case without such filters? E.g: "/MyViewPath?title=abc" versus "/MyViewPath", would that be a representative example of such paths (with versus without such filters)? Commented Jun 10, 2016 at 6:45
  • Hi @Pierre.Vriens , yeh sure. So without filters applied it would just be /MyViewPath like you said. With a filter applied it would look like /MyViewPath?title=&field_content_category_tid%5B%5D=32 or MyViewPath?title=&field_content_category_tid%5B%5D=32&field_more_content_tid%5B%5D=36 etc.
    – rickh
    Commented Jun 10, 2016 at 10:10
  • Appreciate the advice @Pierre.Vriens - I've edited my original answer and included the rule export
    – rickh
    Commented Jun 14, 2016 at 10:07
  • Have a look at my updated answer (based on your updated question). Let me know ho it turns out, ok? Commented Jun 14, 2016 at 10:44
  • Oeps, your extra edit crossed my latest comment ... I think your "update 2"-part is what you should move to a new answer (you may even get upvotes, hopefully not downvotes) if you do so. BTW: I like the further enhanced "ends with courses" ... Commented Jun 14, 2016 at 12:20

1 Answer 1

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 for the page with an URL that matches with your view without any filters applied).

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?". If you look at the example included there, just replace its Rules Condition by a Text comparison with Parameter: Text: [site:current-page:path], Matching text: MyViewPath (assuming that /MyViewPath is the path of your view without any filters applied).

Update: looking at your exported rule (in revision 3 of your updated question, so without "UPDATE 2" you added to it ), I noticed that there is a space (blank) at the end of "course ". Not sure why that is there, but I'd first try with removing that space. If that doesn't make a difference, I suggest you try a variation of your rule that looks like so (only the Rules Condition differs from what you have in your updated question):

{ "rules_block_visibility_popular_courses" : {
    "LABEL" : "Hide block for filtered course view",
    "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" : [
      { "data_is" : { "data" : [ "site:current-page:path" ], "value" : "courses" } }
    ],
    "DO" : [ { "data_set" : { "data" : [ "result" ], "value" : "1" } } ],
    "PROVIDES VARIABLES" : [ "result" ]
  }
}

If that also doesn't help, then it's time for Rules debugging. For that, refer to my answer to "How can I display the value of a variable inside a condition?".

PS: This answer applies to D7 ...

4
  • This is a really helpful module so thank you. I just can't quite get it to behave. I'll post a screenshot below
    – rickh
    Commented Jun 10, 2016 at 15:29
  • @rickh Yes it's an great module, I also think. Though it is a bit challenging to get going with it. If you want, perform an edit of your question by adding to the end something like so: "Update: I tried using the Rules block visibility module as suggested below, using a rule that looks like so (in Rules export format) ... But I still struggle with this part of it ..." Commented Jun 10, 2016 at 15:33
  • Ah yes, so the issue appeared to be the space at the end of "courses ". Good spot! Then to get the functionality I was after in the original question, I changed the comparison operation value to 'ends with' instead of 'contains' and voila. It worked. I've exported the rule and included this as an update in the original question. Thank you :)
    – rickh
    Commented Jun 14, 2016 at 12:11
  • @rickh Yeh! I knew from the beginning that this had to work. But you've experienced also now how tough it can be to do Rules debugging. So any time in the future you post a new question about rules, always include an export of your rule. And keep in mind the "Rules debugging" hint as in my related answer. PS: please do not remove the "space" in the exported rule in your question. Instead, why not post an EXTRA answer similar to your prior comment here, and include in that answer the ultimate result (your correct Rule in export format). BTW: merci for the "accept" also! Next (Rules?) question? Commented Jun 14, 2016 at 12:16

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.