3

Created a content type.

Name: Cities we serve

Machine name: cities_we_serve

Created a page using views.

Page URL: examplesite.com/blog

Created a block.

I would like to display the block to the above content type and the views pages.

Edited the block as follows:

enter image description here

$nodeType = "Cities We Serve";

// valid node id in view mode
if ( arg(0) == 'node' AND is_numeric(arg(1)) AND arg(2) == FALSE ) {
    $node = node_load(arg(1)); // cached
    if ( ($node->type == $nodeType) OR ($node->nid == blog) ) {
        return TRUE;
    }
}
return FALSE;

Now I see the block on all pages. How can I display the block only to the content types and the blog pages?

3
  • is this drupal 8 or 7?
    – Amy
    Jan 12, 2018 at 20:39
  • Drupal 7 version
    – autop
    Jan 13, 2018 at 19:17
  • any feedback about my answer? Feb 8, 2018 at 13:09

2 Answers 2

2

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 type ("Cities we serve").

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", 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 do I suppress a warning or error 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) ... Refer to "PHP in database: bad practice but" for more details about this.

0

Not a very polite solution, but you can create 2 "cloned" blocs and set content type condition in one of them, and a path condition in the second.

You can, also, use context module, but I think this manage all your blocks.

Ps: I'm sure you can look for a php code to do it if you prefer not use 2 blocks.

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.