News:

Get Out, See Green!

Main Menu

Simple Drupal Module -- Input Filter

Started by zourtney, Oct 26, 2009, 07:45 AM

Previous topic - Next topic

zourtney

Last week I created a tutorial on the main site, Creating a Drupal Module. Here is the full source code.

testmod.info
; $Id$
name = My Test Module
description = This is my first Drupal module. Hello world!
core = 6.x


testmod.module
<?php// $Id%function testmod_perm( ){    return array( 'access testmod function' );}function testmod_get_datetime( ){    if ( user_access('access testmod function') )        return date( 'F j, Y, g:i a' );    return '';}function testmod_filter( $op, $delta = 0, $format = -1, $text = '' ){    switch ( $op )    {        case 'list':             // Return a list of possible filter operations in an             // associative array.            //     for example: array( 0 => t('testmod filter1') );            //            // If more than one is defined, determine the current             // filter operation by the contents of $delta            return array( 0 => t('testmod filter1') );        case 'description':             // Return a description of what this filter does. This            // will be displayed in the "Input formats" admin page            // under the checkbox of your filter name.            return t('Test Module, by randomland.net. Replaces                        [testmod_datetime] with the current date                        and time');        case 'prepare':             // Intended as a place to escape HTML. Prepare is called            // before process. This will usually be uncessesary.            //            // For more info visit the Drupal API page:            //     http://api.drupal.org/api/function/hook_filter/6                        // search for [testmod] or [testmod datetime]            preg_match_all( '|\[testmod((\s+)(datetime))?]|', $text, $matches );                        // loop through all matches            // [0] => array with full text of the match(es)            // [1] => array with text of everything after '[testmod'            // [2] => array with text of spaces preceding 'datetime' text            // [3] => array with text of 'datetime'            for ( $i = 0; $i < count($matches[0]); $i++ )            {                // insert current date/time if 'datetime' exists. Otherwise clear it out.                if ( $matches[3][$i] == 'datetime' )                    $text = str_replace( $matches[0][$i], testmod_get_datetime(), $text );                else                    $text = str_replace( $matches[0][$i], '', $text );            }                        return $text;        case 'process':             // This is where the filtering work is done. Return            // $text when finished.            return $text;        default:            // Return $text unchanged            return $text;    }}