Search code examples
drupaldrupal-7preprocessorblock

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?


Solution

  • 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).