Web Developer

Drupal 9 is the latest version of Drupal. There are significant differences between Drupal 8&9 and Drupal 7. Firstly, Drupal 8&9 requires the latest version of php5 to run. Drupal 8&9 uses a PHP framework called Symphony, which relies heavily on OOP. Another major change in Drupal 8&9 is the folder structure. In Drupal 8&9, all core modules are placed within core/ and all other modules are placed in root modules folder. Moreover, there are changes in the way modules are created. Read on to know how to create a custom module in Drupal 8&9.

A Drupal 8&9 custom module consists of three basic files:

  1. mymodule.info.yml holds the module info
        name: My Module
        description: 'My Description Custom Module'
        core_version_requirement: ^8 || ^9 || ^10
        version: 1.0
        type: module
        package: Custom
  2. mymodule.routing.yml file for holding ‘routing’ info or hook_menu
    mymodule.my_page:
      path: '/my-page'
      defaults:
        _controller: '\Drupal\mymodule\Controller\myModuleController::myPage'
        _title: 'Welcome to My Page'
      requirements: 
        _permission: 'access content'
  3. myModuleController.php for controller classes and its callbacks.
    <?php
     namespace Drupal\mymodule\Controller;
     class myModuleController {
      public function myPage() {
        return array(
          '#markup' => 'Welcome to My Custom Page.'
        );
      }
     }
    ?>