Drupal has custom module development that gives unlimited possibilities to build your website. The beauty of Drupal is that, unlike its other CMS counterparts, it doesn’t rely on third-party plugins for add-on services. Multi-lingual capabilities, security, and several other add-ons are already built into the system. That spares a lot of trouble in terms of add-on management and maintaining security.
So here’s more on how Drupal’s core element, custom module development, has made it a CMS leader.
What is a Drupal module?
A Drupal module is basically a file containing PHP, javascript or CSS files written to create a functionality or features for websites. The features or functionalities on the web pages can be made to work by installing or uninstalling the module. Drupal gives its own contributed modules for common web functionalities. On the other hand, it is also possible to develop custom Drupal modules.
Custom Drupal 8 module development has become popular as it allows users to build functionalities bespoke to their website needs.
Can anyone get started with Drupal 8 module development?
Well, a little bit of prior working experience with Drupal will definitely make a difference. If your Drupal module development needs are minimal, you should be able to finish it as a typical DIY project. However, for developing complicated Drupal modules, you will need professional Drupal development services.
How to create a custom module in Drupal 8 – A quick tutorial
Drupal 8 module development is a vast and growing space. It is not possible to compile all know-how in place. We have created this Drupal 8 module development tutorial assuming you are in the beginning stage and want to build a basic module.
Let’s get started.
The File Structure to create Custom Modules in Drupal 8.
One of the key things to be kept in mind when creating a custom module in Drupal 8 is the file structure, which is evidently different from that in Drupal 7.
Related: How to migrate to Drupal 8- A four step guide
When it comes to Drupal 8, the modules folder hosted in the root directory is used to keep the contributed or custom modules.
Since we have selected the name of the module to be “demo_module”, the module created will also have the same name. As the case is, begin by creating a folder under the Drupal installation on the path: “sites/all/modules/custom/demo_module.”
Step# 1: Creating the ‘.install file’
The schema definition in Drupal is an array used to represent one or multiple tables and indexes and keys that relate to the table/tables. A schema is described by ‘hook_schema()’ that should exist in the ‘.install file’ of the module.
Step# 2: Creating the ‘.info.yml file’
In order to let Drupal know about the existence of your module, an ‘.info.yml file’ needs to be created. The process is similar as creating an ‘.info file’ in Drupal 7.
- name: The name key is to provide a name for the module.
- type: The type key is to define the type of extension.
- description: The description key allows to provide extra information about the module which will be displayed on the module’s list page.
- package: The package key specifies the package in which the module should be included.
- version: The version key version of the module.
- Core: The core key is to specify the major release version of Drupal
The file will be ‘demo_module.info.yml’.
The syntax will be as follows:
name: Demo Module
type: module
description: This is a demo module written in Drupal 8.
package: Custom
version: 1.0
core: 8.x
You are required, now, to enable the module by either clicking on ‘Extend’ from menu or following the path: ‘http://YOUR_HOST/admin/modules’.
Step# 3: Creating the ‘.routing.yml file’
So as to handle routing, we utilize Symfony2 Components to a large extend in Drupal 8. To help us with our navigation to the Drupal module, we need to create a routing file by detailing a dissimilar controller action.
- list: The route in Drupal 8 must be defined as module_name.route_name ‘module_name => demo_module’ ‘route_name => list’.
- path: The path of the module can be specified where the users shall be re-directed on going to the module. This ideally is the URL to the route that must contain leading forward slash “/”.
- defaults: We are free to specify and introduce any number of things. As for us, we have chosen to introduce _form and _controller.
- _controller: A method on DemoController class is referenced in the _controller.
- _form: It talks about the classes that are required to be used so as to define various forms in the module such as AddForm, DeleteForm and EditForm.
- Requirements: Here, we identify the permission that in turn checks the user-needs to grant the access to sight the page. We have, here, used ‘Add, Delete, Edit and Access’ content permission that are common for all other types of content on the site.
The Syntax will be as follows:
demo_module.list:
path: ‘/demo’
defaults:
_controller: ‘Drupaldemo_moduleControllerDemoController::demo’
_title: ‘Demo’
requirements:
_permission: ‘access content’
The file will be ‘demo_module.routing.yml’.
Step# 4: Creating the ‘.module file’
In Drupal 7, the .module is required even if it doesn’t contain any code. In Drupal 8, it is optional.
Step# 5: Creating ‘Controller Class’
In this step, we need to create the ‘DemoController.php’ as per the naming standards as postulated by the PSR-4. To do so, create a;
- Folder ‘modules/custom/demo_module/src/Controller’.
And, in this folder, create a;
- File with the name ‘DemoController.php’.
The Syntax will be as follows:
<?php
namespace Drupaldemo_moduleController;
class DemoController {
public function demo() {
return array(
‘#title’ => ‘Demo Module’,
‘#markup’ => ‘This module is for demo purposes’,
);
}
}
Step# 6: Creating the ‘Model Class and Forms’
In this step, we will introduce and make of the ‘utility/model class’ in order to acquire needed data from relevant database and can use the necessary data in the ‘DemoController’, we have created in the previous step for this particular module.
Step# 7: Creating ‘Menu on Admin’
This set of execution steps is the one used in displaying the module we have created as a menu. As we please or choose, the menu link can be placed parallel to ‘Top Menu’ or as ‘Admin -> Content’ like a Tab as any ‘Descended Menus’.
So as to define the ‘Menu’ at the top, a file ‘demo_module.links.menu.yml’ needs to be created and routing needs to be set from there so as to load the Module listing page.
The Syntax is as follows:
demo_module.list:
title: Demo module settings’
description: ‘example of how to make an admin settings page link’
parent: system.admin_config_development
route_name: demo_module.list
weight: 100
If you are able to carefully follow these aforementioned steps, you can easily launch your own module from the Admin Panel of Drupal 8, we are certain.
You can sign in to the Admin Panel of the latest Drupal 8 platform and the path can be followed. In case the module is being installed for the first time, you are required to clean the ‘Cache’ and follow ‘Extend > List > CUSTOM (Accordion) so as to get the module enabled. If you have not done it before installing, then you need to uninstall the module, clean up the ‘Cache’ and reinstall the module from the above path.
Before developing a Drupal 8 custom module
Drupal 8 was released in 2015. Ever since then it has been growing with 51% year-on-year (Media Current). In fact, Builtwith.com shows that a large number of Alexa’s top websites are built using Drupal (Source).
Needless to say, Drupal has an active development community. Custom Drupal modules are created in large numbers to meet the needs of websites. However, should you develop a Drupal 8 custom module every single time?
Not necessarily. There are existing modules and themes that you can make use of. Custom development becomes necessary only when existing modules and themes are unavailable. Again, before venturing into a DIY custom module development, you must know its complexity and the relevant expertise required. It is better to engage Drupal 8 custom module professionals for complex projects.