how to create custom module in drupal 8


This article will cover, how to create custom module in drupal 8. the module creation in drupal 8 is from  Drupal 7 the following steps to create drupal 8 custom module , 4 steps to create custom Module.

After Install Drupal 8, Drupal 8 folder Structure like:

Related article:

Drupal 8 folder Structure:

 how to create custom module in drupal 8
Drupal 8 file structure

Name your module:

First step to create Module name or Machine Name (common) see example:

how to create custom module in drupal 8
Name your Module


  • It must start with a letter.
  • It must contain only lower-case letters and underscores.
  • It must not contain any spaces.
  • It must be unique.
  • It should not be any of the reserved terms like: src, lib, vendor, assets, css, files, images, js, misc, templates, includes, fixtures, Drupal.

More>> Naming and placing your Drupal 8 module


Create a folder for your module:

we need to create folder for your module like :/modules/custom/common

#step 1: Create the .info.yml file

create the <module name>.info.yml file in module folder see folder structure which is given below

name: Common Module
description : This is my first drupal 8 module.
package: Custom

type: module
core: 8.x

#step 2: Create the .routing.yml file

Create <module name>.routing.yml file. this file use define path, title and permission etc.


path: '/first-url'
_controller: '\Drupal\common\Controller\CommonController::new_test'
_title: 'First Page Drupal 8'
_permission: 'access content'

#step 3: Create the .module file

Create <module name>. module file within module name folder which is given in folder structure. this is Optional in drupal 8.

Module use for hooks.

Create the src directory:

we need to create subdirectory folder src . src subdirectory use for the controllers, plugins, forms, templates  etc/modules/custom/common/src

#step 4: Create a basic controller

Page implementation for the Common module.

The Controller class CommonController should be defined in common/src/Controller/CommonController.php:


* @file
* Contains \Drupal\common\Controller\CommonController.php

namespace Drupal\common\Controller;

use Drupal\Core\Controller\ControllerBase;

class CommonController extends ControllerBase {

public function new_test() {
return array(
'#type' ='markup',
'#markup' =t('This is first Drupal8 custom Module'),

File Structure after creating Custom Module:

See file structure after creating files and subdirectory.

how to create custom module in drupal 8

Display the content

After complete process and enabled Module run your define path. see view content like.

 how to create custom module in drupal 8
how to create custom module in drupal 8




  1. Nice tutorial, but I saw a better step by step tutorial for creating modules on Drupal on Cloudways, do have a look at it. thanks


Please enter your comment!
Please enter your name here