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

Drupal 8 ... Bring it on !!

Back after a loooonnggg gap so, need a bang to get started again, isn't it ? Well, I have something in store :) Yet not very comfy with Drupal 7 but does that allow me to keep my friend, Drupal 8 waiting at the door ? Will he really wait for me to be done with Drupal 7 ? Nooo way, he is running in his own pace ... so what are we waiting for friends, need to catch up. So, from here on let's dive into the ocean of Drupal 8. It's about a month or even more, just thinking that it's high time I introduce myself with Drupal 8 or say the other way round ;) Friends, I know most of us might be thinking not to skip steps while climbing up but at times even three dots are enough to define an area, so why not let's get started. We may get stuck, not an issue, will keep adding dots so as to get closer to our perfect structure. Let's begin with the Getting Started guide of drupal.org. Will be back soon with my way of moving ahead with Drupal 8. Till

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

404 Expenses Not Found !!

Everyone out there on web must have come across "404", "Page Not Found" messages. The most important thing with these 404s is that they will happen, they are inevitable. Before moving into complexities, lets recollect what a 404 error means. In simple words, 404 is that our server is not able to find what we have requested, so it says “Sorry buddy, I give up." in the form of page-not-found messages. Keeping in mind this inevitability of these 404s, you can wonder what problems it can pose to our site and what these 404 errors can cost us. Imagine a website, trying to fetch dynamic resources for each of it's page request, some of which may result in a 404. Such requests, resulting in a 404 on a heavy traffic site can eventually add up to be actually troublesome. Hold on ... there is more to it. Let's see what happens when Drupal comes into picture. Even while serving a 404 page in Drupal, it does the full "bootstrap", l