Practical Guide to Creating Basic Modules in Drupal 8
Introduction
Much has been said—and debated—about the latest version of Drupal—Drupal 8, and one thing is certain: building modules has changed significantly. There are many tutorials focusing on the new concepts, and in each one, the authors advise developers to expect even more changes as the codebase evolves.
Over the past few days, I’ve explored some of these guides, as well as the Examples module, and came across a rather interesting example that I hope will remain relevant for the foreseeable future!
Tutorial Goal
In this tutorial, we’ll build a module that generates lorem ipsum. Simply put, it’s a module that combines random phrases to generate filler text. Users can choose how many paragraphs and how many sentences per paragraph, hit a button, and get some content for layout purposes.
The module provides a simple settings page and a block with a form to configure the volume of generated text. It also includes default settings, permissions, tests, and theming.
This is what the finished structure looks like.
Don't Panic
This code is simple enough to run without complications. If any changes in Drupal core break it, please let me know and I’ll fix it as soon as possible. Try not to just copy and paste—typing the code out will help you retain it!
In any case, as stated in the title, this guide is hands-on. I aim to cover the basics and won’t dive too deeply into what changed or why. Where relevant, I’ll break each file’s code into logical parts to include a few comments.
So without further ado, let’s begin. Starting with the basic structure, go through this article’s subsections to learn the steps you need to follow.
Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.