Skip to main content

My First Drupal 8 Module.

Lots of reading and reading ... How about trying some coding ?? Huhh !!! Coding in drupal 8.

Frightened ?? Me too :)

But what I exactly feel here is that this is just the fear of the unknown. And everything we want is

on the other side of fear. I replace my fear of the unknown with curiosity. Give it a shot, who

knows if this works out for you too :)

Let's start together with a simple stuff like "Printing a Drupal 8 welcome message" by creating a

small module.

Taking a deeep breath, let's dive in.

As we must have seen or read that Drupal 8 follows a very strict directory structure, so let's start

with looking for the directory structure followed by custom modules in Drupal 8.

1. Directory structure: It is advised to keep our custom modules by creating a folder 'custom'

under 'modules' folder in the root directory of our site.

So, our custom module 'first_d8_module' will have directory structure something like this.
<root directory>/modules/custom/first_d8_module/
2. The next step in module development, as we know from Drupal 7 is creating a .info file. Quite

similarly, here we will be creating a .info.yml file.

Here goes the content of our custom module's .info.yml file.
first_d8_module.info.yml :
name: First D8 Module.
description: A module to build our first Drupal 8 module
package: Test
type: module
core: 8.x

With this I hope we cleared our first stage :) Let's check the list of modules under /admin/modules

And here's our first module. Like Drupal 7, we no more need .module file to be able to enable a

module. With just .info.yml file, we let Drupal know that we have a new guest knocking at the

door.

3. As per our understanding of Drupal 7, the next step is to register a path from hook_menu and

define a callback for it but will not be doing that this time. Thanks to Symfony2 components for

handling the routing process for us in Drupal 8. Let's see how to go ahead ...
Create a '.routing.yml' file in the same directory as '.info.yml' file.

The content of my module's 'first_d8_module.routing.yml' file goes here :

first_d8_module.welcome_page:
path: '/d8_module/welcome_page'
defaults:
_controller: '\Drupal\first_d8_module\Controller \First_d8_moduleController::welcomePage'
_title: 'My first page in Drupal8'
requirements:
_permission: 'access content'

Unfamiliar names ?? Come, let me introduce.

The first word in the first line is the name of our module, followed by the name of the new route we

are defining right now.

<module_name>.<route_name>

Under "path", we define the path we want our route to register.

"defaults", _controller contains the reference to a function in our custom module's controller class

which we will soon be creating.

and _title contains the default page title.

With "requirements", we specify the permissions for accessing the page.

I hope all of us will soon develop good bonding with these unfamiliar names as we start meeting

them quite frequently.

4. Defining custom module's Controller:

The directory structure for the Controller file will look something like this :
<root directory>/modules/custom/first_d8_module/src/Controller/First_d8_moduleController.php

The content of controller file goes here:
<?php
  /**
  * @file
  * Contains \Drupal\first_d8_module\Controller\First_d8_moduleController.
  */
  namespace Drupal\first_d8_module\Controller;
  /**
  * Providing responses for the first_d8_module.
  */
  class First_d8_moduleController {
    /**
    * Returning a custom page with welcome msg.
    *
    * @return array
    * A renderable array.
    */
    public function welcomePage() {
      $element = array(
      '#markup' => 'I welcome Drupal 8 by creating my first Drupal 8 module.',
      );
      return $element;
    }
  }
?>

These 4 simple steps and we are done. YESS, we just now created a module in Drupal 8 !!
Enable the module if not already and visit the path registered, here the path is

"/d8_module/welcome_page". We can see our custom welcome message printed on the page.

5. Creating a menu link for the path registered:

Let's create a file "first_d8_module.links.menu.yml" under
<root directory>/modules/custom/first_d8_module/

The content of the file is :
first_d8_module.welcome:
title: Welcome D8
description: 'Welcoming D8 by creating a custom module.'
parent: system.admin
route_name: welcome_page

Clear cache and we now have a menu link Welcome D8 linked with the welcome page we just

created. With this we are done with our module development.

Will be soon back with something new and exciting in Drupal 8, till now keep learning, keep

sharing. Good luck :)

Comments

  1. Hello, Sonam, This is a good and informative post for beginner of drupal developer. You explain well in steps and this will really help for those who looking for starting of drupal coding.

    ReplyDelete

Post a Comment

Popular posts from this blog

Know Your Customers !!!

A user landed on a web page and started filling a form. After going half way, he/she got distracted or suddenly felt not interested in the same and tried to leave the page by either closing the tab or by closing the browser itself. In either case, we (a company) lost a prospective client and as the user didn't submit the form we have no info of the user in order to contact them and get the details of them not being interested. Is this your story too ?? Congratulations my friend ... you found the solution for yourself. Please continue reading... To start with let's modularize our requirement. There's a web page containing a form, a user lands on it and started filling the form: 1 . As soon as the user tries to leave the page, ask them if they are ok with sharing their unsubmitted data with you and leave the page. 2 . If a user chooses to stay on the page, simply allow him to be there. 3 . If a user opts for leaving the page without submitting the for...

Request to Response in Drupal 8

Here ? So, I am assuming we have some idea of Symfony2 and it's components used in Drupal 8 by now. Great, let's move ahead. Every web interaction as we know starts with a request and ends with a response, same goes here with Drupal 8. We send a request and receive some response from Drupal but what goes internally ? Let's dive in and find out. Of all the important components of Symfony2 being used in Drupal 8, HTTPKernel and HTTPFoundation plays an important role in handling a page request but this time the proces uses an object oriented way. Ohh.. big names :) Let's know something more about these. HTTPKernel : This component consists of an HTTPKernelInterface which provides a method, handle(). This method accepts $request object as parameter and returns $response object. HTTPFoundation : This component is responsible for creating and managing the $request and $response objects. We can say that, it's an object oriented replacement of sup...