X
    Categories: Drupal 8 Tutorials, Tips and GuidesDrupal Tutorials, Tips and Guides

How to Create a Module in Drupal 8

The biggest trait of CMSs like Drupal is their ability to create websites without the need for users to delve into coding. However, the real power is unleashed when you get to make your own custom modules and customize them according to your needs.

Keeping that in mind previously I presented tutorial for creating Drupal 8 module via Drupal console, but today I’ll show you how to make your own custom module for Drupal’s latest version i.e Drupal 8. So without further ado, let’s get started. Here’s a list of all the essentials that you will need to follow:

 

  1. Name Your Module
  2. Place Module in Drupal
  3. Create Module’s yaml File
  4. Create Routing File
  5. Create Directory for Controller
  6. Create Controller File
  7. Check If the Module Functions

Name Your Module

The very first thing that you need to do is to decide a name for your module. In Drupal, we give our module a machine name. Drupal will identify your module by this name. Note that this name is different from the name that is seen by the user. Here are some of the things that you should keep in mind before giving a machine name to your module:

  • It shouldn’t start with uppercase letters.
  • There shouldn’t be spaces between the words.

Since we all started programming using the famous Hello World, I’ll be naming our first custom module hello world as well. The machine name that I’ll give it to is hello_world.

Place Module in Drupal

We will need to create a new directory in Drupal where we’ll place our module. Although you can place this new module directly in the Drupal root’s module directory, it is always better to place it in sites/all/modules folder in order to be able to differentiate between Drupal’s core modules and our custom modules. So in order to do this, create a new directory under the sites directory as shown.

Create a folder in this directory with the machine name of your module. (In this case hello_world)

Create Module’s yaml File

In Drupal 8 all .info files have been replaced to yaml files with the extension .yml. For Drupal to be able recognize the module, we have to create a yaml file with the machine name of our module. So I’ll create a yaml file like this hello_world.info.yml. Inside this file, we will include details of the module so that Drupal can identify it:

 

name: Hello World (The name to be displayed on the modules list in Drupal)

description: First Custom Drupal 8 Module (Description of the module to be displayed)

package: Custom - (Declaring that this is a custom module)

type: module - (Declaring that this is a module)

version: 1.0 - (Version of the module)

core: 8.x - (The version of Drupal)


Create Routing file

Now we will create a routing file for the module. This file will be used by the controller to know at which URL path the module’s function will execute. The name of our file will be hello_world.routing.yml. Inside this file, the following code will be placed:

hello_world:

 path: /hello/world – (The url path)

 defaults:

   _controller: Drupal\hello_world\Controller\HelloWorldController::hello – (the namespace and the method that the controller will call)

 requirements:

   _permission: ‘access content’ – (The permissions required for the module to successfully perform its function)

Create Directory for Controller

Next, we will need to create a new subdirectory under our module’s folder which will hold the controller file. All external plugins, controllers etc are placed under this directory. We name it src short for source. Create a folder named src under your module’s root directory, then create a new folder under src with the name Controller.

Create Controller File

We will now create a controller file which essentially controls the functionality of your module. Make sure that the name is relatable so that it’s easily identified. In my case, I’ll give it the name HelloWorldController.php. In this file enter the following code:

<?php

 

namespace Drupal\hello_world\Controller;

 

class HelloWorldController {

public function hello() {

return array(

‘#title’ => ‘Hello World!’,

‘#markup’ => ‘Content for Hello World.’

);

}

}

Check If the Module Functions

Login to your Drupal site and enable your module, if you haven’t done it already. To check if it functions properly, visit the path you specified in your routing file. If you get the ‘Page Not Found’ error, then clear the cache by navigating to admin->configuration->performance. Check again and it should function properly this time.

Conclusion

That’s it! You’ve successfully created your very first custom Drupal 8 module. Although this is a very simple Drupal module, it essentially covers all the basics of creating a Drupal 8 module. If you encounter any issues, get in touch with me in the comments section below.

Hamza Zia :Hamza is a Drupal Community Manager at Cloudways - A Managed Drupal Hosting Platform. He loves to write about Drupal and related topics. During his free time, he can be seen obsessing over Football, Cars, Android and Gaming.