block_example.module
<?php
// $Id: block_example.module,v 1.6 2004/11/02 22:45:23 drumm Exp $
/**
* @file
* This is an example outlining how a module can be used to define a block
* to be displayed on the right or left side of each page.
*/
/**
* Implementation of hook_help().
*
* Throughout Drupal, hook_help() is used to display help text at the top of
* pages. Some other parts of Drupal pages get explanatory text from these hooks
* as well. We use it here to provide a description of the module on the
* module administration page.
*/
function block_example_help($section) {
switch ($section) {
case 'admin/modules#description':
// This description is shown in the listing at admin/modules.
return t('An example module showing how to define a block.');
}
}
/**
* Implementation of hook_block().
*
* This hook both declares to Drupal what blocks are provided by the module, and
* generates the contents of the blocks themselves.
*/
function block_example_block($op = 'list', $delta = 0) {
// The $op parameter determines what piece of information is being requested.
if ($op == 'list') {
// If $op is "list", we just need to return a list of block descriptions. This
// is used to provide a list of possible blocks to the administrator.
$blocks[0]['info'] = t('A simple text string');
$blocks[1]['info'] = t('An empty block');
return $blocks;
}
else if ($op == 'view') {
// If $op is "view", then we need to generate the block for display purposes.
// The $delta parameter tells us which block is being requested.
switch ($delta) {
case 0:
// The subject is displayed at the top of the block. Note that it should
// be passed through t() for translation.
$block['subject'] = t('Title of block #1');
// The content of the block is typically generated by calling a custom
// function.
$block['content'] = block_example_contents(1);
break;
case 1:
$block['subject'] = t('Title of block #2');
$block['content'] = block_example_contents(2);
break;
}
return $block;
}
}
/**
* A block content function.
*/
function block_example_contents($which_block) {
if ($which_block == 1) {
// Modules would typically perform some database queries to fetch the content
// for their blocks. Here, we just return a string.
return t('Some example content.');
}
if ($which_block == 2) {
// It is possible that your block will not have any content, since it is
// probably dynamically constructed. In this case, Drupal will not display
// the block at all.
return;
}
}
?>
