Shefali Shetty
17 Mar, 2020
Drupal 8 custom modules can range from being super simple to highly complex, depending on the need for the customization. The true power of Drupal can be unlocked with custom modules in Drupal 8. Irrespective of the complexity of a custom module, following some best practices can help you create more efficient custom modules. So, let’s get started!
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 solutions, the first thing you might hear is, “there is a module for that!”. While there are close to 70 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, 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. Creating a Drupal 8 Custom Module to further extend Drupal’s capabilities is not the same as it was in Drupal 7.
What is a custom module in Drupal 8? Why create one?
A Drupal 8 custom module is basically a module that is made up of customized functionalities. The Drupal custom module could start with a contributed module that needs small customizations or a completely new Drupal 8 module with new sets of non-existent functionalities custom fit to the project. Or sometimes, a migration from Drupal 7 to Drupal 8 will call for a custom module because the Drupal 7 module isn’t available in Drupal 8. A Drupal 8 custom module is essentially built to extend the core functionality of Drupal, which by itself comes with built-in set of core modules.
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 PHP 7 to run and it uses Symphony, a PHP framework that relies on Object Oriented Programming. Nonetheless, Drupal 8’s new approach is a futuristic approach and has opened doors to a wider set of developers who are well-acquainted with OOP and MVC concepts.
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 more about this.
Change in the Drupal 8 File Structure
One important point to remember is that in Drupal 8 modules development the file structure is quite different from the one in Drupal 7. Custom modules in Drupal 8 are stored in the /modules directory, unlike in Drupal 7 where this directory was reserved for the core modules. The core Drupal 8 modules now go into the /core directory.
Next up, to let Drupal 8 know that your custom module exists, you need to create an .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 looks 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 make any changes is quite a task. 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 results in high quality modules. With reusable codes being the norm in writing quality software, configurations in the code ensure advanced functionality 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 must 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 learnt 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-developed Drupal modules (in an attempt to use lesser modules). Developers then tend to favor programming their own modules over reusing existing 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 encourages 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 exact same development environment for an efficient workflow.
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.