• Home
  • About Us
  • Resources
  • Articles
  • Site Update Info
  • Contact
Home
Home

Lesson 1: How to Build Your First Drupal Module

PostDateIcon January 1, 2011 | PostAuthorIcon Katy
  • [View]

1. Building Drupal modules isn’t as hard as it sounds. You don’t need to be an advanced PHP programmer or hard-core Drupal dev to create custom modules. In this short tutorial, I will show you how to build your first Drupal module. This tutorial is aimed at web designers and other non-programmers. You do need to have experience in building Drupal websites and knowledge of basic PHP is helpful.

2. Let’s look at a sample module called Show Version. I created a small module to display the current version of Drupal. Click here to download the module files
a. Unpack the zip file and copy the show_version folder to your sites/all/modules folder

b. Enable the module called Show Version. Go to the admin/build/modules page. If you use the Admin Menu module, select Site Building/Modules in the drop-down menu. Look in the "Other" category.

c. Add this path to your site URL to see the new Drupal page showing the current Drupal version. /show_version

d. Test this module online at http://www.mydrupal.ca/show_version

3. A Drupal module is made up of (at the very minimum) these 2 essential files, .info and .module

Open the show_version folder and you will see 2 files:
a. show_version.info
b. show_version.module

.info tells Drupal 6 the pertinent info about your module like module name, short description, Drupal core version and your module version. This info shows up on the /admin/build/modules page where you enable and disable your modules.

.module file contains the code for your module.

4. Let’s look at the .info file. Open show_version.info in your plain text editor.

The first line, starting with a semicolon, is a comment with information for version control when you contribute a module to drupal.org. For now it just contains the name of your file and some odd looking character strings.

; $Id: show_version.info $

The next four lines contain info that will show up on the admin/build/modules/page

name = Show Version
description = “Displays current Drupal version”

core = 6.x – this is the version of Drupal core that this module works with.

version = “6.x-dev” - this is your module version number. Start with the Drupal version and followed by dev or 1.0

Note: The name of the folder, .info and .module files all need to have the same name.
sites/all/modules/show_version
show_version.info
show_version.module

5. Let’s look at the show_version.module

The first line is the <?php opening tag.
The 2nd line, starting with //, is a comment with information needed for version control for when you contribute your module to Drupal.org.

/**
* @file show_version.module
* The show_version module displays current Drupal version.
*/
This section shows comments for Doxygen – a standard documentation system. Doxygen generates documentation from source code. It’s a good practice to follow.

/** Implementation of hook_menu
Drupal modules are built with a system of hooks. This allows your new module to hook into the existing Drupal framework. For example, your module needs to hook up with the Drupal framework so you can add items to your Primary menu or give the user permission to view your new page. You will need to replace the word ‘hook’ with the name of your module. This is the machine-readable name of your module.

For example, the function name, hook_menu(), will become show_version_menu()

So basically we have a PHP function called show_version_menu()
It sets up an associative array with several items.
-‘title’ sets up the page title which appears in the Browser title and the Drupal page title.
- ‘page callback’ calls the function (show_version_page) that generates the new page
- ‘access arguments’ will give the user access to your new page

The next function (show_version_page) generates the new page and prints the Drupal version on the page.

Drupal has a PHP constant called VERSION. So this function returns the text Drupal followed by the Drupal version number.

If you’ve done any PHP programming, you will notice that the php ending tag is missing. It’s not necessary to end your module with the PHP ending tag ?>

6. To see this module in action, you will need to enable it. Go to admin/build/modules and in the Other section, enable Show Version.
Then type this URL in your browser address bar: /show_version

Assignment:
Your assignment is to modify this module and create a page that says Hello World. Let me know if you have any questions or comments.

Follow these steps:
1. Copy show_verson folder and rename it hello_world
Rename the .info file to hello_world.info
Rename the .module file to hello_world.module

2. Open hello_world.info
Put correct file name in the first comment line
Put the correct module name and description
Save.

3. Open helloworld.module
Edit this file to look like the following code and Save your work.

<?php
// ; $Id: hello_world.info $

/** @file hello_world.module
* The hello_world module says Hello World */

/** Implementation of hook_menu() */

function hello_world_menu() {
$items = array();

$items['hello_world'] = array(
'title' => 'Hello World',
'page callback' => 'hello_world_page',
'access arguments' => array('access content'),
);

return $items;

}

/** Generate the Hello World page */

function hello_world_page() {
return 'Hello, World!';
}

NOTE: Be sure to save your work as hello_world.module

4. Go to admin/build/modules and enable your new module. It will be found in the Other category.
5. Type in this URL to see your new module in action: /hello_world

A good video tutorial by Chris Shattuck inspired this tutorial.
http://buildamodule.com/drupal-video/build-your-first-drupal-module-video

Bookmark/Search this post with:
PostCategoryIcon Digg Digg  | PostCategoryIcon del.icio.us del.icio.us  | PostCategoryIcon Google Google  | PostCategoryIcon Yahoo Yahoo  | PostCategoryIcon Facebook Facebook  | PostCategoryIcon Twitter Twitter
PostCategoryIcon Printer-friendly version  | PostCategoryIcon Send to friend   | PostTagIcon Tags: Build a module, Drupal Study Group

Great Lesson!

Submitted by Frank Gatlin (not verified) on January 10, 2012.

This was very easy to understand and straightforward.

However, my second time around I am stuck.
Are there extra steps to take when coding a Drupal 7 mod? When I changed the version number over to 7, I get the entire "Hello World" code written across the top of ever page.

Thank you so much for this lesson!

  • reply

GREAT JOB FOR YOU!!! The

Submitted by Visitor (not verified) on February 20, 2012.

GREAT JOB FOR YOU!!! The digital volcano vaporizer is one of the latest inventions of vaporizers today. For the first time on the market it was introduced in 2007.

  • reply

Thank you very much

Submitted by Visitor (not verified) on June 2, 2011.

Hi Katy
thank you very much for your great lesson
really useful.
with regards
najm

  • reply

Hi Tena, Thanks for working

Submitted by Katy on January 25, 2011.

Hi Tena,
Thanks for working through my tutorial. And thanks for letting me know about the mistake in the $Id line. I will fix it right away.
Katy :-)

  • reply

Great first assignment!

Submitted by TenaMurphy (not verified) on January 21, 2011.

Hi Katy - thanks so much for spelling this out in clear and easy terminology. FYI - in the assignment step 3, the $Id line still says show_version.info. You should probably change that to hello_world.info to avoid confusion. Thanks again!!

  • reply
Post new comment
The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd><pre>
  • Lines and paragraphs break automatically.

More information about formatting options

Subscribe by RSS
Syndicate content
Recent blog posts
Lesson 2: How to Create Site Update Info
Jan 25, 2011
Lesson 1: How to Build Your First Drupal
Jan 01, 2011
Drupal Study Group - Learn Custom Module
Jan 01, 2011
My Top 6 Free Themes for Drupal 6 for 2010
Dec 31, 2010
New site upgrade with new Drupal Training
Dec 31, 2010
Recent comments
  • If there are problems with
    Feb 20, 2012
  • GREAT JOB FOR YOU!!! The
    Feb 20, 2012
  • VERY INTERESTING ARTICLE!!!
    Feb 20, 2012
  • Thank you for sharing. radyo
    Feb 18, 2012
  • Thank you for sharing. radyo
    Feb 18, 2012
Popular blog posts
Drupal Advanced Forum vs. phpBB Forum 134,444
How to Convert PSD to Drupal Theme 15,144
Newsletter Subscriptions for Drupal 6 3,607
How to Kill Spam on your Drupal Contact Form 3,577
My Top 5 Drupal Themes 3,409

Copyright © 2009-2011 SeascapeWebDesign.com