How Drupal themes work - Theme inheritance

Last revision:

We previously discussed how Drupal looks for suitable twig files to use:

  • Drupal looks in the currently active theme's directory
  • if it doesn't find one, it looks in the base theme's directory
  • if still doesn't find any it will fall back on the default block template file provided by the core block module.

The same thing happens when Drupal tries to figure out which regions are active:

If the base theme doesn't list any regions, Drupal checks if the base theme itself has yet another theme specified as its base theme. If it does, Drupal will check if that theme has defined a list of regions. If it does, those regions are inherited.

If it doesn't, Drupal keeps going down the theme inheritance hierarchy until it reaches the end.
If it still hasn't encountered a specific list of regions, it falls back on Drupal's default regions:

Let's look at the flow chart of this logical sequence:

In Olivero's case, its theme regions are defined it its own info file:

File: /core/themes/olivero/olivero.info.yml

regions:
 header: Header
 primary_menu: Primary menu
 secondary_menu: Secondary menu
 hero: Hero (full width)
 highlighted: Highlighted
 breadcrumb: Breadcrumb
 social: Social Bar
 content_above: Content Above
 content: Content
 sidebar: Sidebar
 content_below: Content Below
 footer_top: Footer Top
 footer_bottom: Footer Bottom

Assume you are building a theme based on the Starterkit_theme core theme. Which regions will it have?

  • Your info file doesn't list any regions, so you look at its base theme starterkit_theme's info file.
  • /core/themes/starterkit_theme/starterkit_theme.info.yml doesn't list any regions, so you look at its base theme stable9's info file.
  • /core/themes/stable9/stable9.info.yml doesn't list any regions and doesn't list a base theme, so starterkit_theme, stable9, and your theme all fall back on Drupal's default regions.

Theme inheritance

This cascading mechanism is called theme inheritance: themes can inherit and/or override settings (and template files) from a base theme, which in turn inherits and/or overrides settings from its own base theme, and so on, all the way down the chain.

If no suitable template file is found at the end of the inheritance chain, Drupal will fall back on the default template or default settings.

Summary

  • Themes can inherit template files and settings from their base theme, which can in turn inherit template files and settings from their base theme, and so on.
  • Drupal looks for template files in order of specificity based on the name of the template file.