A Step-By-Step-Guide to Create Custom Modules in Drupal 8

With the most recent release of Drupal—Drupal 8, it has become pretty clear that the folks at Drupal are on a mission; one which aims to make Drupal all the more awesome and simpler. If the release of Drupal 8 is any indication, the coming days are going to be exciting for everyone using the platform.

One of the many things that changed with the release is how differently Drupal 8 modules work compared to Drupal 7. It has become significantly effortless, now.

Here are the 7 steps that will help you create custom Drupal 8 modules without having to break a sweat. However, since Drupal 8 is still in the development phase, there may be changes brought about in the future as the code attains maturity.

The File Structure

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:


  path: ‘/demo’


      _controller: ‘\Drupal\demo_module\Controller\DemoController::demo’

      _title: ‘Demo’


      _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.

drupal 8

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:


namespace Drupal\demo_module\Controller;

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:


  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.

Hope you have found the above tips useful. Please contact us to know more about our Drupal expertise and services.

Related Posts:

Author : Rahul Suresh Date : 02 May 2017