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.
- Know where custom modules go, not in /modules but in /sites/all/modules
- Create a new folder there, e.g., new.
- Open a text editor and create a file named new.info
- In new.info add the following lines:; $Idname = Newdescription = A new module for testingpackage = Othercore = 6.x
-
Create another file named new.module
-
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];
}
} -
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.
-
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.
-
The possibilities with modules are nearly endless, I recommend you pick up Pro Drupal Development for more instructions.
Comments
Anonymous
Fri, 11/26/2010 - 03:15
Nice
thankyou so much yaar.......
I just tried my 1st module .......
Rocio
Thu, 01/06/2011 - 10:05
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.
Drupal Caulfield
Thu, 01/06/2011 - 10:42
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.
Anonymous
Thu, 01/27/2011 - 03:46
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.
Drupal Caulfield
Fri, 01/28/2011 - 01:11
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.
Daria
Fri, 06/03/2011 - 08:14
Coder in the Rye, nice
Coder in the Rye, nice article - thank you, found some useful staff here
Anonymous
Fri, 08/12/2011 - 00:59
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
Wed, 10/26/2011 - 11:42
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.
Pablo P.
Wed, 04/04/2012 - 14:14
So far so good!
so far so good. thank you a lot
Anonymous
Mon, 04/30/2012 - 08:59
Thanks!
Thanks!
Anonymous
Tue, 07/10/2012 - 04:47
Great!
Thanks for your help. It works fine. And it gives me some idea how to create custom module. Thanks
Anonymous
Tue, 07/10/2012 - 04:48
Thank you
Thank you
Anonymous
Mon, 08/13/2012 - 23:39
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.
Drupal Caulfield
Thu, 09/13/2012 - 15:50
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