How to create Drupal 8 custom module

If you want to extend drupal functionality then you have to write a custom module, otherwise you have to check any contributed modules are available for that functionality. So in this article I will give you guidance of how to create a custom module and what are all the files need to be create for that. Before that you might have to install Drupal in your machine, if you want to install a new drupal instance follow this article.
1. First you have to create a folder for your module , from the Drupal root directory (/var/www/html/d8), within modules folder create a folder called custom(/var/www/html/d8/modules/custom) and contrib(/var/www/html/d8/modules/contrib). So all the modules which will be downloaded from www.drupal.org will be located in contrib and all our custom module will be available in custom folder for the best practice.
createFolder
2. Within custom folder create your module folder, here I have created “utilities” is my module folder name. Then with in that folder create a *.info.yml (utilities.info.yml) this file will contain all the meta-information about our custom module, this module packaged in a custom group.
name: Utilities Module
description: All custom utilities comes under this module
package: Custom
type: module
version: 1.0
core: 8.x
3. You have to create a *.module file (utilities. module) is an optional file in Drupal 8 but in Drupal 7 it’s mandatory.
4. Then you have to create a controller, with that controller only you have to control all your custom functionality. So in your custom module folder create an “src” as a folder (/var/www/html/d8/modules/custom/utilities/src/) within that create a “Controller” folder then create a Controller file, here I have created “UtilitiesController.php”. In this controller file you can have a function for example we have used content function returns “Hello World” so when calling this controller
“Hello World” will be returned in the page. To call this function you can have a route for this controller.
<?php

/**

 * @file

 * Contains \Drupal\utilities\Controller\UtilitiesController.

 */

  

namespace Drupal\utilities\Controller;

  

use Drupal\Core\Controller\ControllerBase;

  

class UtilitiesController extends ControllerBase {

  public function content() {

    return array(

      '#type' => 'markup',

      '#markup' => t('Hello world'),

    );

  }

}

5. So after creating the controller, go to your modules folder create a routing file (/var/www/html/d8/modules/custom/utilities/utilities.routing.yml), there you have to add your route entry, as follows

utilities.content:

  path: '/demo'

  defaults:

    _controller: 'Drupal\utilities\Controller\UtilitiesController::content'

    _title: 'Hello world'

  requirements:

    _permission: 'access content'

The folder structure will be like below,

Folder Structure

6. Now we are ready to call a route, navigate your browser with your route “/demo” you can see a “Hello World” return statement.

Controller Route

Leave a Comment