0

The default options in the administrative page for block visibility (e.g. admin/build/block/configure/block/3) are not enough for my needs.

For security reasons, I do not wish to use the PHP option (Show if the following PHP code returns TRUE (PHP-mode, experts only).). I plan to turn off all PHP configuration throughout the site.

The only route that remains available is to create a module that will control the visibility of certain blocks on specific pages. I am an experienced module developer but I do not know how to implement this specific thing.

What API exist that I could use? Apparently it's possible in Drupal 7. What alternatives do exist in Drupal 6?

Edit:
I actually easily found the answer to my original question. Now, I want to be able to control from my module the visibility of a block implemented by another module. My specific use case is thus: core's aggregator.module create feed blocks, but I want each feed block to be displayed on pages of certain types authored by specific users who are associated with specific feeds. I could re-implement the whole aggregator_block() or... I could let the aggregator.module do its job and simply control the visibility of the block that it produces. Is it possible?

2 Answers 2

1

You can call the aggregator's (or an other module's) block routines by doing something like this in your module...

$foo=module_invoke('aggregator', 'block', 'view', 'BLOCK_DELTA');
$content.=$foo['content'];

...with the resulting $content containing the html of the block in question.

So, it would be fairly trivial to include something like the above in your custom module's block view hook instead of copying all of the aggregator module's code into your module.

More information on module_invoke() here.

Just an FYI: Using the visibility key set to PHP in hook_block() actually requires the <?php ... ?> brackets with the whole shabang being passed through eval() so IMHO I don't think it is all that much safer than using the Block UI's PHP option.

1
  • +1 Thanks. Your solution is much cleaner than what I was working on. Regarding PHP input and safety, I prefer to have PHP code within a module than having PHP filtering enabled on the web site. My (administrator) account could be hacked and if php.module is not enabled, the harm that the hackers can do is limited. They cannot change the code in the module. Thanks.
    – augustin
    Commented Dec 11, 2013 at 2:37
0

Edit: this answers the original question, but not the follow up.

I should have looked closer before asking.

Apparently, it's also possible in D6: https://api.drupal.org/api/drupal/developer!hooks!core.php/function/hook_block/6

'visibility' = 2: Use custom PHP code to determine visibility. 'pages' gives the PHP code to use. 'pages' = custom PHP code when 'visibility' is set to 2.

I had seen that question but I thought it only applied to D7. Can't set PHP block visibility programatically

I'll try that!

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.