Back to blog
Drupal Notes • Drupal 11

Back to Drupal After Ten Years: Building a Council Service in Drupal 11

I last wrote Drupal code over a decade ago and have spent the years since on WordPress, Python and AI. Here is what changed when I came back to build a bin day finder for a fictional council.

Focus
Drupal 11
Read time
9 min
Series
Drupal Notes
Theme
Public-sector web

Most of my recent work is Python, FastAPI and AI systems, and I have built WordPress sites for around fifteen years. Drupal was something I last used more than ten years ago. When I started looking at public-sector web roles, I wanted proof, for myself as much as anyone, that I could still build properly in it.

So I built a small but complete project: Council Services on Drupal 11. Residents of the fictional Demo City Council enter a postcode and see their next black, blue and brown bin collections. It has a custom module, a custom accessible theme, 26 automated tests and a CI pipeline. The code is on GitHub at github.com/SamAde1203/council-services-drupal.

Drupal 11 feels much more like modern PHP than the Drupal I remember.
Part 1

What Changed Since I Last Used Drupal

The biggest change is that Drupal now reads like a well-structured Symfony application. The ideas I use every day in Python, such as small services, dependency injection and configuration kept out of code, are the normal way of working.

ThenDrupal 11
Procedural hooks in a .module fileHook classes with a #[Hook] attribute
Annotations in doc comments for pluginsPHP attributes such as #[Block]
Global functions and static callsServices injected through the container
Settings hard-coded or in variablesConfiguration with a schema, exportable and deployable
Tests as an afterthoughtPHPUnit unit, kernel and functional tests built into core
Part 2

Hooks Are Now Classes

Hooks used to be functions named after the module. In Drupal 11 they can live in a class, marked with an attribute. The class can use traits and injected services like any other code.

Example
final class BinDayFinderHooks {

  #[Hook('theme')]
  public function theme(): array {
    return [
      'bin_day_finder_results' => [
        'variables' => [
          'postcode' => NULL,
          'verified' => TRUE,
          'collections' => [],
        ],
      ],
    ];
  }

}

Plugins work the same way. The sidebar block that puts the postcode form in any region is declared with a #[Block] attribute instead of a doc-comment annotation.

Part 3

Services Behind Interfaces

The module has two services. PostcodeLookup checks a postcode exists using the postcodes.io API. CollectionScheduleProvider turns a postcode into collection dates. Each sits behind an interface and is wired up in the services file.

Example
bin_day_finder.postcode_lookup:
  class: Drupal\bin_day_finder\Service\PostcodeLookup
  arguments:
    - '@http_client'
    - '@cache.default'
    - '@config.factory'
    - '@datetime.time'
    - '@logger.channel.bin_day_finder'

This matters for a council. The schedule here is demonstration data, but a real council would connect the same interface to its waste-management system. Nothing else in the module would need to change, and the tests can swap in a fake HTTP client so they never call the real API.

Part 4

Designing for When Things Fail

A public service shouldn't stop working because a third-party API is down. If postcodes.io returns a 404, the postcode isn't real and the resident sees a clear error. If it times out or returns a server error, the module logs a warning and still gives the resident an answer, labelled as unverified.

  • A 5-second timeout, so a slow API never hangs the page.
  • Successful lookups cached for 24 hours, tagged so that saving the settings clears them.
  • Rate limiting with Drupal's flood service: 20 searches per visitor per hour by default.
  • An admin settings page, so staff can change these without a developer.
Part 5

Two Bugs the Tests Caught

The functional tests drive the real form through a browser-like client. They found two bugs I would not have spotted by clicking around.

1. A crash on the second search

I had written the form's injected services as private readonly properties, which is my habit in modern PHP. The first search worked and the second crashed. Drupal serialises a form between requests and re-injects its services when it wakes it up. That code runs in a parent class, so it can't write to private readonly properties declared in the child. Changing them to protected fixed it, and I left a comment in the class explaining why.

2. Old results beside a new error

Search for a valid postcode, then type an invalid one, and the page showed the new error above the previous postcode's dates. A resident could easily read the wrong dates. The fix is to clear any stored result at the start of validation.

Example
public function validateForm(array &$form, FormStateInterface $form_state): void {
  // Never show the previous postcode's dates alongside a new search's error.
  $form_state->set('result', NULL);
  unset($form['results']);
  // ...flood check, then the postcode lookup
}
Part 6

How I Worked

I used Claude Code as a pair programmer throughout, the same way I do in my Python work. It sped up the parts I had forgotten, such as file layouts and the exact names of core services. I reviewed every change, and the tests, coding standards checks and accessibility checks in CI decided what was finished, not the assistant.

The local setup uses DDEV and Docker, and one command installs the site, the module, the theme and a welcome page. Getting that working on Windows taught me more than I expected, from line endings in shell scripts to a corrupted database image, and it's now documented in the README.

Part 7

What I Would Do Next

  • Connect the schedule interface to a real waste-management API.
  • Add bin day reminder emails using Drupal's Queue API and cron.
  • Run PHPStan at a high level alongside the coding standards.
  • Move the theme's preprocess functions into hook classes before Drupal 11.5 deprecates .theme files.
  • Test manually with NVDA and VoiceOver, on top of the automated checks.
Coming back after ten years, the platform had moved on, and the engineering habits I use every day transferred directly.