Search code examples
phpdrupaldrupal-7drupal-theming

How to display one block among two block in Drupal?


I have two block (like Block1 and Block2) and I have to display one block among two. Condition is Block1 will be display into the whole site if Block2 is not present there. Block2 will be displayed into specific pages which is controlled dynamically, i.e. when user create content in future they can control the Block2 display. So I need to display Block1 where Block2 is not present.

I am using Durpal 7.

Note: I have tried with default block visibility but this is not worked for me either.


Solution

  • You can do that in many ways.

    Using template_preprocess_page() in your theme's template.php file. You will need the 2 blocks printed on the page. Block1 and Block2. I'll assume that Block1 resides in region_1 and block_2 resides in region_2 and your theme name is mytheme.

    inside your template_preprocess_page() function in theme's template.php file:

    function mytheme_preprocess_page(&$variables)
    {
        if(isset($variables['page']['region_2']['block2']))
        {
            hide($variables['page']['region_1']['block1']);
        }
    }
    

    This way, Block1 will never be printed if Block2 is present.

    Hope it helps,

    Muhammad.