How to make a new Drupal module which really works

A brief guide to making a LocalGovDrupal module

Guide Navigation

Skip Guide Navigation

Step two - turning it into a Content Type

'That's all very well', I'm sure you're thinking 'but I don't need to make a custom module in order to create a page of content with a single quote from 2001: A Space Odyssey / The Sentinel'. If you're anything like me you'll perhaps be a little irked at having wasted your time on yet another pointless click that is basically the same tutorial you've already seen many times from a DuckDuckGo search for 'how to create a drupal module' where they all proudly end with the line 'congratulations - you have now created your first Drupal module' without going any further.

I do not want to be that barbaric to you and end it there, not even for the easy clicks such a tutorial will almost certainly be attracting to my site already.

So we don't want a single hard-coded page on a single hard-coded URL, what we really want is to be able to have it as a custom content type - one which will ultimately contain a page of school term dates for the current school year, but we're doing baby steps here, so in this step we're going make the necessary changes to the files we've already created to add it to the Content menu of the admin area, so you can create as many pages with the 2001 quote as you like.

First of all, add a dependencies declaration to your .info.yml file thus:

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

At this stage, I don't know what that does.

Note also that we've updated the version number by 0.1 to 0.2 - there's no real need to do that for the purposes of our exercise here, but it's generally good practice. If you're following really good practice, of course, you'll also be depositing and updating these files as you go along in your own Git repository.

Next we need to create a new file in our main module directory, localgov_school_term_dates.module and put some content into it.

Filesystem with .module file added
<?php
/**
* Implements hook_entity_type_build().
*/
function my_custom_content_type_entity_type_build(array &$entity_types) {
 $entity_types['node']->setClass('Drupal\localgov_school_term_dates\NodeTypeSchoolTermDates');
}
?>

In the first instance, that implements the Drupal hook hook_entity_type_build into the build of our module.

We now create another file in the directory of our module, NodeTypeSchoolTermDates.php and add some code to it.

The new .php file added to the filesystem
<?php
namespace Drupal\localgov_school_term_dates;

use Drupal\node\NodeTypeBase;

/**
 * Provides a custom content type.
 */
class NodeTypeSchoolTermDates extends NodeTypeBase {

  /**
   * {@inheritdoc}
   */
  public function postSave(NodeTypeInterface $node_type) {
    parent::postSave($node_type);

    // Define the custom content type settings.
    $config = \Drupal::configFactory()->getEditable('node.type.' . $node_type->id());
    $config->set('name', 'School Term Dates');
    $config->set('description', 'A content type module for displaying school term dates.');
    $config->set('new_revision', TRUE);
    $config->save();
  }
}
?>
Last reviewed
21-08-2024
Next planned review
21-08-2025