Declaring we have block content

Last modified: June 10, 2009 - 15:17

Main topic described: A content block
Drupal hook described: hook_block

There are several types of modules: block modules and node modules are two. Block modules create abbreviated content that is typically (but not always, and not required to be) displayed along the left or right side of a page. Node modules generate full page content (such as blog, forum, or book pages).

We'll create a block content to start, and later discuss node content, as well as filtering content. A module can generate content for blocks and also for a full page (the blogs module is a good example of this). The hook for a block module is appropriately called "block", so let's start our next function:

<?php
/**
* Generate HTML for the onthisdate block
* @param op the operation from the URL
* @param delta offset
* @returns block HTML
*/
function onthisdate_block($op='list', $delta=0) {
 
}
// end function onthisdate_block
?>

The block function takes two parameters: the operation and the offset, or delta. The offset allows you to create different content for different blocks, all within the same block function. We'll just worry about the operation at this point. In particular, we care about the specific case where the block is being listed in the blocks page. In all other situations, we'll display the block content.

When the module will be listed on the blocks page, the $op parameter's value will be 'list':

<?php
/**
* Generate HTML for the onthisdate block
* @param op the operation from the URL
* @param delta offset
* @returns block HTML
*/
function onthisdate_block($op='list', $delta=0) {
 
// listing of blocks, such as on the admin/block page
 
if ($op == "list") {
    
$block[0]["info"] = t('On This Date');
     return
$block;
  }

}
// end onthisdate_block
?>

Next, we'll generate the block content.

For more information about the block hook see the hook_block API documentation.

Description of Block info array

jpappe - April 11, 2008 - 17:38

You can use the array returned by the "list" operation to set a default configuration for the block. As far as I can tell, the following keys can be set:

info (required)
A description of the of the block.
status
Set to true if the block should be enabled by default, false otherwise.
region
The region on the page that the block should be displayed in by default (ie, "left", "right", etc.).
weight
Typical usage of weight; controls which blocks float to the top of the region.
visibility
Use this in conjunction with the "pages" element below. It tells Drupal how to interpret the listed pages:
  • 0: show on all pages except those specified by the "pages" element.
  • 1: only show the block on the pages specified by the "pages" element.
  • 2: interpret the "pages" element as PHP, and only show on a page if that resolves to true
pages
If "visibility" element is set to 0 or 1 (or undefined), this is a list of pages separated by new-line characters, with "*" available as a wildcard. If "visibility" is set to 2, then this must be a snippet of PHP enclosed in tags.

module and theme names

asgalon - July 1, 2009 - 17:13

I have just discovered a very nasty trap concerning module and theme names.

I am relatively new to drupal and maybe I have missed a warning in the several books and the labyrinthine drupal 5 documentation on this site.

I am working on sorting out a very badly structured project and to make things a bit clearer I introduced a new module named after the project title. The main theme for the site has the same name.
Now I had a very confusing situation where my newly defined block views stopped to work after I added a generalized version of a few blocks to the main module. After several hours of experimenting, I discovered that projectname_block is the module projectname's implementation of hook_block, but also it is the overload for theme('block') for the theme 'projectname'. This is very bad. So don't give the same name to a module and a theme, it will bite you back sooner or later.

 
 

Drupal is a registered trademark of Dries Buytaert.