How to Create a Basic Drupal Module



Creating your own Drupal module is quite easy. In this tutorial I’ll walk though the bare essentials of creating a module. Even an ‘empty’ module is useful for web projects, being that they serve as a great place for code snippets, quick experiments with the Drupal API and a place for simple pages.
In order to create a module, you’ll need the following:
directory and file structure of a Drupal module
Directory and file structure of a Drupal module
A directory and three files:
  1. Create a directory inside the sites/all/modules directory to hold your custom module. The name of the new directory needs to be the same as the name of your custom module. For example, using 
  2. the name ‘yourModule’ as the directory and module name gives you an information file named ‘yourModule.info’ and a main code file named ‘yourModule.module’. Your module’s PHP code is in ‘yourModule.module’.
  3. Create a text file inside sites/all/modules/yourModule named ‘yourModule.info' and paste this text into it:
    ;$ID$
    name = "Your Module"
    description = "Place to put PHP functions for Drupal customizations."
    core = 6.x
    php = 5.1
    (note: I'm assuming you're working with Drupal 6, and with a host that supports PHP 5.1...)
  4. Create a 2nd text file inside /sites/all/modules/yourModule named ‘yourModule.module' and paste this into it:
    <?php
    /*****************************************************************************
    * Implementation of hook_block()
    * @see http://api.drupal.org/api/function/hook_block/6
    * create a minimal block
    */
    function yourModule_block( $op = 'list', $delta = 0, $edit = array()) {
    switch ($op) {
    case 'list':
    // appears on the Blocks configuration page admin/build/block
    $blocks = array();
    $blocks[0]['info'] = t('My Block');
    return $blocks;
    case 'view':
    // content of block; required to have 'subject' and 'content' elements
    $blocks = array();
    $blocks['subject'] = t('<center>My Block Status</center>');
    $blocks['content'] = t('<center>My Block Content</center>');
    return $blocks;
    }
    }
  5. Navigate on your site to admin/build/modules and locate your new module's entry... click the checkbox to activate the module and click the "Save configuration" button.
  6. Navigate on your site to admin/build/block and scan down the page for a block with the name you gave in the ‘list’ case of the switch statement above. Try moving your block to one of your theme’s regions, then scroll down and click the ‘Save blocks’ button to enable your new block. 
This is pretty bare bones, but there is one thing worth pointing out before continuing: notice the PHP from step three has no closing PHP tag. This is on purpose, and is a Drupal convention. If interested, you can learn why here. Other than that, the above implements your own module, and is a basic little block.
Any PHP code snippet can be thrown into yourModule.module, 
function show_table($table = NULL, $rows_per_page = 20) {
if (!$table || !db_table_exists($table)) {
drupal_set_message(t('You must supply a valid database table name.'), 'error');
drupal_access_denied();
}

// We get the first (or only) part of the Primary key to be added to the sort sequence.
$result = db_query("SHOW INDEX FROM {$table}");
$x = db_fetch_array($result);
if ($x === FALSE) {
drupal_set_message(t("The '@table' table has no index defined. This is probably normal.",
array('@table' => $table)), 'notice');

$first_key = NULL;
}
else {
$first_key = $x['Column_name'];
}

drupal_set_title(t('@table Table Contents', array('@table' => ucwords($table))));
$output = '<p>'. t('Click on a column title to sort by that column.') .'</p><br/>';
$rows = array();

// Now we get the column names from the table and build the header.
$header = array();
$result = db_query("SHOW COLUMNS FROM {$table}");

while ($col_desc = db_fetch_array($result)) {
$header[] = array(
'data' => ucwords(str_replace('_', ' ', $col_desc['Field'])),
'field' => '`'. $col_desc['Field'] .'`',
);
}

// Get the data rows from the table.
$select = "SELECT * FROM {$table}";
// Set it up so that the user can sort on any column,
// but the primary key will always be the last value to sort on.

$select .= tablesort_sql($header) . ($first_key ? (', '. $first_key .' ASC') : NULL);
// Do the query so that we can page the data.
$result = pager_query($select, $rows_per_page);

while ($row = db_fetch_array($result)) {
$line = array();
foreach ($row as $key => $value) {
// We use check_markup to apply our filters.
$line[] = check_markup($value, FILTER_FORMAT_DEFAULT,TRUE);
}
$rows[] = $line;
}

// Build the displayable table.
$output .= theme('table', $header, $rows);
$output .= theme('pager', $rows_per_page);
return $output;
}
You use this function inside PHP tags in node content saved with the PHP filter.It generates a handy, sorting table from any table in your database:
<?php echo show_table( ‘filter_formats’, 5 ); ?>
The above in one of your nodes gives you this:

Click on a column title to sort by that column.
FormatNameRolesCache
1
Filtered HTML
,1,2,3,4,
1
2
Full HTML
,4,
0
3
PHP code
,,
0
4
Full HTML and PHP, no auto-links
,4,
0
.
Pretty nice. 
This should give you a pretty good start making modules. I suggest you check out api.Drupal.org for more information. As usual, if you have any questions, please ask.


0 Comments
Disqus
Fb Comments
Comments :

0 comments:

Post a Comment