Drupal 8 is advanced 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. Let us follow this drupal tutorial.
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.
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.
Admin panel displaying Modules Installed in drupal 8
Congratulations on successfully creating your first Custom Module in Drupal 8 CMS.