Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

What Drupal is

Drupal is a content management system (CMS) for complex projects: portals, universities, online magazines. Right after installation it has an admin panel, users, content and menus. Everything that is missing is added with modules — ready-made ones or your own.

Version

The course is written for Drupal 11. It needs PHP 8.3 or newer. Almost all the code in the course also works in Drupal 10.3; the differences are pointed out in the text.

Modern Drupal is built on Symfony components: it has routes, controllers, services and Twig templates. So writing a module is very close to working with a framework.

The panel and the code

Work with Drupal falls into two parts.

What has to be doneWhere it is done
Add an Article content type with fieldsIn the panel, no code
Show a list of content with a filterIn the panel, the Views module
Give permissions to rolesIn the panel
Your own address with your own logicIn a module: a route and a controller
Your own form with validationIn a module: a form class
Change how another module behavesIn a module: a hook
Change the markup of a pageIn a theme: a Twig template

The person who configures the site in the panel is a site builder. The person who writes modules is a developer. The course is about the second role, but the first lessons give the necessary minimum of the first one too.

Where your code lives

Your own code is written in two places:

web/modules/custom/   your modules
web/themes/custom/    your themes

A module is responsible for logic: addresses, forms, data. A theme is responsible for the look. Core and other people's modules are never edited: Composer updates them, and your changes would be lost.

What will be built

The running example of the course is the blog module in the folder web/modules/custom/blog. Step by step it gets:

  • a /blog page with a list of articles;
  • its own template and styles;
  • a form for subscribing to new articles;
  • a module settings page;
  • a Latest articles block;
  • its own database table and a terminal command.

Each lesson adds one file to the module or changes one that already exists. Every example starts with the path to its file.

What you need to know first

You need PHP at the level of classes, namespaces and Composer. The YAML format Drupal uses for its descriptions is simple: pairs of key: value, with nesting set by two-space indentation.

Common mistake

Beginner developers write code where configuration would be enough: they create their own table for articles although Drupal already has content with fields, or write their own list although Views can build it. Before opening the editor, check whether the task can be solved in the panel or with a ready-made module from drupal.org.

Thanks for reading!

Donate to the site — we work hard for you.