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.
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;
}
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.
Comments
Try the following <?php$block
Try the following
Thanks
much.
The above won't activate the
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.
Please read
Please read http://drupal.org/node/26502
To know drupal way to call a block without using sql statement.
Which is not what the
Which is not what the original question was :)
But oddly enough helped me at
But oddly enough helped me at 11:49pm :D
In Drupal 7...
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.
In Drupal 6...
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
This has proved to be very
This has proved to be very useful to me, thanks!
Enable block programmatically
Thanks for this function it helps me.
To embed a view in a block
Use print views_embed_view('case_study', 'block_1');
where 'case_study' is the view name and 'block_1' is the block name.
In Drupal 7 it's hook_block_info_alter() to call
An issue with hook_block_info
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.