Module Development Essentials

This page is archived

We're keeping this page up as a courtesy to folks who may need to refer to old instructions. We don't plan to update this page.

Alternate resources

PHP developers are excited that Drupal 8 has a new object-oriented framework with Symfony components. Are you ready to learn how to leverage these Symfony components in your Drupal 8 module? Then check out this new set of tutorials from our friends at KnpUniversity.

You'll learn some of the new Symfony concepts and tools used in Drupal 8 development, including routes, controllers, the services container, and events. You'll also learn to integrate new tools into your development workflow, including Drupal Console, Webprofiler (now part of Devel), Git, and Composer.

This series isn't just for Drupal 7 developers. If you're a PHP developer familiar with Symfony and you want to know how these components are used in the context of Drupal 8, this series is for you too.

Your trainer is Symfony documentation lead and Drupal community member, Ryan Weaver. He walks you through concepts and application in a custom module. By the end of this series you'll understand how to:

  • Set up a new module with a route and a controller
  • Get services out of the services container and into your module
  • Listen for events and respond to them
  • Utilize tools such as Drupal Console and Webprofiler in your development workflow

If you need a brush-up on object-oriented PHP and other key concepts, check out these prerequisite tutorials:

Tutorials in this course
More information

This tutorial is for PHP developers—including Drupal 7 developers—who want to get a local development environment up and running for Drupal 8 development work. In this tutorial, we will install Drupal 8, fire up the built-in PHP web server, set some variables in php.ini, initialize a Git repository, and discuss how Composer will impact what you commit to Git now and in the future. Finally, we'll walk through how to configure PhpStorm for Symfony development that plays nicely with Drupal 8 projects as well.

Additional resources

Development Environments
Local Development Guide (drupal.org)
PhpStorm
The Wonderful World of Composer — Drupalize.Me
PHP's Built-in web server (manual) — php.net

More information

In this tutorial, we'll create a new module and create a route and controller for it. Remember hook_menu? Well, hook_menu is out and routes and controllers are in! If the YAML files in this lesson piqued your interest, check out our introduction to YAML tutorial to learn more.

Note: See Create an Info File for a Module for up-to-date instructions on info file requirements, which have changed since this video was recorded.

Additional resources

Create an Info File for a Module
An Introduction to YAML — Drupalize.Me
PHP Namespaces in 120 seconds
Clear Drupal's Cache

More information

In this tutorial, we'll use an essential tool for Drupal 8 development: the Drupal Console. We'll take a look at how to list the commands available in Drupal Console and then use a Drupal Console command to clear the route cache.

Note: See the Drupal Console docs "Getting the project" page for the latest information on how to install Drupal Console. You will need to use Composer to install Drupal Console for each project.

Additional resources

Drupal Console — Download and Documentation
Drupal Console: Getting the Project
Tutorial: Drupal Console (Drupalize.Me)
Composer

More information

In this tutorial, you will learn how to return a response through a controller using route wildcards. We'll walk through how the wildcard in the route was created and then how this wildcard gets passed as a parameter to the controller function, enabling you to display a value to the page based on the value in a particular URL path segment using Symfony's Response object.

We're assuming you have the Drupal Console up and running (which we set up in the previous tutorial). The drupal command calls Drupal Console.

Curious about the inner workings of the HTTP response? Dive into Symfony and HTTP Fundamentals, which takes you from HTTP fundamentals to how PHP handles HTTP requests and responses, and integrates what Symfony's tools bring to this flow from request to response.

Additional resources

Symfony and HTTP Fundamentals — Symfony Documentation
Http Foundation Response Object — Symfony Documentation

More information

In this tutorial, you'll learn how to turn on debugging tools through changing values in local.settings.php and then use these tools with the Drupal Console.

We're assuming you have the Drupal Console up and running (which we set up in the previous tutorial). The drupal command calls Drupal Console.

Additional resources

Drupal Console

More information

In this tutorial, we'll continue to look under the hood of Drupal 8, this time using a tool bundled with Devel called Webprofiler. You'll learn how to use the web debug toolbar and the profiler. You'll also learn how to reverse engineer a page to find out the names of the controllers responsible for output on that page.

Note: Webprofiler is now a separate project from Devel but still has it as a dependency.

Note: In order to get the Webprofiler timeline view working you need to add some configuration to your settings.php file. See the README.md file included with the Webprofiler module for more information on how to do this.

Additional resources

Webprofiler project on Drupal.org

More information

In this tutorial, we'll shift gears from routes and controllers to services. First off, you'll learn all about the Service Container, also known as the Dependency Injection Container. This is the basis for understanding these special objects called services and how you can leverage them in Drupal.

Additional resources

Dependency Injection and the Art of Services and Containers

More information

In this tutorial, you'll learn how to refactor the controller and create a service.

Additional resources

Dependency Injection and the Art of Services and Containers — Drupalize.Me

More information

Now that we've created a service, let's configure it. In this tutorial, you'll learn how to register your new service. We'll also be using Drupal Console to ensure everything is working as it should.

Note: The Drupal Console command container:debug is now debug:container.

Note: Drupal Console is no longer maintained. There are other tools and methods for getting a list of services in the container. See the tutorial Discover and Use Existing Services to learn more.

Additional resources

Discover and Use Existing Services
Dependency Injection and the Art of Services and Containers — Drupalize.Me
An Introduction to YAML — Drupalize.Me

More information

In this tutorial, you will learn how to extend the ControllerBase class in Drupal and get services out of the container.

Additional resources

Injecting services in your D8 plugins (lullabot.com)
abstract class ControllerBase — api.drupal.org
PHP Service Container — Drupalize.Me

More information

In this tutorial, we'll explore some of the helper functions in ControllerBase. We'll discover the magic behind these shortcut functions is services!

Additional resources

abstract class ControllerBase — api.drupal.org
PHP Service Container — Drupalize.Me

More information

In this tutorial, you'll learn how to get a service that's in another service by using the special constructor method.

Additional resources

PHP Class Constructors — Drupalize.Me

More information

In this tutorial, we'll inject some configuration into our constructor class and introduce parameters by way of our services YAML file.

Additional resources

An Introduction to YAML — Drupalize.Me

More information

In this tutorial, you'll see how Drupal's development.services.yml file can be used to configure services on a local environment. We'll use it to turn the cache off in our service during local development.

Additional resources

An Introduction to YAML — Drupalize.Me

More information

In this tutorial, you'll learn all about events versus hooks in Drupal 8 and we'll introduce the concept of event listeners.

Additional resources

Events — api.drupal.org

More information

In this tutorial, you will learn how to create an event subscriber with dependency injection tags. You'll learn how to tell Drupal which event we want our code to listen for and what method to call when that event happens.

Additional resources

An Introduction to YAML — Drupalize.Me
Introduction to Interfaces — Drupalize.Me

More information

In this tutorial you'll learn how to make use of the event object that are passed to you and the methods that come with it. You'll also learn how to use dependency injection to add a logger for our class and add this argument to our services YAML file.

Note: Drupal 9 now requires Symfony 4.4. The class mentioned at 0:15, \Symfony\Component\HttpKernel\Event\GetResponseEvent, is deprecated. Use \Symfony\Component\HttpKernel\Event\RequestEvent instead.

Additional resources

Introduction to Interfaces — Drupalize.Me
An Introduction to YAML — Drupalize.Me

More information

In this tutorial, we'll peel back the layers of the render array to find an event listener. We'll take a look at the core.services.yml file to unveil and solve the mystery of what's behind the render array.

Additional resources

An Introduction to YAML — Drupalize.Me