Build Modules
Modules are how you extend Drupal with PHP. This section opens with a seven-part tutorial where you build a module, an Event RSVP system, from scaffold to tested and shippable, then continues with how-to guides for everything modules commonly do. Full code samples come from a tested companion project.
Tutorial: build your first moduleSeven parts, one running project: scaffold, routes and controllers, forms and services, configuration, a block plugin with caching, permissions, and tests.
Routes and controllersDefine paths in *.routing.yml, return render arrays from controllers, use route parameters, and inject services properly.
FormsBuild, validate, and submit forms with the Form API, including config forms, confirmation forms, and AJAX.
Blocks and pluginsCreate block plugins with attributes, add configuration, and attach the cacheability metadata that keeps them fast.
Work with entitiesCreate, load, query, and update entities, and write entity queries.
Custom entity typesDefine your own content entity types with attributes, fields, and the handlers that control their behavior.
Services and eventsDefine services in *.services.yml, subscribe to events, and decorate core behavior cleanly.
Custom database tablesDefine and query your own tables with the Database API when an entity is more than you need.
Background processingDefer work with cron, queues and queue workers, and long-running batch operations.
Call external APIsMake outbound HTTP requests with the Guzzle-based client, handle responses, and queue slow calls.
LoggingRecord events with a logger channel so operators can see what your module did and why.
Custom field typesDefine a field type, widget, and formatter so site builders can add your data type to any entity.
Menus and linksAdd menu links, tabs, and action buttons from YAML, and generate links with a deriver.
Work with filesWrite and read managed files, generate file URLs, and accept uploads.
MigrationsImport data into entities repeatably with the Migrate API, and roll it back.
Extend Views with handlersExpose your data to Views and write custom field, filter, sort, and argument handlers.
Extend Views with pluginsControl how Views lays out results with style, row, pager, and other plugins.
TestingUnit, kernel, and functional tests with PHPUnit, using attributes and the test base classes core ships.
Planning to build with an AI coding agent? Work through this tutorial by hand first so you can tell good output from plausible output, then see Build with AI agents.
Was this page helpful?
Drupal is a registered trademark of Dries Buytaert.