ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Drupal 7 - Creating Modules The Quick and Easy Way

Updated on April 5, 2012

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

* 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 = '') {
    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

* 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 = '') {
    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 file.

Your .info file:

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.


    0 of 8192 characters used
    Post Comment

    • hotwebideas profile imageAUTHOR

      Bruce Chamoff 

      6 years ago from New York

      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.

    • WD Curry 111 profile image

      WD Curry 111 

      6 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.


    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at:

    Show Details
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the or domains, for performance and efficiency reasons. (Privacy Policy)
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)