How to write the Drupal Module?

Let say the module name is xxx.

Telling Drupal about your module
Edit modules/xxx/xxx.info:


name = Module Name
description = A description of what your module does.
dependencies = module1 module2 module3
package = "Your arbitrary grouping string"
version = "5.1"

Telling Drupal who can use your module
Edit modules/xxx/xxx.module:


<?php
/**
* Valid permissions for this module
* @return array An array of valid permissions for the onthisdate module
*/
function xxx_perm() {
return array('access content');
} // function xxx_perm()
?>

Declare we have block content


<?php
/**
* Generate HTML for the onthisdate block
* @param op the operation from the URL
* @param delta offset
* @returns block HTML
*/
function xxx_block($op='list', $delta=0) {
// listing of blocks, such as on the admin/block page
if ($op == "list") {
$block[0]["info"] = t('Block Name 1');
$block[1]["info"] = t('Block Name 2');
return $block;
}
// out block content
else if ($op == 'view') {
switch ($delta) {
case 0:
return xxx_block_block1();
case 1:
return xxx_block_block2();
}
}
} // end xxx_block
?>

Generate the block content


function xxx_block_block1() {
$block_content = '';
$query = "SELECT nid, title, created FROM {node} WHERE ...";
// get the links
$queryResult = db_query($query);
while ($links = db_fetch_object($queryResult)) {
$block_content .= l($links->title, 'node/'.$links->nid) . '
‘;
}
// check to see if there was any content before setting up the block
if ($block_content == ”) {
// no content from a week ago, return nothing.
return;
}
// set up the block
$block[’subject’] = ‘Block Title’;
$block[’content’] = ‘$block_content;
} // end xxx_block_block1

Declare we have new page


<?php
function onthisdate_menu() {
$items = array();
// add a new page
$items[] = array(
'path' => 'xxx',
'title' => t('...'),
'callback' => 'xxx',
'access' => user_access('access content'),
'type' => MENU_CALLBACK
);
return $items;
}
?>

Generate the page content


<?php
function xxx() {
// content variable that will be returned for display
$page_content = '';
$query = "SELECT nid, title, created FROM {node} WHERE ...";
// get the links
$queryResult = db_query($query);
while ($links = db_fetch_object($queryResult)) {
$page_content .= l($links->title, 'node/'.$links->nid).'
‘;
}
// check to see if there was any content before setting up the block
if ($page_content == ”) {
// no content, let the user know
$page_content = “No events occurred on this site on this date in history.”;
}
print theme(”page”, $page_content);
}
?>

Create new node type


function xxx_node_info() {
return array (
'xxx' => array(
'name' = > t('Node Name'),
'module' => 'xxx',
'description' => t("Create content description"),
)
);
}


Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <div> <span> <table> <td> <tr> <b> <i> <u>
  • Lines and paragraphs break automatically.
  • You may post PHP code. You should include <?php ?> tags.
  • Use the special tag [adsense:format:group:channel] or [adsense:flexiblock:location] to display Google AdSense ads.

More information about formatting options

To combat spam, please enter the code in the image.