The Foundations of the Module

In the second chapter, we developed a module with the .info and .module files. These files are common to all modules, and we will begin this module by creating

.info and .module files.

Our module will be named philquotes, and (as would be expected) will be located in drupal/sites/all/modules/philquotes.

Inside that directory, we will first create a standard module .info file:

philquotes.info.

name = "Philosophy Quotes"

description = "Dynamic display of philosophy quotes." core = 6.x php = 5.1

There should be nothing unfamiliar about the above code. It follows the same format as the module we created in Chapter 2.

Next, we will start our new philquotes.module file:

* Module for dynamic display of pithy philosophy quotes.

* Implementation of hook_help() */

function philquotes_help($path, $arg) {

$txt = 'This module displays philosophical quotes in blocks. '. 'It assumes the existence of a content type named "quote".';

The above code implements hook_help() to provide some information about the module. This is also similar to the code we wrote in Chapter 2.

Just as with the module in Chapter 2, our philquotes module is mainly intended to provide block content.

Next, we will implement hook_block() —the hook that controls what content is displayed in a block. (A description of this hook was also given in Chapter 2.)

Here's our block hook:

function philquotes_block($op = 'list', $delta = 0, $edit = array()) { switch ($op) {

$blocks[0]['info'] = t('Philosophical Quotes'); return $blocks; case "view":

$item = _philquotes_get_quote(); if(!empty($item)) {

$content = theme('philquotes_quote', check_plain($item->body), check_plain($item->title));

$blocks['subjectl] = t('Pithy Quote'); $blocks['contentl] = $content; return $blocks;

For the most part, this code should also look familiar. In the case where the operation ($op) passed into the hook is list, this should simply return information about the blocks this hook makes available. In our case, there is just one block.

The more important case for us, though, is the highlighted section above. In the case where view is passed in as the operation, the module should be returning some content destined for display to the user. This content is wrapped in the $blocks variable.

As far as this hook_block() implementation is concerned, we will focus on the highlighted portion above.

When a block is generated for viewing, every block item contains two pieces: a subject and some content. The subject of our block will always be Pithy Quote, but the content is generated by a two-step process:

• Getting a quote from the database

• Adding any necessary theming information to that quote In the above code, this is done by the following two statements:

$item = _philquotes_get_quote(); if(!empty($item) {

$content = theme('philquotes_quote', check_plain($item->body), check_plain($item->title)); // ...

We use !empty($item) to make sure that the returned item is not NULL, -^-n, which would indicate that there were no quotes available. When $item \j> is NULL, we simply avoid sending a return value, and the block does not show up at all.

The first statement performs the database lookup, and the second handles theming. Let's take a detailed look at each.

0 0

Post a comment