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

Popular posts from this blog

Catch the Batch !

Drupal 8 with Symfony2