Drupal Notes 3: a themed module for a new content type

create a new content type, create some entries
create a new theme (see earlier notes for how)

quoteDisplay.info contains the following:

; $Id$
name = “Quote Display”
description = “Dynamic display of  quotes.”
core = 6.x
php = 5.1

quoteDisplay.module contains this:

// $Id$
/**
* @file
* Module for dynamic display of pithy philosophy quotes.
*/

/**
* Implementation of hook_help()
*/

function quoteDisplay_help($path, $arg)
{
if ($path == ‘admin/help#quoteDisplay’)
{
$txt = ‘This module displays philosophical quotes in blocks. ‘ .
‘It assumes the existence of a content type named “quote”.’;
return ‘<p>’ . t($txt) . ‘</p>’;
}
}

/**
* Implementation of hook_block()
*/

function quoteDisplay_block($op = ‘list’, $delta = 0, $edit = array())
{
switch($op)
{
case ‘list’:
$blocks[0][‘info’] = t(‘Philosophical Quotes’);
return $blocks;
case ‘view’:
$item = _quoteDisplay_get_quote();
if(!empty($item))
{
$content = theme(‘quoteDisplay_quote’,
check_plain($item->body),
check_plain($item->title));
$blocks[‘subject’] = t(‘Pithy Quote’);
$blocks[‘content’] = $content;
return $blocks;
}
}
}

/*
* Return a quote from the Drupal database
*/

function _quoteDisplay_get_quote()
{
$sql = “SELECT nid FROM {node} “.
“WHERE status = 1 AND type = ‘quote’ ORDER BY RAND() LIMIT 1”;
$res = db_query($sql);
$item = db_fetch_object($res);
$quote = node_load($item->nid);
return $quote;

}

/*
* Implementation of hook_theme().
*/

function quoteDisplay_theme()
{
return array(
‘quoteDisplay_quote’ => array(
‘arguments’ => array(
‘text’ => NULL,
‘origin’ => NULL
),
),
);
}

/*
* Theme function for theming quotes.
*
* @param $text
* The quote content as a string.
* @param $origin
* The original source of the quote, as a string.
* @return
* An HTML themed string.
*/

function theme_quoteDisplay_quote($text, $origin)
{
$module_path = drupal_get_path(‘module’,’quoteDisplay‘);
$full_path = $module_path . ‘/quoteDisplay.css’;
drupal_add_css($full_path);
$output = ‘<div id=”quoteDisplay-text”>’ . t($text) . ‘</div><div id=”quoteDisplay-origin”>’ . t($origin) . ‘</div>’;
return $output;
}

quoteDisplay.css (also in /sites/all/modules/quoteDisplay) contains:

#quoteDisplay-text:first-letter{
font-size:18pt;
font-weight:bold;
}
#quoteDisplay-origin{
font-style: oblique;
text-align: right;
margin-right: 5px;
}

Note:

This module thus has a default “theme” of its own.  This may be overridden via CSS, or via a quoteDisplay.tpl.php file placed within the themes/module/currentTheme folder

Comments are closed.