EKK
EKK

Reputation: 4138

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 ?

Upvotes: 0

Views: 4065

Answers (2)

EKK
EKK

Reputation: 4138

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

Upvotes: 1

tko
tko

Reputation: 99

drupal_write_record doesn't work if you want to use within an update hook. you can also use db_update or db_insert depending of course if you are updating or creating a database entry. Here's an example updating:

<?php
// find your block id, for me $bid = 38
db_update('block')
  ->fields(array(
    'module' => 'system',
    'delta' => 'main-menu', // block delta, find in database or module that defines it
    'theme' => 'mytheme', // theme to configure
    'visibility' => BLOCK_VISIBILITY_NOTLISTED, // see drupal constants
    'region' => 'main_menu', // region declared in  theme
    'status' => 1,
    'pages' => '',
    )
  )
  ->condition('bid', $bid, '=')
  ->execute();
?>

See the hook_block_info api for more details on the parameters.

Upvotes: 3

Related Questions