You are here

How I Built The Simplest Possible Custom Drupal 7 Module

DrupalOne of the toughest things about Drupal is learning how to write a custom module. They say the easiest way to get started is by looking at existing code taken from the Drupal core as well as contributed modules, study it, and then modify it to suit your needs. So that's exactly what I did, and will share here what I learned while writing the simplest possible module imaginable. It involves only four lines of code, which is really just two lines inside a function, that originally was just ONE line. Simple enough? Ok, let's get started.

First, let's briefly go over the basic structure of Drupal modules. They can have as little as two files, the .info file & .module file (which are also just text files). The .info file is so easy that I'm not even gonna bother going over it (and if you can't even figure this out yourself, you might as well quit Drupal right now). We'll look at the .module file, which is where the magic happens. The module I modified is something called Remove history. Take a look at the before & after pictures and you get the idea. Simple enough module huh?

Before

Before

After

Now since at the time of this writing it's only available for Drupal 6 and I needed it (because I'm also using a module called Author Pane, which presents this information again but in a much better way on the same user profile page as you can see from the screenshots above) for my music startup which is built in Drupal 7, I thought I'd write the code myself as I only had to figure out three lines of code (but again, technically just one :D). So let's look at how I did it. Here's the original Drupal 6 version (available for download):

function remove_history_profile_alter(&$account) {
  unset($account->content['summary']);
}

Now here's my Drupal 7 version (available for download):

function remove_history_user_view_alter(&$build) {
  $build['summary']['#title'] = NULL;
  $build['summary']['member_for'] = NULL;
}

So how did I do it? First, a disclaimer. I have built a website in the past from scratch using PHP & MySQL that once had over 10,000 members (which I am essentially rebuilding after pivoting the original idea, mentored by the best CEOs that San Diego has to offer at a Silicon Valley-based technology startup incubator called the Founder Institute, and using Drupal after having learned from my initial mistakes). So while not a maven, I'm definitely not a neophyte either when it comes to web development. However, although knowledge in PHP really helps in understanding Drupal (which is a CMS framework built using PHP), the goal of this post is to help newbies get over their initial fear of module development. You start with an easy module such as this one, and work your way up to whatever it is you need to eventually build for your website. You learn to walk before you fly, ok?

So all I did was the following:

1) Look for where the code is that you're trying to alter. In my case, all I needed to do was find where the code was that took care of user profile pages. I looked inside the Drupal core folder, then in the modules folder, and then in the user folder, and found a file called user.module. I found what I needed starting at line 969, a function called user_user_view($account). Even if you don't understand exactly what the code inside this function means, you can easily tell that this code is what you need to alter just by looking at lines 981 (t('History')) & 985 (t('Member for')).

2) I looked at an example from the book Pro Drupal 7 Development on chapter 6, which is called Working with users. I studied the code at pages 128 & 129, a function called loginhistory_user_view_alter(&$build). The rest was just trial & error.

I mentioned a book here. There are actually three books I highly recommend that will help you become a really good Drupal developer. Depending on your current skill level with Drupal:

1) The Definitive Guide to Drupal 7: Basic, intermediate. If you're a newbie, look no further. This is THE book you've been waiting for.

2) Drupal 7 Module Development: Advanced. This is currently known as the best book to learn how to write custom Drupal 7 modules.

3) Pro Drupal 7 Development: Advanced. This used to be the best book to learn how to write modules in Drupal 5 & 6, but the newest edition was rushed to coincide with the launch of Drupal 7 and thus doesn't cover some very important topics in Drupal 7 (such as entities, RDF, etc.) and therefore from what I understand is no longer considered the best (but still pretty damn good).

So that's all folks! I hope my post inspires a lot of newbies to take their Drupal skills to the next level. I also hope that Drupal gurus leave a comment if they feel that the code I wrote is not accurate or if there is a better / different way to achieve the same goal (there are 10 ways to do just about anything in Drupal). I am currently still mainly a Drupal site builder, and have lots to learn before I can call myself a Drupal developer. But I'm finally getting there, and so can you. :-)

You are here