User login

Example of hook_block in Drupal 6

Searched words: 
multiple blocks Drupal pages define block in code
<?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.

Resolution

Comments

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.

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

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Internal paths in single or double quotes, written as "internal:node/99", for example, are replaced with the appropriate absolute URL or path. Paths to files in single or double quotes, written as "files:somefile.ext", for example, are replaced with the appropriate URL that can be used to download the file.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.