How to easily create a custom module for Drupal 6

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 Pro Drupal Development

 

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. 

 

Comments

Nice

thankyou so much yaar.......
I just tried my 1st module .......

thanks

Hello,
Thanks so much for your help, though I can't implement it, when I go to the admin/modules the module appears with a red x and says that this version is not compatible with drupal 6.2 version... Don't know what to do next.

Well, I just tried it again

Well, I just tried it again myself on a fresh install and it works fine. Make sure that you named folder the module is in "new", that it is located at "sites/all/modules/new" and that the filenames are "new.info" and "new.module"

Also, make sure you copied and pasted above code in the post, just in case you may have made any typos. Good luck.

just type 6.x in info

just type 6.x in info

I want to put html, js, css in my new module

Could you help me more now i create new module already now i want to put html, js, css in my module

thanks for your help.

Sure, this is what drupal_add

Sure, this is what drupal_add_js http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_ad... and drupal_add_css http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_ad... are used for. As for the HTML, you will want to add it in the block content. So instead of 'test module' you can put '<p>My html test module</p>' Give it a try. Maybe I should write a blog post about it.

Coder in the Rye, nice

Coder in the Rye, nice article - thank you, found some useful staff here

Drupal module admin management

I added the new custom module in sites >> all >> modules >>
it displayed fine. But i want to display the data for that module from backend. i.e In admin i have to add some content then it will display in my module file.

how can i achieve this?

Stephen

I think this is a nice idea. I like the idea of a new costume drupal 6. It will help me a lot of this. Thank you for the source that you share.

So far so good!

so far so good. thank you a lot

Thanks!

Thanks!

Great!

Thanks for your help. It works fine. And it gives me some idea how to create custom module. Thanks

Thank you

Thank you

thanks

i am done upto 6th step now how i can do to ,,,
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.

In the menu, click on Site

In the menu, click on Site Building, then click on Modules. This will list all the modules that are in your sites/all/modules folder. If you created your new module correctly, it should show up on that page. So to turn it on, you just have to check the box for it, then save.

Add new comment

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.

Comment using an existing account (Google, Twitter, etc.)