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

Creating a custom Twig filter

Similar to a Twig function used to extract or generate content, a Twig filter can be used to transform a value into something different.

We can use the same WeatherExtension class that we used before to add our custom functions, but this time we’ll implement the getFilters() method:

public function getFilters(): array {
  return [
    new TwigFilter(
      'celsius_to_fahrenheit',
      [$this, 'celsiusToFahrenheit']
    ),
  ];
}

Staying in the field of meteorology, we’ve implemented a Twig filter to convert a temperature value from Celsius to Fahrenheit. The code for the filter is a simple one-line method:

public function celsiusToFahrenheit(
  float $celsius
): float {
  return $celsius * 1.8 + 32;
}

WeatherExtension is already tagged to be a Twig extension (in the...