creating a Drupal 8 Module
Blog

My First Tryst with creating a Drupal 8 Module 'Demo'

I spent the whole day trying to do something that I've been wanting to for quite some time now. I went through the changelog for Drupal 8 to see if I could build an initial level of custom modules in D8 and I would love to share what I have learned so far. Let us take a look at this simple D8 module (that really does nothing other than printing a 'Hello World' ) called 'demo'.Here I have listed out the steps involved while creating the Module with some inline explanation.

Step 1:

Create a Custom folder inside  /modules/custom/. Under Custom we have to create .info.yml (holds basic information about module). & .routing.yml (holds menu link and permission access). And then you need to create Controller.

Drupal 8 uses the Symfony HTTP Kernel, a system which gets the request and asks other systems to produce the requested output (a response object) and sends the response back to the client. The output is generated by a piece of code called the controller. In theory the controller can be either a pure PHP 4 function, a method on an object or even an anonymous function.

The second part of creating a page that outputs our content is to put the page controller in place. This could be a PHP 4 style function, but best practice in Drupal 8 is to use a controller class. This class is in a file named exactly after the controller provided in the routing file, ExampleController.php

For the above router the class needs to be placed in example/src/Controller directory and with name ExampleController.php. So the full path will look like, example/src/Controller/ExampleController.php.

Syntax: use src/ & Controller/ folder inside module directory(As per PSR-4 standard).

The .info file has been converted to YAML. This is the Symfony YAML component. So, the new file extension is .info.yml

For assignment = changed to :

For dependencies, use following format:

dependencies:
- block

For comment # changed to ;   

type key is required with values,indicate the extension.(e.g: module/theme).

name: demo
description: 'Demo module for Drupal 8'
type: module
core: 8.x

routing.yml is used to define routes and for handling the callback function.

Line 1:

Line 2: Path you want to register

Line 3: having Default page _title and _content refers to DemoController with method name demo

Line 4 : provide Permission.


 

Step 2:

The second part of creating a page that outputs our content is to put the page controller in place. This could be a PHP 4 style function, but best practice in Drupal 8 is to use a controller class. This class is in a file named exactly after the controller provided in the routing file, ExampleController.php

Create controller DemoController with a method demo( )

Create src/ & Controller/ folder inside module directory.(src/ is a PSR-4 standard).

Controller/ is a place to store controller. Create DemoController.php


On top, I have specified the class namespace.

Inside Controller class , we have demo( ) method. Which is rendering array.

To view the output all we need to do is clear the cache and navigate to mysite-name/demo

Download module using Github:  git@github-name:xaiwant/drupal8.git

We at Valuebound are committed to furthering your business initiatives with our Enterprise level web solutions. For further information on our service offerings, please Contact us.