I'd like to avoid having to enable each one separately and place into a region by hand.

Comments

abhijeet sandil’s picture

Try the following

$block = module_invoke('block', 'block', 'view', your block id);
print $block['content'];
carlwenrich’s picture

much.

Anonymous’s picture

The above won't activate the block. Best way: write a SQL statement to assign the blocks to a region. The Drupal table is called BLOCKS.

abhijeet sandil’s picture

Please read http://drupal.org/node/26502
To know drupal way to call a block without using sql statement.

nevets’s picture

Which is not what the original question was :)

nzcodarnoc’s picture

But oddly enough helped me at 11:49pm :D

bartk’s picture

I checked out the block module, and there's not a specific function that does it -- it updates the database on its own. I wrote a function that appears to work, but YMMV.

function _activate_block($module, $block, $region, $theme, $pages, $visibility) {
  drupal_set_message("Activating block $module:$block\n");
  db_merge('block')
  ->key(array('theme' => $theme, 'delta' => $block, 'module' => $module))
  ->fields(array(
    'region' => ($region == BLOCK_REGION_NONE ? '' : $region),
    'pages' => trim($pages),
    'status' => (int) ($region != BLOCK_REGION_NONE),
    'visibility' => $visibility,
  ))
  ->execute();
}
eddie_c’s picture

Here's a function that I'm using to do a similar thing in Drupal 6. I wrote it with a little help from here: http://bit.ly/q28Ow8


function my_module_activate_block($delta, $title, $weight = 0, $module = 'my_module', $region = 'content', $theme = 'my_theme', $pages = 'users/*', $visibility = 1) {
  $block_data = array();
  
  //Check here to see if the row is already in the table
  $query = 'SELECT bid FROM {blocks} WHERE module=\'%s\' AND delta=\'%d\'';
  $count = db_fetch_object(db_query($query, $module, $delta));
  $bid = $count->bid;
  
  //If there is an existing row for this block, we need to pass the name of the primary field as a third parameter to drupal_write_record
  //The value of the primary field goes in the $menu_block array.
  if ($bid) {
    $block_data = array('bid' => $bid);
    $primary = 'bid';
  }
  
  $block_data += array(
    'module' => $module,
    'delta' => $delta, // the id of the block
    'theme' => $theme, // the current theme
    'visibility' => $visibility, // it is displayed only on those pages listed in $block->pages.
    'region' => $region,
    'status' => 1,
    'pages' => $pages, // display the menu only for these pages
    'title' => $title,
    'weight' => $weight,
    );

  $status = drupal_write_record ('blocks', $block_data, $primary);
  if ($status) watchdog($module, 'Activated block '.$delta.': '.$title);
  
  return $status;
}

czigor’s picture

This has proved to be very useful to me, thanks!

see15_aug’s picture

Thanks for this function it helps me.

adavis.co.uk’s picture

Use print views_embed_view('case_study', 'block_1');

where 'case_study' is the view name and 'block_1' is the block name.

norman.lol’s picture

/**
 * Implements hook_block_info_alter().
 */
function mymodule_block_info_alter(&$blocks, $theme, $code_blocks) {

  if (isset($blocks['mymodule']['mymodule_block'])) {

    $blocks['mymodule']['mymodule_block']['status'] = 1;
    $blocks['mymodule']['mymodule_block']['region'] = 'secondary';
    $blocks['mymodule']['mymodule_block']['weight'] = 0;
  }
}
duncan.moo’s picture

An issue with hook_block_info_alter() is that it prevents future changes, as the hook will fire every time blocks or block configuration is saved. So in your example above, if you changed the region of the block in the UI, it would jump back to the Header region.

BartK's solution function allows you to enable blocks and place them on install of an install profile or a module without prohibiting future configuration changes.