2

I wrote lately some instruction to display a block in two different cases:

  • when the content type is [my-content-1] or [my-content-2]
  • when the url is /my_page1 or /my_page2

I found how to do it in Block visible for Specific Content Type AND Specific URL.

<?php
$match = FALSE;
$types = array('content-petites_annonces' => 1);
if (arg(0) == 'node' && is_numeric(arg(1))) {
  $nid = arg(1);
  $node = node_load(array('nid' => $nid));
  $type = $node->type;
  if (isset($types[$type])) {
    $match = TRUE;
  }
}

if (substr($_SERVER["REQUEST_URI"], 0, 10) == '/petitesan')
{ $match = TRUE;}

if (substr($_SERVER["REQUEST_URI"], 0) == "/node/add/content-petites_annonces
")
{ $match = TRUE;}

return $match;
?>

It works fine, I could declare several content types in the first array (I've got 6).
But as for the urls, it only works if I repeat the same line every time (and there are loads of them).

if (substr($_SERVER["REQUEST_URI"], 0) == "/my_page1
        ")
    { $match = TRUE;}
if (substr($_SERVER["REQUEST_URI"], 0) == "/my_page2
    ")
    { $match = TRUE;}
...

I am certain there is a more mutualized way to do it.
Is there also a mean to get it working regardless of the pagination, i.e. whithout having to notify every time

 if (substr($_SERVER["REQUEST_URI"], 0) == "/my_page?page=X 
            ")
            { $match = TRUE;}

Is the answer hidden in the 0, 10 in the precision about the request URI? I didn't even used that line.

2 Answers 2

3

This sounds like the perfect use case for something like the Context module. Contexts are triggered and become active, and would save you from writing tons of PHP to get the matching to work in an unlimited number of use cases and edge cases.

Think of conditions as a set of rules that are checked during page load to see what context is active. Any reactions that are associated with active contexts are then fired.

Related modules

2
  • I used the context module on another project, I thought it would too much for this case but actually that is enough for what I need, including the numbered pages.
    – Mars
    Aug 24, 2016 at 15:09
  • It also has added benefits in 6/7, mainly performance, especially if you have lots of blocks.
    – Kevin
    Aug 24, 2016 at 15:19
1

There is another way to answer your question, without PHP in the block settings: 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). And combine this with a check in such rule for your specific content types.

For an illustration of how to use this module, refer to my answer to "How to show blocks by url, when nodes have multiple paths?". It includes some Rules Components (in Rules export format), which you should replace by your variations to make it match your specific question.

To actually "determine the path structure in rules" (as in your extra comment below this answer), you might want to use a Rules Condition using a regular expression. Similar to the Rules Condition in the rule I included in my answer to "How to automatically skip intermediate redirects?".

Using regular expressions opens up many more situations (on top of the Rules block visibility module) where this solution can work. If you're not familiar with Regular Expressions (not obvious ...), then maybe you also want to look at my answer to "How to suppress a core Drupal warning or error message set with drupal_set_message for site users?".

Bonus:

This approach does NOT require the PHP filter to be enabled either (which you should try to avoid whenever possible) ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.