Login

Creating a Drupal Hello World Module

We just posted our new online tool which generates a full custom Drupal module. This post talks about that tool and how modules fit in to the Drupal ecosystem.

A little background first. Occasionally Jason and I will answer questions on the Drupal.org Module Development and Code Questions forum. Some time last year, someone asked for a "Hello World" module.  A "Hello World" application is a barebones implementation of any development environment. If you read any books that cover programming languages they inevitably start with a "Hello World" example. So on the Drupal.org forum, I posted a simple "Hello World" module for Drupal 7. That post generated an unusual amount of interest. Therefore we decided, rather than just repost the "Hello World" example, we would create a tool to allow folks to generate their own.

So our online tool allows you to name the module and customize the package name. The module itself, is still only going to do 2 things:

  • Creates a menu item link (so folks can click and invoke the hello function).
  • Creates a custom function which simply displays "Hello World"

However, for a developer, the tool

  • Creates the proper directory structure and name.
  • Creates the proper file listings.
  • Creates a custom "info" file that allows you to install your custom module in your Drupal installation.
  • The source code includes an implementation of hook_menu().

So the developer gets a starter it. You can add addition functions, menu items etc. The proper naming conventions are started for you (by our tool).

A few details you should know about Drupal modules and functions:

  • You can't have more than one function with the same name. for example "sayHello()". Therefore, our tool prepends your module name to the helloWord function.
  • When an adminstrator navigates the the "Modules" section of your Drupal website, the adminstrator is provide with a screen that installs or uninstalls modules. On that Admin screen your custom module is displayed by a couple items:
    • Package Name -- Kind like a category.
    • Description - A short (approx one sentance) summary identifying your module
    • Module Name -- The module name cannot contain embedded blanks (e.g. My Module) or dashes (e.g. My-Module). Use underscores to separate words within your module name (I advise to do the same for function names) (e.g. My_Module).
  • Just like functions, you can't have more than one module with the same name. Therefore, please don't use our tool to name your module "node" .  Node is a core module provided with your base Drupal installation.

Important! When using our tool you need to choose the correct Drupal major version (6 or 7). If you try to install a Drupal 6 module on Drupal 7 your installation will fail. The same for vice-versa, an attempt to install or run a Drupal 7 module will fail on a Drupal 6 site.

Quick Installation summary:

  • Download zip file created by our tool to your local machine.
  • Extract the contents of the zip file in the sites/all/modules directory of your Drupal installation.
  • Login to your Drupal installation as the Adminstrator.
  • Navigate to the Admin > Modules section of your site. Drupal will display a list of currently installed modules (checked) and modules availabie for installation (unchecked).
  • Locate your custom module and enable it by setting the checkbox.
  • Click the "Save Configuration" button at the bottom of the screen.

Running the custom module:

  • Navigate to the front page of your Drupal site.
  • The main navigation menu has a new menu item titled "Invoke HelloWord - Module(NAME OF YOUR CUSTOM MODULE)".
  • Click on the new menu item. Your site displays the following message "This is a simple Hello World Proof of Concept".

Finally. we had to place a re-captcha challenge along wit a limit of 5 requests within an hour. Like all sites on the internet, we get spammers who attempt to post,  or bring the site with a Denial of Service (DOS) attack . Thus, after creating 5 modules with our tool, our site will tell you to come back in an hour if you still need more attempts.

As always. back up your site before making any changes (like installing a module).  As a developer, you should always test on a local installation first. A local installation is a copy of your website that runs on your laptop or desktop computer. Once you've tested the module on your local installation, then install or your remote or production (live) site.

Please feel free to post any questions or comments using the comment facility on our site.  Hope this helps.

Lorin

Comments

Nice and Easy

I just generated a Hello World module with your tool and installed it on a Drupal 6 development site (running on my laptop). Worked like a charm. Any thoughts on enhancing this tool to provide some of the standard node hooks?

It is under consideration

Hi, First thank you so much for using our tool and providing feedback.

Sure we will consider enhancing the tool. We encourage folks like your self to provide feedback here so that we can get an indication of what folks most want to see added. Therefore a question. Are your a software developer or more of a themer (graphic designer)? Are you looking for any specific hooks?

How about the node hooks used in the "Examples for Developers"

Actually I am a software developer, not a "themer".

I would take a look hooks used in the node examples published at "Examples for Developers" http://drupal.org/project/examples. I think a lot of developers would find it useful to get a boilerplate of the common nook hook (over-rides).

Again thank you.