How to make a new Drupal module which really works

A brief guide to making a LocalGovDrupal module

Guide Navigation

Skip Guide Navigation

First steps - creating your initial files

There are a few basic files you'll need to create to start off your module build. Because in this Guide we're going to make a School Term Dates module, all the filename components which identify the module will be called school_term_dates, but you will name them according to your module's name and purpose. For consistency and compatibility with the rest of the Drupiverse we won't be having any upper case characters in our filenames, and we'll be using the _ underscore character as a separator.

First of all, in your web/modules/contrib folder create a new folder to hold the files for your module, and in that new folder, create two initial files, a module_name.info.yml file and a module_name.routing.yml file:

folder localgov_school_term_dates containing files localgov_school_term_dates.info.yml and localgov_school_term_dates.routing.yml

Also in that folder, create subfolders /src/Controller with a file module_nameController.php in it:

Controller file within the file structure

Now we'll add the actual content to each file in turn. First, the info file localgov_school_term_dates.info.yml

name: 'School Term Dates'      
description: Creates a content type for managing and displaying school term dates.      
package: Simon's modules      
version: 0.1      
type: module      
core_version_requirement: ^9 || ^10 

This is the file which tells Drupal's Extend area that the module exists; the content is hopefully self-explanatory - the name and description of the module, the package indicates which of the list of expanders on the Extend main page the module will appear under, the version number of the module, the fact that it is indeed a module, and which versions of Drupal it's compatible with.

The second file we'll put some text into is the routing file localgov_school_term_dates.routing.yml

localgov_school_term_dates.showContent:
 path: '/school-term-dates'
 defaults:
   _controller: '\Drupal\localgov_school_term_dates\Controller\school_term_datesController::showContent'
   _title: 'School Term Dates'
 requirements: 
   _permission: 'access content'

At the time of writing, I don't know what the first line does or how important what is written is, beyond it being the name of the route :) When I find out in due course I'll come back and update this part of the guide.

The path denotes what the URL for this module is going to be; at this stage of our learning, this module is hardcoded content which will appear on a hardcoded URL www.ourwebsite.gov.uk/school-term-dates - we shall return to this later in this Guide.

Under defaults, the controller parameter refers to the controller .php file we created, together with the function name which does the business, and title is the hardcoded page title for the output page of this module.

Under requirements, permission indicates that users have the permission to view this page.

Finally, our third file, the one which actually does something rather than merely informing the system of the module's existence - school_term_datesController.php

<?php
/**
* @file
* Contains \Drupal\localgov_school_term_dates\school_term_datesController.php.
*/
namespace Drupal\localgov_school_term_dates\Controller;
use Drupal\Core\Controller\ControllerBase;
class school_term_datesController extends ControllerBase {
 public function showContent() {
   return array(
       '#markup' => '' . t('Good afternoon, Mr. Amor. Everything is going extremely well.') . '',
   );
 }
}
?>

This makes use of core Drupal functionality to return the text in the function t().

And when you've gone over to the Extend area of the Drupal control centre and enabled your module (you might also need to flush all the caches), you can go over to the /school-term-dates page and view it in action:

The new school term dates module running on the website, not as yet showing any actual term dates of schools
Last reviewed
20-08-2024
Next planned review
20-08-2025