Create your first module with Drupal 8

By Arun AK, 21 May, 2017

It may feel crazy to build a module without module file in drupal. But in drupal 8 it is possible. Which means module file is not a mandatory entity in drupal. Instead of writing everything inside module file drupal 8 will make your code more structured by using classes and objects.

Today we are going to create a smile module which creates a sample page using custom menu callback.

First let us list down the steps to create this module in drupal 7:

  1. Create a module folder.
  2. Create file and deine the module details.
  3. Create mycustommodule.module file inside module folder.
  4. Implement hook_menu() to define custom url.
  5. Write the callback function for the custom menu created either within the module file or inside an *.inc file.

Now let us see how we can create the same module in drupal 8

  1. Create a module folder. Let us a give a name to the module “mycustommodule”
    Create file and define the module details like below:

    name: MyCustomModule
    description: My First Drupal 8 Module
    package: Custom
    type: module
    core: '8.x'
    hidden: false
  2. Define your custom path.

    Create mycustommodule.routing.yml file. Then define your path like below:

      path: '/my_custom_page'
        _controller: '\Drupal\mycustommodule\Controller\MyCustomModule::custom_page'
        _title: 'My First Custom Drupal 8 URL'
        _permission: 'access content'
  3. Define your custom url menu.

    To define a url first we need to create Then define the url like below:

      title: 'My First Custom Drupal 8 URL'
      description: ‘My First Custom Drupal 8 Page’'
      route_name: mycustommodule.custom_url
  4. Write the callback function for your custom url.

    In mycustommodule.routing.yml file we have defined the controller as MyCustomModule::custom_page. It means that name of callback function will be custom_page and which is inside class MyCustomModule. Because first we need to define MyCustomModule class.

    Create a new folder inside your module folder and name it as src. Create one more folder inside src folder and name it as Controller. Inside Controller folder create file MyCustomModule.php. Define the our custom controller class like below.

    We are defining our class by extending the features of ControllerBase class. So we need to add ControllerBase class inside your file.


    namespace Drupal\mycustommodule\Controller;

    use Drupal\Core\Controller\ControllerBase;

     * AuthorPanePopup Class defines ajax callback function.

    class MyCustomModule extends ControllerBase {
     * Callback function for custom url.

      public function custom_page() {            
        return array('#markup' => "My First Drupal 8 Custom page content.");

We are done with our first drupal 8 module. Place your module inside module folder which located in your drupal 8 installation root directory. Then go to and enable your new module.

Now try to access the url

I think this article helps you to get some basic ideas about drupal 8 module development. I will come up with more drupal 8 tutorials soon. Cheers!!!