Search code examples
drupaldrupal-7drupal-blocks

How to activate and set conditions for a block programmatically in Drupal 7?


I have a block, and I want to activate it in some region and also to set for him the condition to be seen only in a give node. How can I do this programmatically in Drupal 7 ?


Solution

  • I was able to achieve this by using the following code.

    $menu_block = array(
        'module' => 'menu',
        'delta' => 'IDBLOCK', // the id of the block
        'theme' => 'MYTHEME', // the current theme
        'visibility' => 1, // it is displayed only on those pages listed in $block->pages.
        'region' => 'menu',
        'status' => 1,
        'pages' => '', // display the menu only for these pages
        );
    
    drupal_write_record('block', $menu_block);