The main role of preprocessor functions is to prepare variables to be used within our Twig templates using template_preprocess functions. These functions reference the theme and template we want to intercept. We can write an example of intercepting the html.html.twig template variables used within our Twig theme as follows:
First we need to create a new file called twiggy.theme.
Within our new file, add the following code:
<?php /** * Implements hook_preprocess_html(). */ function twiggy_preprocess_html(&$variables) { // add to classes $variables['attributes']['class'][] = 'twig'; }
Remember to save the file and clear Drupal's cache since we introduced a new file. If we now refresh our home page and inspect the <body> element, we will see a new class has been added with the value of twig. The code block above added to the attributes class the value of twig. This preprocess function allows us to perform numerous functions to assist with theming in Drupal 8.
Currently, our home page is a little boring, so to kick things up a notch, let's look at how to work with our libraries file to add additional assets such as Bootstrap and Google Fonts.