In Drupal 8, all the custom modules will go in the module folder in the root directory.
Here are the steps to create a simple custom module to print hello world in Drupal 8:
Step 1. Create 'mymodule' folder under modules folder in the root directory.
Step 2. create mymodule.info.yml file.
This file tell drupal about the module.
Here is our mymodule.info.yml file
name: My Module
type: module
description: My Custom Module
core: 8.x
name: My Module
This is the title of our module which will appear on the extend page.
type: module
This is to tell Drupal that we are making a module.
description: My Custom Module
This is a description shown alongside the title on the extend page.
core: 8.x: Means our module is compatible with Drupal 8.x core.
After 2nd step, You will now be able to see the custom module (My Module) in the admin panel of D8. (under admin Extend page)
To enable this module:
Go to adminpanel-> Extend
check the My Module & click on the install button.
Step 3. Creating mymodule.routing.yml
This file tell drupal where the module can be access.This involves routing as configuration and handle the callback in a controller function.
Here is our mymodule.routing.yml =>
mymodule:
path: /view
defaults:
_controller: Drupal\mymodule\Controller\MyController::view
requirements:
_permission: 'access content'
Step 4: Create Route Controller Class under src folder (MyController.php)
we need to add functionality to our module. To do this create the src folder under mymodule. After creating this, make a Controller folder under src.
<?php
namespace Drupal\mymodule\Controller;
class MyController {
public function view() {
return array(
'#title' => 'Hello World!',
'#markup' => 'this is hello world title contents.',
);
}
}
Before visiting the custom page (http://localhost/drupal/view), you need to clear the cache to see the page. Otherwise,you Will see 404 Not Found page.
0 Comment(s)