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
- In a custom theme, attempt to add a new theme suggestion that does not begin with the theme hook and two underscores
- Enable twig debug and note that the invalid name appears as a theme suggestion
- 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:
- We need to add this rule to the api.drupal.org documentation for this hook
- We ought to prevent invalid theme suggestions from appearing in the twig debug statement
- If possible, we should trigger some errors if an invalid theme_suggestion is added
| Comment | File | Size | Author |
|---|
Issue fork drupal-3343198
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:
- 3343198-
compare
- 3343198-improve-documentation-of
changes, plain diff MR !3507
Comments
Comment #2
andy-blumAnecdotally, 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?
Comment #4
andy-blumComment #5
andy-blumMR updates the theme.api.php doc comments, and filters malformed theme suggestions into a new list where themers can see them.
Comment #6
murilohp commentedHey @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.
Comment #7
andy-blum@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.
Comment #8
andy-blumComment #9
murilohp commentedHey, 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.
Comment #10
DSushmita commentedThe code looks okay to me.
Comment #11
Gazi Akil commentedThis 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'].
Comment #12
smustgrave commentedRemoving tests tag as I see they were added.
Moving to NW for the failure in MR.
Comment #13
andy-blumComment #15
andy-blumComment #16
smustgrave commentedThink this will need a change record.
As existing sites may be using these templates, even though they are invalid
Comment #17
andy-blumChange record drafted.
Comment #18
andy-blumComment #19
smustgrave commentedThanks for the quick turnaround on the change record!
Think this is good for next review.
Comment #20
andy-blumComment #21
andy-blumComment #22
andy-blumComment #23
smustgrave commentedAdditional changes look good to me.
There is 1 thread still open. Should this use the D10 link or D9?
Comment #24
andy-blumCould we craft the link to be whatever version of core is running?
Would we be able to get the core value in the tests?
Comment #25
andy-blumThe other option is to leave the link as-is without a version, and let it resolve to whatever the latest supported version is
Comment #26
smustgrave commentedLooking 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.
Comment #28
andy-blumRemoving 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:
Comment #29
volkswagenchickThank You @andy-blum for taking a moment to educate @Avanishyadav on issue queue etiquette,
Comment #30
gisleDeleted the bogus MR by Avanishyadav.
Comment #32
lauriiiCommitted 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_HOOKdocs too?Comment #34
lauriiiOpened a follow-up #3345789: Improve documentation of hook_theme_suggestions_HOOK().