I will eventually install Panels to accomplish this. But I can't at this time. I want to allow an editor to display a right sidebar menu on any page when they want by checking a Single on/off checkbox . I added a CCK field to the Page type - Boolean -Single on/off checkbox. I am using Drupal 7.43 on Centos 7 and php 5.4

There is a php block invoke inside another php that checks if a cck field is not null. The first php is looking at the Boolean field 'field_hide_sidebar_menu' to see if it is null or not checked. If it is null it will display the block. This is not working. I have used 'if!empty' before to check if an image field is null and it works. Now I want it to check if the Boolean is checked or not checked so do I need to write this in a different way to check if the Boolean value is on or off - equal to 1 or equal to 0. Here is my code:

<?php if (!empty($entity['field_hide_sidebar_menu'])): ?> <div id="myclass">
<h2>Menu</h2> <?php $block = module_invoke('views', 'block_view',
'menu_sidebar-block_1'); print render($block['content']);?></div><?php
 endif; ?>

3 Answers 3


Generally, if you're wanting to enable or disable a block based on some parameters (and assuming this block is defined within a module of your writing), you'd use hook_block_list_alter(), test for your condition, and then either display or hide your block.

You can see how this works by look at the block module's implementation of it's own hook, block_block_list_alter().

For instance, if you wanted to hide something based on the current node:

MYMODULE_block_list_alter(&$blocks) {
    $node = menu_get_object();
    $my_hiding_condition = isset($node->somefield[LANGUAGE_NONE][0]['value']) 
      && $node->somefield[LANGUAGE_NONE][0]['value'] == TRUE;
    if (isset($blocks['my_block_id']) && $my_hiding_condition == TRUE) {
        // Hide the block.

A good hook for this kind of thing is hook_page_alter

function hook_page_alter

7 system.api.php hook_page_alter(&$page)

Perform alterations before a page is rendered.

Use this hook when you want to remove or alter elements at the page level, or add elements at the page level that depend on an other module's elements (this hook runs after hook_page_build().

If you are making changes to entities such as forms, menus, or user profiles, use those objects' native alter hooks instead (hook_form_alter(), for example).

The $page array contains top level elements for each block region:







You can use menu_get_object to populate a variable named $node, test it for null, then node type, then your boolean value, and if satisfied you can render your block into the $page['sidebar_second'] variable on the page.


You should be able to get this to work using the Rules block visibility module. Here is a quote from its project page:

The Rules block visibility module allows Rules components to be used to control block visibility. This provides Drupal administrators and developers extreme flexibility in controlling when blocks should be displayed on their websites, in addition to the default visibility options provided by Drupal.

The general idea is that if you can do it with Rules, you can use it to control block visibility, so the possibilities are limitless:

  • Need to show a block only for users registered more than a month ago?

  • Perhaps you have a block that must be shown only between 8am-5pm on weekdays?

  • What about displaying or hiding a block based on current weather conditions?

All of this can be done by using Rules block visibility.

With that, and as per the "if you can do it with Rules, you can use it to control block visibility" above, you've reduced your question to making Rules "check the value of your Single on/off checkbox".

For an illustration of how to use this module, refer to my answer to "How to stop a Views block from displaying to admins, such as user/1?".

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.