Chapter 2: Hello World

This chapter is part of the Drupal Module Developer Guide.

In this chapter, you’ll write your first module, create a custom block that adds content to a Drupal page, and learn about how info files provide Drupal with information about a module and its capabilities. In addition to getting to write some real code and have Drupal use it, you’ll get to practice navigating your new development environment and the other module development-related tools introduced in the previous chapter.

Tutorials in this course
Categories
Drupal 8, 9, 10, and 11
More information

A Drupal module encapsulates files and directories that serve a specific purpose and follow Drupal's standards and conventions. This tutorial describes the anatomy of a Drupal module, focusing on the placement and purpose of different file types.

In this tutorial, we'll:

  • Explain where Drupal looks for modules and where you should place your custom module.
  • Describe the standard file and directory types in a module.

By the end of this tutorial, you should be able to identify and understand the purpose of various files and directories within a Drupal module and know where to correctly place them.

Categories
Drupal 8, 9, 10, and 11
More information

Every module must have an info file that provides Drupal with metadata about the module. Drupal uses this file to recognize that the directory the info file is contained within, along with the files inside it, are part of a module. Without an info file, Drupal will not recognize your code as a module, and the code will be ignored. Info files are written in YAML and contain information like the module's name, versions of Drupal it's compatible with, and a description.

In this tutorial, we'll:

  • Pick a name for our custom module and create a directory for it.
  • Create an .info.yml file with metadata about our module.
  • Install our new custom module to verify Drupal can locate it.

By the end of this tutorial, you should have a directory for your custom module's code and an info file that tells Drupal this directory contains a module.

More information

Every introduction to coding starts with a "Hello, World!" example, right? With Drupal, it's a bit more complex than just echo "Hello, World!". To follow Drupal best practices, we should provide content from our custom code in a way that allows a site administrator to choose where and when it's shown, instead of hard-coding those decisions. This keeps our Drupal application flexible and customizable.

In this tutorial, we'll:

  • Author a custom block plugin that outputs the string "Hello, World!".
  • Place the block on the home page of our site.

By the end of this tutorial, you should have written the code for a custom block that can display the string "Hello, World!".

This course appears in the following guides:
Module Development
Learn Drupal module development in this hands-on guide.

Drupal Module Developer Guide