Creating a “Custom Module” in Drupal 8

Creating a “Custom Module” in Drupal 8

Share this
Creating a “Custom Module” in Drupal 8
Nov 19,2018

As the majority of you may know that Drupal 8 is built on symfony web application framework as it is an MVC framework. In this article we will be talking about creating a “Custom Module” in Drupal 8.

MVC is an architectural pattern used for development and it allows components to be separated into different layers such as model, view and controller.

 

What is a Module?

A module consists of PHP, JavaScript or CSS that accomplishes a certain job to result in web function. Creating a custom code isn’t as hard as it seems, below you will find a simple break down of the steps to create a fully functioning custom module.

 

Creating Module Folder

The first step would be is to create a machine name for the module to use in all systems. For example let us create a machine name “welcome” for the module.

In Drupal 7, the directory module path used to be [sites/all/module] On the other hand, in Drupal 8 the path for custom modules and contributes are located in [/module].

It’s worth mentioning that you can still use the same directory used in Drupal 7 for Drupal 8. Now let’s put a folder module “welcome” in /module in our example.

Also, I will name the new folder to be “welcome” that will result in this path module/welcome.

 

Creating INFO FILE

The next step would be is to create an info file (INFO.YML) using the symfony YMAL component, info file should have the same name as the module folder name.

We will create welcome.info.yml in the welcome directory, and we will put all meta data as explained below:

Name: Welcome

Type: module

Description: 'First custom Module in Drupal 8'

Package: Custom

Core: 8.x

 

Creating a MODULE FILE

In Drupal 7, the module file must include coding. However, in Drupal 8, it became optional to include code and is now just used to help implementing the hook.

We will create welcome.module to be used later.

 

Creating the SRC directory

We need to create a sub-directory inside the module for controllers, forms and plugins. All subdirectories should be called source. This allows controllers to autoload class.
We now can create SRC folder inside the welcome module folder.

 

Creating a Controller

We will create a folder controller in SRC we just created. The controller works typically like an MVC application.

To create basic controller, follow the following steps:

1- Create a folder in SRC, called controller.

2- In controller, create file WelcomeController.php

In the WelcomeController.php, we need to include “welcome to our website” message.

 

<?php

namespace Drupal\Welocme\Controller;

Use Drupal\Core\Controller\ControllerBase;

class WelcomeController extends ControllerBase {  public function content() {

    return array(

      '#type' => 'markup',

      '#markup' => t('welcome to our website'),

    );

  }

 

Creating Route File

The controller we created above will not work without adding route file from the URL to controller to be executed.

Create a file called welcome.routing.yml and add this code:

welcome.content:

  path: '/welcome'

  defaults:

    _controller:

'Drupal\welcome\Controller\welcome::content'

    _title: 'welcome'

  requirements:

    _permission: 'access content'

After adding the code, enable the module, and go to “/welcome” and you will see the message welcome in our website.

 

Conclusion

We just created a simple custom module and with these simple steps you can create more complex ones, In Drupal 8 it can look more complicated but eventually it’s clearer than Drupal 7 and will be easier to understand by other developers. We at Complete Chain has implemented many complex successful projects using Drupal 8 that are active on the web and being maintained. Need help on your next project? Get a quote here and subscribe to our newsletter for fresh content about the world of web development.

If you’re a Drupal Developer and want to join our team of experts you can send your CV to this email [email protected]