Tutorial introduction

Last updated on
12 September 2023

Intro

This guide was initially written when Drupal 8 was launched, before even Config Sync was fully fleshed out. Although there was a (slightly naive) hope for it to remain current, it grew to be very outdated in the following years - its purpose, however, has always been to flatten the learning curve for developers who wished to learn the basics of module building. With Drupal 10 out in the wild, this guide has been given a much-needed update. Read on to find out how to build a simple, but D10-compliant module, from start to finish.

Tutorial goal

Only local images are allowed.

What we're building in this tutorial is a module for lorem ipsum generation. Simply put, it's a module that combines random phrases to create filler text. Users can select how many paragraphs of text and how many sentences on each paragraph, hit a button and get some material for their layout needs.

The module provides a simple settings page and a block with a form for customizing how much text to generate via a Service. There's also default settings, permissions, tests and theming involved.

πŸ“ loremipsum
β”œβ”€β”€ πŸ“ config
β”‚   β”œβ”€β”€ πŸ“ install
β”‚   β”‚   └── loremipsum.settings.yml
β”‚   └── πŸ“ schema
β”‚       └── loremipsum.schema.yml
β”œβ”€β”€ loremipsum.info.yml
β”œβ”€β”€ loremipsum.install
β”œβ”€β”€ loremipsum.links.menu.yml
β”œβ”€β”€ loremipsum.module
β”œβ”€β”€ loremipsum.permissions.yml
β”œβ”€β”€ loremipsum.routing.yml
β”œβ”€β”€ loremipsum.services.yml
β”œβ”€β”€ README.md
β”œβ”€β”€ πŸ“ src
β”‚   β”œβ”€β”€ πŸ“ Controller
β”‚   β”‚   └── LoremIpsumController.php
β”‚   β”œβ”€β”€ πŸ“ Form
β”‚   β”‚   β”œβ”€β”€ LoremIpsumBlockForm.php
β”‚   β”‚   └── LoremIpsumForm.php
β”‚   β”œβ”€β”€ πŸ“ Plugin
β”‚   β”‚   └── πŸ“ Block
β”‚   β”‚       └── LoremIpsumBlock.php
β”‚   └── πŸ“ Service
β”‚       └── LoremIpsumService.php
β”œβ”€β”€ πŸ“ templates
β”‚   └── loremipsum.html.twig
└── πŸ“ tests
    └── πŸ“ src
        └── πŸ“ Functional
            └── LoremIpsumTests.php

This is what the finished structure looks like

Don't panic

This code is simple enough to work without complications. If any change to Drupal core breaks it, please do let me know and I'll fix it ASAP. Try not to copy and paste but instead write the code as it appears: it'll help you memorize it!

Anyway, this guide is, as stated in the title, a practical one. We'll try to cover the basics and won't get into much detail about what has changed and why. Where applicable, code will be broken down into logical parts so as to include a few comments.

So without further ado, let's get started. Starting with the basic structure click through the subsections of this article for the various steps to be taken.

Help improve this page

Page status: No known problems

You can: