Drupal 7 - Creating Modules The Quick and Easy Way

Drupal 7 is changing the way that I design websites after coding PHP and MySQL websites for 16 years with various languages, but with Drupal, it is those annoying hooks that drives everyone crazy. We have to learn over 100 of them. Ironically, that is what makes Drupal module development so powerful. We just have to get over the hump with these hooks.

Well, if you have been coding PHP and MySQL for long and have some basic understanding with Drupal, especially blocks, you are pretty good to go with modules. They really are not that difficult if you are creating modules for your website and nobody else's.

The Down Side With Drupal's Contributed Modules:

It seems that there is both positive and negative feedback around contributed modules. On one hand, they help us get some functionality that we need in our websites and are readily available, because they were developed by other programmers. On the other hand, they never seem to fit the business module of our Drupal sites. These contributed modules only fit the needs of the developers who coded theme. The key to benefiting from contributed modules is being able to customize them to our website after we download them.

In this module, I will show you how to make modules that use our personal PHP experience yet keep the learning curves for the Drupal 7 hooks API down. The modules we create will produce blocks that we can add to any of our Drupal themes, so let's get started.

You will need to have some basic knowledge of Drupal blocks, because let's face it, Drupal blocks are everything in a theme. Everything else that we create in Drupal eventually gets created inside a block. Here is the simple code we will use for our module:

The simple code to display the date in a block

<?php
/**
* Implements hook_block_info().
*/
function the_date_block_info() {
  $blocks['the_date'] = array(
    'info' => t('The Current Date'), //The name that will appear in the block list.
    'cache' => DRUPAL_NO_CACHE, //Default
  );
  return $blocks;
}

/**
* Implements hook_block_view().
*
* Prepares the contents of the block.
*/
function the_date_block_view($delta = '') {
  switch($delta){
    case 'the_date':
      $block['subject'] = t("Today's Date Is ");
      $block['content'] = Date('m-d-Y');
}
return $block;
}

That's as simple as any Drupal module can get. You really need nothing else.

Out of the 100+ Drupal 7 hooks that I can put into my module, I have added the only two that will ever need to produce blocks: hook_block_info() and hook_block_view(). That's it!

  1. hook_block_info() simply produces the information that user sees on the block page.
  2. hook_block_view() simply produces the output of your PHP.

You're done! Yes, I know that some of the more experienced Drupal module developers will tell me that I can use other hooks, but like I said, this is for simple modules and to break people into the art of Drupal module development. Since almost everything in Drupal ends up in a block, I think that these are the two most important hooks anyone needs. Let me know if you disagree with me in the comments. I am open to everyone's opinion.

Ok, now copy my code into your .module file. Whatever YOUR Drupal module is doing, like doing simple math for example, name your .module thedate.module. If you use a different file name, you will need to change your hook function names. For example, a module that outputs a loop from 1 to 100 can be called loop100.module. In your code, you will rename hook_block_info() to loop100_block_info and hook_block_view to loop100_block_view and then have the following code:

Here's A Simple Module For A Loop that outputs to 1 to 100

<?php
/**
* Implements hook_block_info().
*/
function loop1to100_block_info() {
  $blocks['loop1to100'] = array(
    'info' => t('1 to 100'), //The name that will appear in the block list.
    'cache' => DRUPAL_NO_CACHE, //Default
  );
  return $blocks;
}

/**
* Implements hook_block_view().
*
* Prepares the contents of the block.
*/
function loop1to100_block_view($delta = '') {
  switch($delta){
    case 'loop1to100':
      $block['subject'] = t("A list from 1 to 100");

$html = "";
for($n=1; $n<=100; $n++)
{
$html .= "$n<br/>";
}

      $block['content'] = $html;
}
return $block;
}

This second module is a little more complex than the first, because it contains a loop which concatenates my $html variable. Then, the $html is assigned to $block['content'] which is what people see on your Drupal site when you add the block to your theme. It is still simple. You can add MySQL and other PHP coding to the $block['content'] element.

Now, all you need to complete this 3 simple links for your .info file. The .info file name will match your .module name, so your loop1to100.module has a loop1to100.info file.

Your .info file: loop1to100.info

name = Loop from 1 to 100
description = Creates A List from 1 to 100
core = 7.x

Take this text and customize these 3 simple lines and save it. Then, zip your .info and ,module files into a folder and install in your sites/all/modules folder or use your module installer that comes with Drupal 7.

This is it folks. As you can see, creating Drupal 7 modules is easy. I hope you enjoy this hib and it starts off some people on creating some cool Drupal 7 modules the easy way.

More by this Author


Comments 2 comments

WD Curry 111 profile image

WD Curry 111 4 years ago from Space Coast

This is the problem. Drupal may be powerful, but it is lame. It is Quark. It isn't fully developed for professional design with all of the modules, modes and the like. There are too many frustrating, time consuming, weird steps if you are used to Adobe.

It is wrong to reward them with so much acceptance. How about all of the web experts go on a "use the best systems" campaign and get rid of this junk? Time is money, and what you see, is what you get , is what we need.


hotwebideas profile image

hotwebideas 4 years ago from New York Author

Hey WD, good comments and points. I am glad to see someone stand up to some of Drupal's downsides. I agree with you on some levels and get frustrated with Drupal sometimes, but there are millions of sites using it and its most powerful feature is its flexibility to adapt to any type of content. Thanks for your valid points.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working