Caroline_710
Caroline_710

Reputation: 37

Display different blocks depending on a specific condition in drupal

I have a page type "Promotion". The page's template has a region, inside the region there are two blocks (created with structures/blocks) being rendered.

I have different versions of the blocks (text, color, etc). What I want to do is have a THEMENAME_preprocess (?) function to switch between blocks depending on a specific condition.

For example: if user is a member then display block-21 and block-22, if user did this or that then display block-23 and block-24.

Can you please advice me how to do that?

Upvotes: 0

Views: 1261

Answers (3)

Anthony Leach
Anthony Leach

Reputation: 1993

If you have custom functionality ('paying', 'non-paying') you may be best using hook_block_view_alter (https://api.drupal.org/api/drupal/modules!block!block.api.php/function/hook_block_view_alter/7) to control the display.

You could default both blocks to display and within the hook_block_view_alter check the conditions and modify the return to remove the block content (return false or unset the items).

Upvotes: 0

MilanG
MilanG

Reputation: 7114

You have context module:

https://www.drupal.org/project/context

...for more advanced block placing (and many more).

But if you want some custom logic why don't you put it inside the block it self? So make the block displaying one on another content based on your condition written in plain PHP.

Upvotes: 0

FalcoB
FalcoB

Reputation: 1333

actually i think you can try the Hide Block by Role module, which is exactly what you want, just the other way arround.

This module will allow you to hide blocks for users who belong to certain roles.

or the core functionality

under /admin/structure/block/manage and then configure:

enter image description here

so if i understand you right (!?) there is no need for some custom code/module

Upvotes: 1

Related Questions