0

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?

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Actually there is more to that as the basic roles are extended so that e.g. "authenticated user" can be a "paying" or "non-paying" user and depending on those two he should also see different blocks.
okay, so giving a user a specific role, wether they are paying or not is no option?
Not really cause again there are two types of paying users, one that bought a year membership and another one that has monthly membership (later on they can also switch between the membership types). The roles are too complicated to extend them in the panel. That's why I wish to use a function and specify a specific type of user and switch between blocks.
0

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

1 Comment

No problems, you can use xdebug / var_dump, don to work out the module and delta within this function.
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.