Building Custom Modules in Drupal 8 - Best Practices to Follow

Parent Feed: 
  • By : Ganesh
  • Date : 28-03-2018

Recently, I was looking for a module in Drupal and something just crossed my mind. In the Drupal world, when you are looking out or considering for solutions, the first thing you might hear is, "there is a module for that!". While there are close to just 40 core modules in Drupal, it is the additional contributed modules built by the Drupal community that extends the platform's functionality. And with Drupal 8 (the latest version of Drupal), there is a completely different architecture and modernized approach towards CMS , with a shift towards object orientation to improve the overall efficiency and software quality.

Why do you have to build a module?

A custom module is essentially built to extend the core functionality of Drupal; which by itself comes with built-in set of core modules. However, the true nature of Drupal mainly comes from hundreds and thousands of custom modules developed by the dedicated community.

Regardless of how your experience has been with the previous versions of Drupal, module development in Drupal 8 has its own set of challenges. To give you a fair idea, there are some differences between Drupal 7 and Drupal 8, the key ones being that Drupal 8 requires the latest version of the php5 to run and it uses Symphony, a php framework that relies on Object Oriented Programming. Also, there are changes in the way a module is built and there are certain best practices to keep up with the community standards. Let us talk a bit more about this, and some of these practices for Drupal 8 module development.

File Structure

One of the major things to be aware of, in Drupal 8 module development is the file structure which is quite different from the one in Drupal 7. In Drupal 8, the custom modules built are stored in the /modules directory, unlike in Drupal 7 where this directory was reserved for the core modules. The core modules of Drupal 8 now go into the /core directory.

Next up, to let Drupal know that your custom module exists, you need to create a .info.yml file which provides the meta data. This process is similar to the one used for Drupal 7.

name - This key is to provide a name for your custom module.

type - This key defines the type of the extension (module/theme/profile).

description - This key provides the detailed description about the custom module (displayed on the module list page).

package - This key specifies the package in which the module should be included.

version - Specifies the version of the module.

If your file is something like 'blogexmple_module.info.yml', the syntax will be as follows.

name - Blogexmple Module

type - module

description - A simple demo module

package - Custom

version - 1.0

core - 8.x

Use of Configuration before code

Having to rewrite the code every time you change it is not something which you would want to do. Instead of hard coding a class into a theme, set the values in the configuration and apply it with the code. This ensures easy and faster ways to code and also results in high quality modules. With reusable codes being the norm in writing quality software, configurations in the code ensure advanced functionality and and "easy to modify" features.

Use What Drupal Offers

Once you have built your module, you should know that Drupal comes with various built-in admin functionalities to store and display module data and settings. The module settings page can be defined with hook menu . This hook enables modules to register paths in order to define how URL requests are handled. With the drupal_get_form page callback usage, all you have to do in the callback function is define and return the settings that are to be stored.

Too Many Cooks Spoil The Broth

Having worked on large scale Drupal sites for a long time, Drupal developers would have learn that hundreds of modules can work their magic together to produce an effective large-scale project. But when working on enterprise Drupal websites, it is important to note that avoiding a single poor module has greater effects than avoiding 30 well built Drupal modules (in an attempt to use lesser modules). This too many modules advice leads programmers to favor programming their own modules over reusing modules. The higher the number of custom modules you write for a project, the more work it takes to maintain and modify your Drupal site later. Instead, consider publishing your modules on Github. This allows you to avoid usage of a large collection of custom modules and instead encourage you to create reusable code that has the required configuration.

Environment & Coding Standards

Development environment is an important influence as it ensures that a Drupal project runs without any hassle. Drupal development companies ensure that the entire team works in an exactly same development environment for an efficient work-flow.

While working in such an environment, one of the biggest issues to handle is to make sure that the code is clean and maintainable. Drupal developers need to make sure that their code is readable and makes sense to everyone they are working with. And this doesn't apply only to the team members or to anyone from your organization, but also to the larger Drupal community itself. With community involvement being an essential part of Drupal development and a key aspect in distributed teams working together efficiently, following the coding standards helps in achieving a project's goals and objectives.

Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web