Saturday, May 5, 2012

How to easily create a custom module for Drupal 6?


Let's break this down into a few easy steps.
  1. Know where custom modules go, not in /modules but in /sites/all/modules
  2. Create a new folder there, e.g., new.
  3. Open a text editor and create a file named new.info
  4. In new.info add the following lines:
    ; $Id
    name = New
    description = A new module for testing
    package = Other
    core = 6.x
  5. Create another file named new.module
  6. In new.module add the following lines:
    <?php
    /**
    * Implementation of hook_block().
    * Adds a block that has new text.
    */
    function new_block($op = 'list', $delta = 0, $edit = array()) {
      switch ($op) {
        case 'list':
          $blocks[0]['info'] = t('New Module');
          return $blocks;
        case 'view':
          $block[0]['subject'] = t('New Module');
          $block[0]['content'] = t('Hi from your custom block and your first new module');
           return $block[$delta];
      }
    }
  7. Make sure both files are saved and then in Drupal go to Site Building -> Modules. Look under the other category and you should see your new module listed. Just check the box for it and submit.
  8. You now have a new module which creates a custom block, you can go to Site Building -> Blocks and you should see your new module block. You can select to enable it and add it your pages. 
  9. The possibilities with modules are nearly endless, I recommend you pick up Pro Drupal Development  for more instructions. 

No comments:

Post a Comment