Book Image

Modernizing Drupal 10 Theme Development

By : Luca Lusso
4 (1)
Book Image

Modernizing Drupal 10 Theme Development

4 (1)
By: Luca Lusso

Overview of this book

Working with themes in Drupal can be challenging, given the number of layers and APIs involved. Modernizing Drupal 10 Theme Development helps you explore the new Drupal 10’s theme layer in depth. With a fully implemented Drupal website on the one hand and a set of Storybook components on the other, you’ll begin by learning to create a theme from scratch to match the desired final layout. Once you’ve set up a local environment, you’ll get familiarized with design systems and learn how to map them to the structures of a Drupal website. Next, you’ll bootstrap your new theme and optimize Drupal’s productivity using tools such as webpack, Tailwind CSS, and Browsersync. As you advance, you’ll delve into all the theme layers in a step-by-step way, starting from how Drupal builds an HTML page to where the template files are and how to add custom CSS and JavaScript. You’ll also discover how to leverage all the Drupal APIs to implement robust and maintainable themes without reinventing the wheel, but by following best practices and methodologies. Toward the end, you’ll find out how to build a fully decoupled website using json:api and Next.js. By the end of this book, you’ll be able to confidently build custom Drupal themes to deliver state-of-the-art websites and keep ahead of the competition in the modern frontend world.
Table of Contents (21 chapters)
1
Part 1 – Styling Drupal
12
Part 2 – Advanced Topics
17
Part 3 – Decoupled Architectures

How to override a template

The templates provided by modules define a reference implementation of a specific theme hook. In our custom alps_weather module, for example, we’ve defined a couple of theme hooks and their related templates. But if you look at Drupal core, or at other contrib modules, you’ll find that they’re full of such reference templates. Starting from the internal elements, up to page.html.twig and html.html.twig, every portion of a page is built by a theme hook and its Twig template.

We do that to separate where data is built from where data is rendered in HTML, and to allow themes to provide an alternate version of default templates.

Defining theme regions

Our demo website is simple, so we need fewer regions than the default set provided by Drupal core. To define them, we need to add those lines to the alps_trips.info.yml file, inside the themes/custom/alps_trips folder:

regions:
  header: Header
  breadcrumb: Breadcrumb...