Improve documentation of hook_theme_suggestions_HOOK_alter()

Created on 20 February 2023, over 1 year ago
Updated 3 March 2023, over 1 year ago

Problem/Motivation

Themers & front-end developers are not given enough information to know exactly why new template suggestions may fail. As discussed in slack with @safetypin, consider the following

THEME.theme

function microsite_theme_suggestions_page_alter(array &$suggestions, array $variables) {
  $suggestions[] = 'microsite_page';
}

This adds a new item to the THEME_DEBUG comments in the markup when twig.config.debug === true, but that template will not be used.

 FILE NAME SUGGESTIONS:
   * microsite-page.html.twig
   * page--photocontest.html.twig
   * page--group--371.html.twig
   * page--group--%.html.twig
   x page--group.html.twig
   * page.html.twig

While this suggestion was added correctly, and appears in the theme debug, it's not a valid template name because it does not start with hook__, as expected in drupal_find_theme_templates() (source).

Steps to reproduce

  1. In a custom theme, attempt to add a new theme suggestion that does not begin with the theme hook and two underscores
  2. Enable twig debug and note that the invalid name appears as a theme suggestion
  3. Create the matching twig file and note that it will not be used, even though a suggestion exists for it

Proposed resolution

There are a couple things that I think we ought to do to make this better:

  1. We need to add this rule to the api.drupal.org documentation for this hook
  2. We ought to prevent invalid theme suggestions from appearing in the twig debug statement
  3. If possible, we should trigger some errors if an invalid theme_suggestion is added
🐛 Bug report
Status

Fixed

Version

10.1

Component
Theme 

Last updated 1 day ago

Created by

🇺🇸United States andy-blum Ohio, USA

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024