Drupal 8: Create a Simple Module

Drupal Simple Module Bright

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 Module Directory Structure 1Drupal 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:

  • 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:

Drupal Module List

Let’s enable this module:

Drupal Module Enabled

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:

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

Drupal Module Directory Structure 2

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:

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:

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:

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:

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:

Drupal Router Rebuild

Or through the Admin Control Panel:

Drupal Clear All Caches

Now, if we visit http://www.mydrupalwebsite.com/hello/world, we can see our custom page:

Drupal Hello World

The Full Module

Drupal Module Directory Structure FinalHere 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.

Author: Akshay Kalose

A teenager, who is interested in Computer Science, Information Technology, Programming, Web Designing, Engineering and Physical Sciences.

2 thoughts on “Drupal 8: Create a Simple Module”

Leave a Reply

Your email address will not be published. Required fields are marked *