Two web artists. One amazing company.

Understanding how custom code can help you build your Drupal site

from Michael Prasuhn on June 27, 2013 02:50pm

Many times, building out the features on a Drupal site seems easier with contributed modules. Why reinvent the wheel? Next thing you know, you're standing on your head trying to read module code backwards to understand it. Hours in you're making custom tweaks to keep your client happy and spending more time tweaking and overriding behavior than if you had just written the code for the feature from scratch.

Shortly after Drupalcon Portland wrapped up, I came across an excellent blog post by Mike Crittenden: Drupal and "Invented here". If you haven't read it yet, I'd highly recommend checking it out. Not only is it funny and entertaining, it is also quite informative and describes very well the dilemma presented to Drupal developers of using contributed modules versus writing custom code.

Most of the time I tend to lean towards custom code for lots of small Drupal tasks as I find it typically leads to a highly performant site, with easy to understand features. Just as an example I took the time to build out the feature described in Mike's article and it surprised me how little code it required. Here's the Drupal 7 version:

Language          Files       Code    Comment  Comment %      Blank      Total
----------------  -----  ---------  ---------  ---------  ---------  ---------
php                   2         66         15      18.5%         16         97
html                  1          1          0       0.0%          0          1
----------------  -----  ---------  ---------  ---------  ---------  ---------
Total                 3         67         15      18.3%         16         98

That's right, just 67 lines total, generously formatted. Keep in mind that this is with a bare bones template and no preprocess function since those portions would be the same even if the developer or site builder used Views. This took about about thirty minutes with my somewhat rusty development skills (I admit to stopping to check several times).

I thought about some of the concerns raised in the comments on Mike's post and the biggest one seemed to be the ongoing task of maintaining the code and the especially daunting task of updating it to Drupal 8.

So I went ahead and ported it to work with the latest Drupal 8.x. This took a little bit longer, perhaps and hour and a half total. Finding information on upgrading is still a little tricky as all the documentation is spread throughout numerous change notice records on I suspect with time, upgrade documentation will be improved and ideally there would be a way to read documentation that incorporates all change notices affecting a given area, such as forms, or routing/controllers. So how much code did this take in Drupal?

Language          Files       Code    Comment  Comment %      Blank      Total
----------------  -----  ---------  ---------  ---------  ---------  ---------
php                   3         67         32      32.3%         27        126
html                  3          3          0       0.0%          0          3
----------------  -----  ---------  ---------  ---------  ---------  ---------
Total                 6         70         32      31.4%         27        129

Keep in mind again with this conversion, that some of my time was spent converting the simple template to the TWIG format, which would be required even if you used a custom template with Views.

Overall this was an enlightening exercise, while the time taken slightly exceeded my initial estimates, the amount of code required was far less. Custom coding in Drupal is still a very useful way to build out features for your site, and is one of the core concepts of my upcoming book Model Your Data with Drupal. If you want to learn more about the book, using custom code with Drupal, or receive updates and discounts, sign up below, or at

blog comments powered by Disqus