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
CommentFileSizeAuthor
#5 Screenshot 2023-02-21 at 10.44.22 AM.png163.61 KBandy-blum

Issue fork drupal-3343198

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

andy-blum created an issue. See original summary.

andy-blum’s picture

Anecdotally, and with zero testing right this second, I seem to recall seeing theme suggestions added with hyphens will appear in the twig debug output as well, but will never be used because they were crafted incorrectly. If that's still the case, can we also make that system able to accept hyphenated suggestions or prevent them from appearing in the twig debug output?

andy-blum’s picture

Status: Active » Needs review
andy-blum’s picture

StatusFileSize
new163.61 KB

MR updates the theme.api.php doc comments, and filters malformed theme suggestions into a new list where themers can see them.

murilohp’s picture

Status: Needs review » Needs work
Issue tags: +Need tests

Hey @andy-blum, the code looks good, just added some stuff there but I'm open to discuss. I think it would be nice to add some tests here, I have to dive into this to see if we have any tests to validate the debug info being rendered, so I'm leaving it as NW and the need tests tag.

andy-blum’s picture

@murilohp - Thanks for the feedback, I've updated the code with your suggestions. I agree some test coverage would be good, probably as part of TwigDebugMarkupTest.php, though we will likely need to create a test suite module to add in some theme suggestions that will fail.

andy-blum’s picture

Status: Needs work » Needs review
murilohp’s picture

Hey, I just added a new hook to the theme_test module with an invalid suggestion and updated the test, let's see how it goes.

DSushmita’s picture

The code looks okay to me.

Gazi Akil’s picture

This hook allows any module or theme to provide alternative theme function or template name suggestions and reorder or remove suggestions provided by hook_theme_suggestions_HOOK() or by earlier invocations of this hook.

HOOK is the least-specific version of the hook being called. For example, if '#theme' => 'node__article' is called, then node_theme_suggestions_node() will be invoked, not node_theme_suggestions_node__article(). The specific hook called (in this case 'node__article') is available in $variables['theme_hook_original'].

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: -Need tests +Needs Review Queue Initiative

Removing tests tag as I see they were added.

Moving to NW for the failure in MR.

andy-blum’s picture

Status: Needs work » Needs review

andy-blum’s picture

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs change record

Think this will need a change record.

As existing sites may be using these templates, even though they are invalid

andy-blum’s picture

Status: Needs work » Needs review

Change record drafted.

andy-blum’s picture

Issue tags: -Needs change record
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Thanks for the quick turnaround on the change record!

Think this is good for next review.

andy-blum’s picture

Status: Reviewed & tested by the community » Needs review
andy-blum’s picture

Status: Needs review » Needs work
andy-blum’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work

Additional changes look good to me.
There is 1 thread still open. Should this use the D10 link or D9?

andy-blum’s picture

Could we craft the link to be whatever version of core is running?

$version = explode('.', \Drupal::VERSION)[0];

$output['debug_info'] .= "\n   See https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Render!theme.api.php/function/hook_theme_suggestions_alter/" . $version;

Would we be able to get the core value in the tests?

andy-blum’s picture

The other option is to leave the link as-is without a version, and let it resolve to whatever the latest supported version is

smustgrave’s picture

Status: Needs work » Reviewed & tested by the community

Looking at other examples in core

@link https://api.drupal.org/api/drupal/namespace/Drupal!migrate!Plugin!migrat... List of source plugins provided by the core Migrate module. @endlink

They don't seem to be specifying either so think we can do as #25 mentioned and rely on latest supported version.

andy-blum’s picture

Removing credit for @Avanishyadav. Simply opening a MR with no commits does not warrant a contrib credit.

@Avanishyadav, if you're genuinely interested in contributing please be sure to adhere to issue ettiquette, and not just gaming the credit system as it appears you're doing in this issue and the issues below:

volkswagenchick’s picture

Thank You @andy-blum for taking a moment to educate @Avanishyadav on issue queue etiquette,

gisle’s picture

Deleted the bogus MR by Avanishyadav.

  • lauriii committed 0ac4b763 on 10.1.x
    Issue #3343198 by andy-blum, murilohp, smustgrave, safetypin: Improve...
lauriii’s picture

Status: Reviewed & tested by the community » Fixed

Committed 0ac4b76 and pushed to 10.1.x. Thanks!

I don't think this issue needs CR. It is a DX improvement. I don't think anyone. needs to be actively aware of this feature and I don't see that there would be any action for anyone to take as a result of this.

Should we add similar documentation to hook_theme_suggestions_HOOK docs too?

lauriii’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.