4

I have a block that I want to show on pages matching news and news?page=1. In the 'only show on these pages' section I have news and news/* but it doesn't match the second url. Is there a way to do this?

Update 1: I have also tried this:

<?php $url = request_uri(); if (strpos($url, "news")) { return TRUE; } ?>

But even after clearing all the caches it doesn't seem to have worked either.

I also tried the pattern news?page=* but that doesn't work either! Surely somebody considered being able to show blocks on paginated pages?

Update 2: Found the answer Restrict block to specific pages is broken when using view pagination

Turns out the block I was trying to display was a View which was also set to display a pager (even though one doesn't appear). Turning the pager off resolved the issue.

5
  • 4
    it will not match the second as there is no / after news but an url parameter. Try news*
    – AjitS
    Commented Sep 18, 2012 at 10:44
  • That doesn't seem to make a difference
    – nexus_6
    Commented Sep 18, 2012 at 10:51
  • as indrock mentioned news* should work. Commented Sep 18, 2012 at 12:07
  • and, the next tme you use strpos() , keep in mind that how you have added i there is wrong. (strpos($url, 'news') !== FALSE)
    – AKS
    Commented Sep 19, 2012 at 15:02
  • <?php $url = request_uri(); if (strpos($url, "news")) { return TRUE; } ?> There are many drupal way to check this.
    – niksmac
    Commented Sep 19, 2012 at 15:06

4 Answers 4

2

Found the answer Restrict block to specific pages is broken when using view pagination

Turns out the block I was trying to display was a View which was also set to display a pager (even though one doesn't appear). Turning the pager off resolved the issue.

1

You can ajaxify the views using Ajax option in the view settings. So that page won't reload with parameters like ?page=1.

If you dont want to use ajax then you can select PHP code as visibility settings and write below php code. You need to enable php filter module to see the php code setting in the block configuration.

<?php
if (arg(0) == "news") {
  return TRUE;
}
else {
  return FALSE;
}
?>

Have a look at here: http://drupal.org/node/1208488

0

The solution as described in your own answer seems to be like a workaround for your original question, it doesn't answer the question for those case where you cannot (or do not want to) "turn the pager off to resolve the issue". To actually show blocks on paginated pages (as in your original question), 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 pages with a path either matching "news" or starting with "news?page=").

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 this variation of it to make it match your specific question):

{ "rules_show_block_on_pages_with_url_parms" : {
    "LABEL" : "Show block on pages with url parms",
    "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" : [
      { "OR" : [
          { "data_is" : { "data" : [ "site:current-page:path" ], "value" : "news" } },
          { "text_matches" : {
              "text" : [ "site:current-page:path" ],
              "match" : "news?page=",
              "operation" : "starts"
            }
          }
        ]
      }
    ],
    "DO" : [ { "data_set" : { "data" : [ "result" ], "value" : "1" } } ],
    "PROVIDES VARIABLES" : [ "result" ]
  }
}

To further enhance the above Rules Component (to make it more generic), just replace the 2nd Rules Condition in it with something using an appropriate regular expression (ie to check if the current path contains a number to indicate the page id).

0

To be able to configure blocks to show up or not show up based on URL parameters (same as you can for the URL path in Drupal core) use condition query module for Drupal 8 or block query for Drupal 7.

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.