Creating custom modules

This guide includes tutorials and other information that you need to create modules for Drupal 8.

Other resources:

Getting Started - Background & Prerequisites (Drupal 8)

Drupal 8 leverages advanced PHP language features & sophisticated 3rd party libraries for better developer experience

Prepare a Module skeleton

In the sub-sections of the guide after this one, we will be building different things. A custom page, block, entity, field, etc. But all the

Naming and placing your Drupal 8 module

Main topic described: module naming and location

Let Drupal 8 know about your module with an .info.yml file

Main topic described: project metadata

Add a composer.json file

A composer.json file can be added to your module to depend on non contributed projects.

A "Hello World" Custom Page Module

The following section will walk you through the creation of a simple module. There is a long tradition of having the first program you write

Adding a basic controller

The content() function in the HelloController class will be returning the markup text, when the routing system invokes the page.

Add a routing file

Back in your module's root folder, where the .info.yml file is located, add a new file called hello_world.routing.yml and add the following

Add a menu link

Now that we have created a placeholder for our module settings page, let us add a menu link for it. The instructions below show how to

Going further

For the truly adventurous, these code examples are the abridged version of the full page example from the Examples module.

Adding Custom Blocks to your custom Module

Contrary to Drupal 7, creating multiple instances of a block to place on your site is the second easiest example of module creation

Create a custom block

Blocks in Drupal 8 are instances of the block plugin.

Add a Form to the Block Configuration

Now let us say that we want to add the ability for the site builder to enter a piece of configuration for each instance of our custom block.

Process the Block Config Form

Add the following method to the HelloBlock class. In this example, it is located in the src/Plugin/Block/HelloBlock.php file, but as you

Use Config in Block Display

To make use of the configuration of instances of the block, we can modify the build() method of the HelloBlock class:

Add a Default Configuration

By adding a single settings yaml file to our module, Drupal will automatically load the contents of that yaml file, and we can access it to

Include default configuration in your Drupal 8 module

Main topic described: default configuration

Adding stylesheets (CSS) and JavaScript (JS) to a Drupal 8 module

This documentation is for modules. For the themes, see Adding stylesheets (CSS) and JavaScript (JS) to a Drupal 8 theme.

A practical guide to building basic Drupal 8 modules

From .info to tests, just the basics

Basic structure

Basic structure


Everything's pretty neat so far, but how do we change what we're seeing? With some forms, of course. /src/Form/LoremIpsumForm.php



Defining a Block

Remember at the beginning of this tutorial when I said we'd define a block with a form? Well, now's the time to get right on it.


Finally for some QA. Let's check that our module works as expected by writing a few tests of our own which can be run via Drupal itself.

Defining and using your own configuration in Drupal 8

Main topic described: defining own configuration

Creating a custom Field

In this guide we will go through the following topics:

Create a custom field type

Field types define the properties and behaviours of fields

Create a custom field formatter

The field formatter formats the field data to be viewed by the end user.

Create a custom field widget

Field widgets are used to render the field inside forms.

Create a custom page

There are two steps in creating a simple page in Drupal:

Building a Views display style plugin for Drupal 8

Creating a Views display style plugin might seem like an intricate task, but it's easier than it looks. Here's a step by step guide on how

Guide maintainers