Leveraging Drupal's Open Source Ecosystem

Submitted by Benjamin Bradley on .

Real Magic Isn’t Perfect

As someone who had been hand-coding web applications for years, I thought I’d found a magic wand when I discovered the world of open-source software. I no longer needed to laboriously recreate each website from scratch. With the flick of a wrist, I could now whip up a website in minutes — a task that used to take hours. But only movie magic is perfect. In the real world, every magic has its tradeoffs.

Modern content management systems like Drupal provide a lot of functionality out of the box. For a basic website, they might do everything you need. But chances are, as your website and its interactions get more complex, you’ll need to enhance the basic CMS functionality with additional logic. As is usually true with software, there are several ways to implement your desired changes, and each approach has its pros and cons.

First and whenever possible, use existing components to satisfy your use case. Content types and views are two of the major building blocks of Drupal. These components are flexible and powerful, and can handle the majority of use cases for data collection and display needs. You can add data fields (like an image or a number) to your custom content types and the system will automatically generate a content entry form. Display of individual records can be customized with the Display Suite module, and lists of records can be filtered, sorted and displayed through Views module. This all can be managed through your website’s administration user interface (known by developers as “the UI”), with no need to touch the code.

Every piece of code you add brings its own baggage in the form of possible bugs, security vulnerabilities and maintenance updates. When you use existing components rather than a module, you avoid adding additional baggage to your system.

When your requirements need some custom logic, if the needs are simple, consider using Drupal’s Rules module. This module excels at event-based reactions, like setting a field value, or sending an email. For example, a rule could be created and configured to send your website manager an email whenever a new feedback form is submitted.

The Rules module can be administered entirely through the Administration UI, and its logic is stored in a managed structure. What this means is that the Rules module itself knows how your custom logic is put together and can upgrade those rules if needed when the Rules module is updated.

If the Rules system gets you most of the way there, but doesn’t do exactly what you need, it is possible to find or create modules to enhance the Rules system. Polycot Associates created the Book Rules module to fill in a gap in the Rules system and enable data to sync automatically between several related pages.

As the required logic gets more complex and touches different parts of the system, an open-source module contributed by other developers may satisfy your needs. Half of the benefit of using open-source modules is in what it provides out of the box. Another benefit is the other developers who are using and maintaining that same code. Watch out for modules which have been abandoned by their developers. Without the attention of other open-source developers, the maintenance of this code is equivalent to custom code -- you’re on your own.

Check out the statistics on the module’s project page for some quick qualifiers to determine the longevity of a module. The listed Maintenance Status and/or a recent release will show you that it’s still getting updates on a regular basis, and the Number of Users indicate the relative strength of the open-source community around the module. For example, a module used by less than a dozen sites is likely to get updates and fixes much slower than a module used by tens of thousands of websites. Check out this article for more info about evaluating Drupal modules.

If none of the existing modules do what you need, you can always write your own code. Modern CMSes are well-documented and a busy community of developers working on the same platform means that support forums are filled with useful FAQs.

One trouble spot that all developers must grow out of is the tendency to collapse theme and function. Your theme, which configures how the website looks, should be structured in such a way that it can be changed without losing the core functionality of the website. Any structural elements (relating to data structures, storage, and security/access permissions) should be implemented outside of your theme, usually in a custom module or plugin. If functionality is erroneously baked into your theme, the next time you want to rebrand your website, you’ll lose that functionality and risk breaking the site.

One drawback of custom code is that it is brittle. Since you’re the only one maintaining the code, you are the only one who can fix it when something breaks. A breakage in this case might be caused by a bug that only surfaces in a rare edge case, or an upgrade of another module that yours depends on.

If you found an existing module that almost does what you want, you can modify it and contribute a patch back to the module’s maintainers to be included in the next version. This gets you the benefit of implementing the custom code that you want, and by contributing it back into the module, it becomes part of the shared codebase that anyone can benefit from - and fix or enhance. If you’re unsure of the best way to implement a change, ask for advice from the module maintainers. With their guidance, your contribution is more likely to be incorporated into the module moving forward.

If the new behavior you’re adding merits its own new module, and has a general purpose use or can be easily adapted to similar use cases on other websites, it makes sense to publish the module into the open-source ecosystem. By publishing your module on Drupal.org, it becomes more visible to others who would like to leverage the work you’ve started, and makes it available for others to report and fix bugs, and suggest or implement enhancements.

The Drupal website has great guides on how to create a module for Drupal 7 and Drupal 8.

Open-source software provides many benefits, cutting initial development time to a fraction of what a custom-coded solution might cost. Working with open-source is like joining a herd of animals. There are great benefits to being part of the group, but you’ve got to stay close and keep up to stay there.