Tags

,

custom-module-creation-in-drupal-8We know that in every Drupal version there are lots of core and third party modules to develop a website. But many time these core and third party modules are not enough for a requirement. At that moment we need to create our own custom module to complete the require task. In this post I will discuss about creating our own custom module in Drupal 8.

Step 1:- Create module folder and choose module name:
Firstly we need to choose a module name. There are some rules for this
i) The name must be start with a character.
ii) The name must be in lowercase letters and underscores.
iii) The name must be unique. Make sure that there are not any same name module, theme, profile or keyword.
In this blog lets take “test_module” as the name of the module. Now create a folder and name it as the same name as that of the module name. The path of the folder will be either “ modules/ ” or “sites/all/modules/”.

Step 2:- Create “.info.yml” file:
In Drupal 8 the “.info.yml” file is the replacement for “.info” file, which was there in Drupal 7. It provides the information of the module to the Drupal UI administration page. Drupal recognizes the module through this file. This file has to be create in the “test_module” folder and the name will be“test_module.info.yml”.
Now check an example of the file.

name: Test Module
 description: This is the first custom module in Drupal 8
 core: 8.x
 package: Custom
 type: module

– The 1st line is the display name of the module in the administration page.
– The 2nd line is for the description of the module.
– The 3rd line for the Drupal version. we are creating a Drupal 8 module so this will be 8.x.
– The 4th line is for the package of the module. The module name will be display under the package name in the administration page.
– The 5th line will be either “module” or “theme” or “profile” for type of the extension.

Note: In Drupal 7 we used the “files[]” for dynamically loading a file or class or interface. But in Drupal 8 the “files[]” entries fully removed from “.info.yml” file. Now the classes are automatically loaded using the “PSR-0 compatible class loader” mechanism which is present in the core. It is a property of Symphony 2.
Step 3:- Create “.module” file:
Now we have to create a “.module” file. In this file we can implement one or more hooks. The name of the file will be “test_module.module”. This file will be created within the “test_module” folder. This is a PHP file.

Step 4:- Route file:
Routing in nothing but the perfect way to select the exact path for a process/function to execute. If we want to work with hook_menu() in Drupal 8 then we have to create a “test_module.routing.yml” file in our module directory. In Drupal 7, hook_menu() did two things. It defines the process to happen when we visit a URL (‘page_callback’) and it adds a link to that URL in an appropriate menu (‘title’). In Drupal 8, the .routing.yml now handles the ‘page_callback’ part, and hook_menu() is only for the ‘title’ part. Lets check with an example.

test_module.module

 function test_module_menu(){
 return array(
 ‘test-module' => array(
 'title' => 'Test',
 'route_name' => 'test_module',
 'menu_name' => 'main'
 )
 );
 }

test_module.routing.yml

 test_module:
 path: '/test/module''
 defaults:
 _content: 'Path_of_PHP_class :: Method_of_PHP_class'
 requirements:
 _permission: 'TRUE'

i) Every route has a unique name to identify a route. In the first line the “test_module:” is the unique name for this route.
ii) The second line is for the path of URL. When a respective menu item will be clicked on, then a new page will open and its URL will be the previous URL + the path content.
iii) The “defaults:“ section lets us specify a special variable “_content”. That variable refers to the PHP class and the method of the PHP class that will be called when this route fires. For the “_content” key, Drupal know that the value returned from our PHP class is going to be the content for the main part of the page.
iv) The “requirements:” section is the replacement of the “’access callback’ => ‘TRUE’”. If the “_permission:” of the section is true then the PHP class is invoked.

Step 5:- Create Controller file:
In Drupal 8 we are following the MVC architecture (Model-View-Controller). The Controller handles the major part of the main logic, i.e it control the main logic. The Controller file has a specific path for Drupal 8. It is “lib/Drupal/test_module/Controller”.
– The “lib” directory separates all the PHP files from the other module files(YML file, .module file, CSS file etc.)
– “Drupal/test_module/Controller” is a namespace. Every part of the namespace is itself a namespace. “Drupal” is a namespace which is grouping all the classes which are a part of Drupal project. “Drupal/test_module” is a namespace which is grouping all the classes which are the part of the newly created module(test_module). “Drupal/test_module/Controller” is a namespace which is grouping all the classes, which are controllers. Now check one example.

<?php
 namespace Drupal\test_module\Controller;

class TestController {
 public function content() {
 return array();
 }
 }

Step 6:- Create Model file:
In MVC architecture, Model represents the object of the data flowing through an application. In Drupal 8 we are following the MVC architecture. So in Drupal 8 if we need the database access, then we will not write the database query within the module files or any other file. We will create a separate Model file and within the file we will write a class with all the database queries. When we need any database access then just call the Model class method with the correct namespace.
lib/Drupal/test_module/DatabaseStorage.php

 <?php
 namespace Drupal\firewall;

class DatabaseStorage {
 static function functionName() {
 return db_query();
 }
 }
Advertisements