A practical guide to building basic Drupal 8 modules

Last updated on
9 January 2017

From .info to tests, just the basics

Intro

Much has been said - and argued - about the latest release of Drupal - Drupal 8, and one thing's for sure: module building has changed a great deal. There's plenty of tutorials covering the new ground, and in every one of them, authors advise developers to look out for even more modifications as the code matures.

Over the last few days, I've investigated some of those tutorials, as well as the Examples module and have come up with a pretty neat example - one that I hope remains current in the near future!

Tutorial goal

Tutorial goal
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 customising how much text to generate. There's also default settings, permissions, tests and theming involved.

Folder structureFile structure
This is what the finished structure looks like

Don't panic

Although Drupal 8 is still in development, this code is simple enough to work without complications. However if any change breaks it, please do let me know and I'll fix it ASAP. Try not to copy and paste but instead write the following code: it'll help you memorise it!

Anyway, this guide is, as I stated in the title, a practical one. I aim to cover the basics and won't get into much detail about what has changed and why. Where applicable, I'll break each file's code into logical parts so as to include a few comments.

So without further ado, let's get started. Click through the subsections of this article for the various steps to be taken.