How to easily create a custom module for Drupal



There seems to be a natural progression in a Drupal administrator's experience. First, he or she will just manage to get the thing installed and working. 


  • Then, they will start using contributed modules and even go so far as to use the custom features available in those, especially CCK and Views. Next, he or she will start playing around with theming in order to get the look they want. Finally, it is time to write your own custom module. Here is a brief and easy way to make a Drupal module. Further steps are found in Another tutorial
     
    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

    0 Comments
    Disqus
    Fb Comments
    Comments :

    0 comments:

    Post a Comment