ExamTray App is now Available on Google Play EXAMTRAY android app link

 

How to Create A Custom Module in Drupal 8 using PHP

create custom module drupal 8

Drupal 8 is advances in many ways to Drupal 7. Drupal 8 is a modular CMS (Content Management System). There is a module for each purpose. The need of the hour is that we want to create a custom module in Drupal 8 to suit our web project.

Creating Custom Module in Drupal 8 Step By Step

Before starting, decide whether you really want to create a custom module as you can find an existing module for your specific needs. Some useful ready-made drupal 8 modules are Captcha, Adsense, Google Analytics, PathAuto, SMTP and XMLSitemap. Modules in Drupal are equivalent to Plugins in WordPress.

Step 1: Drupal 8 has a modules folder in the root directory. Just check for it using an FTP Software like FileZilla Client. If you are testing on your PC, browse the root folder like WWW under Server Directory.

Step 2: Let us create a folder with name “mymodule” under module folder. Our custom module name is going to be mymodule.

Drupal 8 Module Folder Structure Screenshot

Step 3: Now Create a File named mymodule.info.yml inside mymodule folder and open the file a notepad or notepad++ editor. Do not forget the filename extension .info.yml. Write the following lines of code into mymodule.info.yml file:

name: My Custom Module
type: module
description: 'My Custom Module is for implementing Ads'
package: Custom
core: 8.x
hidden: false

mymodule.info.yml is just an information file. It contains Module Name to be displayed on Extend Module Section of Drupal 8 Admin Menu. Try to add a meaningful description, so that other developers can understand.

Step 4: Create a new text file with name mymodule.module under mymodule folder. Write some code in it. We usually write hooks in this custom module. hook_form_alter is frequently used.

mymodule.module:

function logme()
{  
  \Drupal::logger('mymodule')->notice('logme called');
}
//there is no ending ?> tag

Try restarting the server once or clear cache to see changes. Enable the module to start using it.

Drupal 8 Module Name Description display

Admin panel displaying Modules Installed in drupal 8

Congratulations on successfully creating your first Custom Module in Drupal 8 CMS.