Programming The Drupal Way

  • Sebastian Nievas's picture
    Sebastian
    December 10, 2013

The power and flexibility of Drupal is incredible. There is truly no limit to what you can do. More often than not, you can add functionality to Drupal without writing a single line of PHP code by installing a module or configuring one of its different settings in the administration area. This alone makes Drupal the ideal choice for building a website for someone that does not have much knowledge of PHP. However, this can be a hurdle for experienced programmers using Drupal for the first time. Here are some words of wisdom that I hope to pass on to experienced programmers that may be working with or looking to use Drupal for the first time. This will help you program the "Drupal Way".

Drupal is a content management framework (CMF). This is very important to understand. You are building a content management system on top of the Drupal core system by defining content types and installing various modules. Much like Unix, it does not make an assumption of how you're going to be using Drupal. It can be used for just about every type of website or web application out there. It is up to you to define how it will work. This is where experienced programmers can get into trouble.

Common Avoidable Mistakes

#1. Coding Too Much

It is a programmers instinct to want to break open their text editor and start writing code to display information on a site. We love building stuff! Naturally we will want to create a custom database table where we will store our application data and then build an interface so we can populate table. Drupal has a better way. By creating a content type (table), we can define fields directly from the admin interface. We can then populate this content type by using a form that will automatically be generated based on the fields we defined. Furthermore, we can install the Views module which allows us to query the database through an interface that has hundreds of options built right in, such as a pager for pagination or custom text to display when there are no results.

Before you begin writing a single line of code, look to see if Drupal has a built in way of doing it first. It will save you time and make your site more maintainable over time, saving you lots of money.

#2. Hacking Core

No matter how nicely Drupal does the work for us, we still have a desire to code something! Drupal provides a lot of programming hooks that allow us to alter the default behavior of drupal, without hacking the core code. When there is an update to the core, you will be forced to face the nightmare of merging it. This is completely uneccessary. By using hooks, you separate your business logic away from the core functions that make Drupal work.

However, another common mistake is to use the wrong hook or write bad code in one of these hooks. Using the correct hook is important because it may be executed many times on a page if you're doing heavy work in an overly general hook. This can drastically slow down the page.

#3. Reinventing The Wheel

Drupal has thousands of developers contributing to modules every day. If there's something that the Drupal core does not have or do for you out of the box, you will want to go to http://www.drupal.org and search for that functionality as it may have well been coded for you. Sometimes this is not exactly what you're looking for, but most of the time it is sufficient. Leverage and reuse the code that others have written.

#4. Working Alone

The Drupal community is one of the best communities I have ever seen. They are always willing to help and contibuting to the Drupal project. I recommend searching for a local drupal meetup group or attending a Drupal camp event to see these people face-to-face. They come from a variety of backgrounds and interests. There is a very good chance they have encountered many issues you may be facing and can provide guidance to solutions. Working with the community will help you be a better Drupal developer and citizen.

These are just a few things to help make it easy to program the Drupal way. Remember, Drupal already provides the tools to build a powerful CMS without writing much code. Use this to your advantage!