Let’s create a simple custom module in Drupal 8


In this post i am going to show you how you can write a simple Drupal 8 module which will print “Hello world from custom module”.

There are numerous differences between Drupal 7 and Drupal 8.  Especially folder structure, If you have already worked in Drupal 7 you might have created a custom module under /sites/all/modules/your_module but in Drupal 8 it should be inside the root module folder because they have moved all the core modules to the core folder.

You can see the new structure here:


So the folder highlighted in the above screenshot is where you are going to write your new custom module.

So all of our contributed modules and custom module will be in the path: /modules/

To segregate the contributed and custom modules, lets create a folder called custom inside the path: /modules/

After you create the folder custom, lets create another folder called hello_world inside the path: /modules/custom/

Module Info file: 

In Drupal 7 the info file should have the module_name with a dot info extension (eg: hello_world.info)

In Drupal 8 also the same but it should have one more extension as well .yml (eg: hello_world.info.yml) because Drupal 8 uses YAML.

Let’s create the info file hello_module.info.yml:

name: Hello World
description: Just a Simple Hello world Drupal 8 Module
core: 8.x
version: 1.0
type: module
package: Custom

Put the above lines of code into the file: hello_module.info.yml.

You can now see your module is getting displayed in the Drupal admin panel:


So just by creating a .info.yml file is enough for Drupal 8 to recognise your new module. You can now just click the check box and activate your custom Hello World module.

Next step in Drupal 8 is quite different from Drupal 7. Because in Drupal 7 we have to create a module file (hello_world.module) to write hook_menu and other functions.

But in Drupal 8 everything is Controller. You can still write hello_world.module to call hooks in Drupal 8 but for our module it is not required.

So your question should be now, how to create a hook_menu if I don’t have any module file right? (You will see about this in the Route file section)

Controller file:

Before moving to that, We need to create two folders inside the hello_world folder, it should be hello_world/src/Controller.

Also create a file called ‘HelloController.php‘ inside the Controller folder (so the path will be now hello_world/src/Controller/HelloController.php)

Put the below code in HelloController.php:


namespace Drupal\hello_world\Controller;

use Drupal\Core\Controller\ControllerBase;

class HelloController extends ControllerBase{
	public function display_content(){
		return array(
			'#type' => 'markup',
			'#markup' => t('Hello World from custom module!')

Route file:

Now is the time to create another YAML file called hello_world.routing.yml. This file helps to create URLs and to execute the above Controller (almost similar to hook_menu in Drupal 7)

Create the hello_module.routing.yml and put the below lines of code in it.

  path: '/hello_world'
    _controller: 'Drupal\hello_world\Controller\HelloController::display_content'
    _title: 'Hello World!'
    _permission: 'access content'

View the output:

Everything looks good. If you have already enabled the ‘Hello World’ module, just clear the cache once and type /hello_world (eg: https://yourdomain.com/hello_world) to see the output.




You can download this module from the below link if you wish to:


One thought on “Let’s create a simple custom module in Drupal 8

Leave a Reply

Theme: Overlay by Kaira