Monday, August 1, 2011

Block Creation in Drupal 6 & Drupal 7

Block Creation in Drupal 6

To remove static blocks and replace with as custom block.
Implement hook_block specifying the default visibility and so on

<?php
$blocks
['random'] = array(
     
'info' => t('Random'),
     
'weight' => 0,
     
'status' => 1,
     
'region' => 'sidebar',
     
'pages'  => 'offers',
     
'visibility' => 0, // all pages except
     
'cache' => BLOCK_CACHE_PER_PAGE,
    );
?>
Create an update hook that deletes the old blocks and boxes (I've specified the ID as I know they won't change on my site).

<?phpfunction mymodule_update_6101(){
   
   
$items = array();
   
$items[] = update_sql('DELETE FROM {boxes} WHERE bid in (2, 12,13)');
   
$items[] = update_sql("DELETE FROM {blocks} WHERE module = 'block' AND delta in (2, 12, 13)");
// Update the 'blocks' DB table with the blocks currently exported by modules.
// It's a "private" function called when you visit
// the admin/build/block/list/ page
// calling it here causes the rehash which you otherwise have to visit the page to get
   
_block_rehash();
// clear the cache
   
cache_clear_all();
    return
$items;
}
?>
When you run update.php your shiny new blocks should then be immediately available with no manual steps required.
This makes it so much easier to test your upgrades.
  
Block Creation in Drupal 7

Use hook_block_info and hook_block_view hooks in your custom module. hook_block_info defines the block. It will show up in admin > structure > blocks. *hook_block_view* displays the content. See examples below from the Drupal API.

Example of hook_block_info, where two blocks are defined (titled Syndicate and Recent content):
    <?phpfunction hook_block_info() {
  // This example comes from node.module.
  $blocks['syndicate'] = array(
    'info' => t('Syndicate'), 
    'cache' => DRUPAL_NO_CACHE,
  );

  $blocks['recent'] = array(
    'info' => t('Recent content'),
    // DRUPAL_CACHE_PER_ROLE will be assumed.
  );

  return $blocks;
}
?>
Example of hook_block_view:
<?phpfunction hook_block_view($delta = '') {
  // This example is adapted from node.module.
  $block = array();

  switch ($delta) {
    case 'syndicate':
      $block['subject'] = t('Syndicate');
      $block['content'] = array(
        '#theme' => 'feed_icon', 
        '#url' => 'rss.xml', 
        '#title' => t('Syndicate'),
      );
      break;

    case 'recent':
     if (user_access('access content')) {
        $block['subject'] = t('Recent content');
     if ($nodes = node_get_recent(variable_get('node_recent_block_count',10))){
          $block['content'] = array(
            '#theme' => 'node_recent_block', 
            '#nodes' => $nodes,
          );
        }
        else {
          $block['content'] = t('No content available.');
        }
      }
      break;
  }
  return $block;
}
?>
Refer to the Blocks API page on Drupal.org for full list of hooks. Drupal 6 is a little different. There is no hook_block_view hook; instead use hook_block to declare blocks.

No comments:

Post a Comment