1

I want to show/hide my block at some content types so I'm using PHP filter. My code in block configuration is:

<?php 
if(arg(0)=='eshop') { return true; }
if(arg(0)=='node' && is_numeric(arg(1)) {
  return false;
} 
return false;
?>

With this code the block is visible at every page even if it returns false, even if arg(0) is 'node' and arg(1) is 170. But if I use only:

<?php 
if(arg(0)=='eshop') { return true; }
?> 

It will show the block corectly only at /eshop page.

What is this, a GIANT Drupal bug?

  • if(drupal_get_path_alias('node/' . arg(1)) == 'eshop') { return true; }? – Cyclonecode Apr 8 '16 at 13:40
  • eshop page works correctly and it is not a node, it is path defined by hook menu! Problem is that block is visible at every single pages, nodes, everything. – Incredible Apr 8 '16 at 14:17
2

There is another alternative available ... which doesn't require the PHP filter to be enabled (which you should try to avoid whenever possible) ... Just use the Rules block visibility module. 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 like /eshop).

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?".

1

Use PHP Code to filter, the content type and specific pages. This code shall work on node pages, although you can use any code to filter accordingly.

<?php
if (arg(0) == 'node' && is_numeric(arg(1)) && !arg(2)) {
  $node = node_load(arg(1));
  if ($node->type == 'YOUR_CONTENT_TYPE_HERE') {
    return TRUE;
  }
  elseif (PUT ANOTHER CONDITION HERE (for specific pages)) {
    //return TRUE here also
  }
  else {
   return FALSE;
  }
?>
0

Nope it is not a bug, if you are using arg() it will always consider the original path, that means node/[nid]

Your /eshop is an alias of node/[nid] so the Drupal will use original path for arg()

If you want to use /eshop then use if($_GET['q'] == 'eshop') { return true; }

  • Actually 'eshop' is a path defined by hook_menu. Still don't understand how that code can return true on every page when it clearly should return false. – Incredible Apr 8 '16 at 13:26
  • Use this <?php if(arg(0)=='eshop') { return true; } else if(arg(0)=='node' && is_numeric(arg(1)) { return false; } else { return false; } ?> – NTT Apr 8 '16 at 13:29
  • It is practically the same as I have. – Incredible Apr 8 '16 at 14:13
0

Why use PHP filter when you can configure your block using the UI.

enter image description here

0

If you want to show your block to particular content type then you can set your Visibility settings by your content type .Please find the screenshot.enter image description here

  • Yes it is true, however I should mention thtat I need display that block not only on pages of specified content type but also on some ohter pages. Your solution will show the block strictly at specified content types. – Incredible May 9 '16 at 7:03
  • Can you please tell me which page and content type access to see your block. – Pankaj Yogi May 9 '16 at 7:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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