Theming

by Daniel Sipos

This page is a placeholder.

Table of contents

Drupal extensions mentioned in this chapter: Gateway, Project Space.

Introduction

[TBA]

pollresult.png
Figure 1-1: A poll.

Business logic vs. presentation logic

[TBA]

Twig

[TBA]

Theme hooks

[TBA]

Render arrays

[TBA]

Assets and libraries

[TBA]

Common theme hooks

[TBA]

Attributes

[TBA]

Layouts

[TBA]

Theming our Hello World module

[TBA]

Final word

The Drupal theming system is complex and flexible and thus impossible to cover fully in one chapter of a module development book. However, we did go through the basics necessary to get you started – understanding the core tenets of the theme system, some of its most important Drupal specificities and practical use cases.

We started this chapter by discussing the abstract principle of separating business from presentation logic – a principle that is used by many modern web applications. We saw why it is critical for flexible and dynamic theming. Next, we discussed a great deal about how Drupal does this separation – the mighty theme hooks that act as a bridge between the two layers. Here, we also covered some of the highly used practices surrounding them – preprocessor functions and theme hook suggestions for added flexibility. Then, we covered how the business logic can actually use theme hooks – the render arrays (perhaps one of the most important Drupal constructs). Also, since we were on the subject, we outlined the Drupal and Symfony render pipeline to get a better understanding of the process that builds the entire page render array. Next, we discussed libraries and how we can "attach" them to render arrays. We will definitely see some more examples later in the book when we talk about JavaScript.

Finally, we started transitioning into the practical aspects of theming a module by exemplifying a few common theme hooks found in Drupal core. In doing so, we also encountered the topic of Attributes, an important one to understand when dealing with making theme hooks more dynamic. We ended the chapter with an overhaul of our Hello World salutation message to create a themable component. We did so by putting into practice much of what we learned about theme hooks earlier on: we defined a theme hook and corresponding template, a preprocess function, as well as a theme hook suggestion, and built a render array dynamically to fire them all. Not a bad day in the life of a Drupal 9 module developer.

In the next chapter, we will look at menus and the different types of menu links in Drupal. What kind of web application would it be without any menu links in it?


Last update: 2021-04-19.