Drupal 8 came out with many new features and updates at the end of 2015. As Drupal 8 is object oriented and enforces PSR-4 standards, the way you make modules has significantly changed. However, this change makes modules much more organized to fit today’s coding practices. I will be demonstrating how to create a simple “Hello World!” module in Drupal 8.
Getting Started
I will be assuming you already have a working Drupal 8 website.
Creating The Module Directory
Drupal 8 has a much cleaner folder structure than before. All the modules will go in the
module
folder. Create a hello_world
folder here. This will serve as the machine name for the module and will contain all the necessary files for our module to function.
Defining Parameters
Module Information
First, parameters such as title
and description
need to be set. These keys will be defined in the file hello_world.info.yml
. Unlike Drupal 7, Drupal 8 uses YAML (YAML Ain’t Markup Language) for these types of files. Create this file and enter these definitions:
1 2 3 4 5 |
name: Hello World type: module description: Say Hello World package: Custom core: 8.x |
-
name: Hello World
- This is the title of our module shown on the extend page.
-
type: module
- This is to tell Drupal what we are making is a module.
-
description: Say Hello World
- This is a description shown alongside the title on the extend page.
-
package: Custom
- This is what category our module will be listed under on the extend page.
-
core: 8.x
- This tells Drupal our module is compatible with Drupal 8.x core.
You will now be able to see the module listed under the Custom package category:
Let’s enable this module:
Module Routes
Next, we will need to tell Drupal where the module can be accessed. This is done by creating a route for our module. Create the file hello_world.routing.yml
and enter the following parameters:
1 2 3 4 5 6 |
hello_world: path: /hello/world defaults: _controller: Drupal\hello_world\Controller\HelloWorldController::hello requirements: _permission: 'access content' |
-
path: /hello/world
- This tells Drupal what path will be used to access our module.
-
_controller: Drupal\hello_world\Controller\HelloWorldController::hello
- This is the method Drupal will call to process a request to our path.
-
_permission: 'access content'
- This is to ensure only users who can access content will be able to see our Hello World page.
Quotes are not required around the values, however, you can include them to be safe.
Your current directory structure should now look like this:
Coding the Module
After defining all the parameters for the module, we still need to add functionality to our module. We need to write code that will live in a “Controller”. Since Drupal 8 follows PSR-4 standards, there is a specific directory structure to follow. All code will exist inside the src
folder. After creating this, make a Controller
folder. This is where the PHP Class will be made. Here, create HelloWorldController.php
and define the as so:
1 2 3 4 5 |
<?php class HelloWorldController { } |
Any Drupal 8 class will also need to be namespaced. This is just to make sure the right classes are referenced from outside if some classes have the same name and it makes it more organized. Add this above the class definition:
1 2 3 4 5 6 7 |
<?php namespace Drupal\hello_world\Controller; class HelloWorldController { } |
Remember in the routing file we told Drupal to call the hello
method in the class Drupal\hello_world\Controller\HelloWorldController
. Let’s define this in our class:
1 2 3 4 5 6 7 8 9 |
<?php namespace Drupal\hello_world\Controller; class HelloWorldController { public function hello() { } } |
Since Drupal 8 is based on the Symfony Framework, a new Symfony\Component\HttpFoundation\Response
object can be returned. However, since this is Drupal, you can also return a render array which Drupal will process and automatically add theming. For our simple page, we can return an array with keys #title
and #markup
:
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php namespace Drupal\hello_world\Controller; class HelloWorldController { public function hello() { return array( '#title' => 'Hello World!', '#markup' => 'Here is some content.', ); } } |
Finishing Up
Before we visit our custom page, we need to clear the cache, specifically the router cache. Otherwise, you will see a 404 Not Found page. You can clear the cache via Drupal Console:
Or through the Admin Control Panel:
Now, if we visit http://www.mydrupalwebsite.com/hello/world
, we can see our custom page:
The Full Module
Here is the final directory structure and code of our Hello World module:
That’s it! Creating Drupal modules is as easy as that! Now that you
know how to define module parameters and set up your own Controller to handle specific route requests, you can move on to create more complex Drupal modules using other components such as Forms and implementing Services.
This is vastly superior to the official documentation, which just tells you to download a git repository.
This is brilliantly written by a brilliant person.
Bindas article
Great work man. working fine.
Was lookin’ to quietly mention the error on line 5, ’til you encountered & corrected the result near the end — that turned your good tutorial into an excellent lesson for all.
Beyond encouraging you to continue your efforts, I just wanna add that you’d be an asset to any development team, anywhere.
~ [email protected]/[email protected]/cowcreekgeek ~;-)
Thank you! 😀
thank you
Thanks..i never know how to create a page..evrywhere was this:
return new Response(“Hello World!”);
But i wanted normal page with drupal style.
Thank you very much again.
Thanks for the post.
Nice clear post. Thanks!