User login

Example of hook_block in Drupal 6

<?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

Searched words: 
multiple blocks Drupal pages define block in code

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

passing value from page hook to block hook

Hi I already have a page hook module and I have written some code in it to prepare the menu tree. now in same module file I have created block hook and I want to display that menu tree in that block hook. how can we pass data from my page hook function to block hook. is it possible or there is any other way to achieve it?

put the code in a third, internal function

Sounds like you just want to abstract out the code, put it in a new function, and call the same function from both your page hook (do you mean hook_page_build()?) and your block hook.

done

Hey Benjamin, thanks for the reply, I have implemented in same way.
But its making things redundant as the operation which preprocessor function has already done at time of page load , I have to write the same operation in block hook also. Its has DB query too, but there is no way in drupal to stop this redundancy.

Use static caching or, in Drupal 7, use hook_page_alter()

There are two ways actually--

One is to use basic static caching in your function (just declare the variable resulting from the database query as static, and test for its existence before running the query) see also how http://api.drupal.org/drupal_static is used.

The second way, for Drupal 7, is to not use a page build function at all and instead copy the renderable array from your block to the main content region with http://api.drupal.org/hook_page_alter

I have created my custom

I have created my custom block successfully using this, now I want to display contents based on taxonomy, how can I do that, please help, I am new on drupal

remove ?>

You should probably remove that closing "?>", that will be causing some people some WSOD issues. Apart from that though, thanks for a great simple piece of example code.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • 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>
  • Lines and paragraphs break automatically.

More information about formatting options

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