Agaric Design Collective

Example of hook_block in Drupal 6

By Benjamin Melançon
on 15 May
2 comments

Key words and phrases

multiple blocks Drupal pages define block in code

Tags

Description

<?php
/**
* Implementation of hook_block().
*/
function fightfi_block($op = 'list', $delta = 0) {
 
$block = array();
  switch (
$op) {
    case
'list':
     
$block[0]['info'] = t('Sidebar Links');
     
$block[1]['info'] = t('Footer Links');
      return
$block;
    case
'view':
      switch (
$delta) {
        case
0:
         
$block['subject'] = t('Sidebar Links');
         
$block['content'] = "super weak";
          break;
        case
1:
         
$block['subject'] = t('Footer Links');
         
$block['content'] = "super weak";
          break;
      }
      return
$block;
  }
}
// end function fightfi_block
?>

The interaction of configuration and code can get pretty confusing, so to be perfectly clear, this code makes two blocks available, they show up on the admin/build/block page, and then you can enable (and place) each block individually. Your site configuration and theme, that is, call hook block with different values of delta.

 

Add a block on the top roght corner of my site

I was wondering how to add a block to the top right corner of my site using hook_block. I would like to have my log in panel in that location.

Thank you in advance for your answer.

Posted by eloralon (not verified) on Mon, 2009-07-27 11:49
You add new block locations in the theme

Hi eloralon,

Hook_block is for defining new blocks themselves, if you have a block you like already (such as user login) and want to put it somewhere else, that you cannot reach with the admin/build/block page, you create a new region in your theme.

This is pretty simple; in Drupal 6 you just add an entry to your theme's .info file and print a corresponding variable in your page.tpl.php file.

In agaric_example_theme.info:

regions[topright] = Topright

And in page.tpl.php:

<?php
print $topright
?>

(Surround the above with whatever divs or HTML markup you need to position it well with CSS.)

Clear the theme cache (saving the theme page should do it if devel is not installed) and you'll be able to assign blocks to this new region!

See more at http://drupal.org/node/171224

Posted by Benjamin Melançon on Mon, 2009-07-27 17:41
Post new comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <h1> <h2> <h3> <h4> <h5> <h6> <small> <pre> <strike> <sub> <sup> <kbd> <s>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.