Community Documentation

Declaring the block

Last updated February 8, 2012. Created by jn2 on March 24, 2011.
Edited by jhodgdon, ElielHaouzi, techek, kim-day. Log in to edit this page.

Drupal hook described: hook_block_info()

Modules are created to do all sorts of things: create blocks (abbreviated content that often appears on the right or left side of multiple pages), create special content types (for full page content - such as the content you are reading right now), track back-end information, and more. You may hear the term 'block modules' used to describe modules that primarily create block content (such as the menu module), or 'node modules' used to describe modules that primarily generate full page content (such as the blog and forum modules). At this stage, this module is a 'block module', because it generates a block.

In Drupal 7, there are at least eight block hooks. For the purposes of this module, we will use two of them. The first is hook_block_info(). As you might suspect, this hook tells Drupal what block(s) your module creates. We will use this hook to define a block that will eventually display the most recent posts. You can use a given hook exactly once in any module, so this hook must declare all blocks the module needs. For this module, a single block is all we need.

To use this hook to define our block, go to your current_posts.module file and create the function current_posts_block_info() as follows:

<?php
/**
* Implements hook_block_info().
*/
function current_posts_block_info() {
 
$blocks['current_posts'] = array(
   
'info' => t('Current posts'), //The name that will appear in the block list.
   
'cache' => DRUPAL_CACHE_PER_ROLE, //Default
 
);
  return
$blocks;
}
?>

(Remember not to include the closing ?> in your code.)

The doc block simply identifies the hook. For such a straightforward implementation, that is sufficient. Anyone reading the code can easily go to the API and call up the hook for further information.

The return value takes the form of an associative array. Pay special attention to this array structure, as it is Drupal's preferred programming structure. Arrays in PHP are well supported and very fast, and Drupal makes extensive use of them.

The only required value is 'info'. hook_block_info can also specify configuration settings. Here we set the cache to the default. See hook_block_info for a complete list of settings available.

Don't forget the final return statement.

Check

At this point, go to Modules, click the checkbox to enable Current Posts, and save your configuration. Next, navigate to Structure > Blocks. Scroll down to the bottom of the list. Among the disabled blocks, you should find the name, 'Current posts'. Return to Modules, disable your module and save. Important: Be sure you disable your module and save, or new partial code in your module could break your site.

See Also

Comments

Declaring the block

I copy the code above but when I go to my site I don't see "Current_posts" among the disabled blocks. What should I do.
I use the core Drupal from Acquia DEV Desktop.

Thanks

mimosa

did you clear the cache? try

did you clear the cache? try that ;)

_________________________

visit my portfolio: http://luco.ws

other possible solutions

also, note that your implementation of hook_block_info() MUST begin with $blocks = array(); (plural).

same goes for hook_block_view(): begin with $block = array(); (singular).

_________________________

visit my portfolio: http://luco.ws

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 7.x
Audience
Programmers

Develop for Drupal

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here