Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Issue #1906506 by Fabianx, steveoliver, Cottser: Added Twig settings implementation: Show theme hook / template suggestions in HTML comments.
A follow-up to #1843034: Make Twig settings configurable, when debug is enabled, let's add our own custom debugging output as HTML comments wrapped around the template. The debugging output helps themers determine which template is being rendered, which theme suggestions are available and which theme /template suggestion has been selected for each piece of theme output.
Something like this:
<!-- THEME DEBUG -->
<!-- CALL: theme('node') -->
<!-- FILE NAME SUGGESTIONS:
* node--article.html.twig
* node--2.html.twig
x node.html.twig
-->
<!-- BEGIN OUTPUT from 'core/modules/node/templates/node.html.twig' -->
<article id="node-1" class="node node-article promoted view-mode-full clearfix" role="article" about="/node/1" typeof="sioc:Item foaf:Document">
<p>(content)</p>
</article>
<!-- END OUTPUT from 'core/modules/node/templates/node.html.twig' -->
Comment | File | Size | Author |
---|---|---|---|
#46 | drupal-default-settings-twig-debug--1906506-46.patch | 1.04 KB | David_Rothstein |
#42 | drupal-default-settings-twig-debug--1906506-42.patch | 1.04 KB | steveoliver |
#22 | 1906506-22.patch | 6.82 KB | star-szr |
#22 | interdiff.txt | 1.08 KB | star-szr |
#21 | 1906506-21-testonly.patch | 3.44 KB | star-szr |
Comments
Comment #1
star-szrHere's the patch.
Comment #2
star-szrFor reference, here's the phrase from the default.settings.php docblock in the patch at #1843034-47: Make Twig settings configurable, to be updated in this issue after #1843034: Make Twig settings configurable gets in.
Comment #3
steveoliver CreditAttribution: steveoliver commentedAttached patch cleans up comment in twig_theme() and adds note about debugging output in comment for 'twig_debug' in default.settings.php.
Note: Git attribution goes primarily to Fabianx, who implemented this earlier in the Twig sandbox (see #1820158: Print template names in HTML comments).
Comment #4
star-szrTalked with @steveoliver in IRC, here's a slightly tweaked comment for default.settings.php that should be easier to skim and makes the handbook link a bit more prominent.
Comment #5
steveoliver CreditAttribution: steveoliver commentedThe item about comments could read a little better.
That's about all I see.
Fabian/jen/carl: RTBC?
Comment #6
Fabianx CreditAttribution: Fabianx commentedLooks really good now => RTBC, unless we need another test?
Comment #7
star-szrI like the new wording, rerolled to fix one thing:
themeing = theming.
After talking to @Fabianx I think we can and should write tests for this, possibly a unit test.
Interdiff is from #4.
Comment #8
star-szrCNW for tests.
Comment #9
star-szrHere's a fairly basic test, I'm not sure if it's worth testing every little bit of the debug markup.
Comment #10
star-szrMessed up the patch naming, should be 1906506-9.
Comment #12
star-szr#9: 1906506-7.patch queued for re-testing.
Comment #13
steveoliver CreditAttribution: steveoliver commentedI think this line needs to be deleted. theme_hook_original seems to be automatically added to theme_hook_suggestions.
Comment #14
star-szrThat line needs to stay so we can show which theme suggestion is in use.
With the line:
Without it:
Comment #15
Damien Tournoud CreditAttribution: Damien Tournoud commentedThis feels like
$current_template = basename($template_file);
.Comment #16
star-szrRegarding my comment in #14, it would be more accurate to say the line needs to be there so that the base template is added to the debug output. In the example I showed, the base template is also the template in use.
@Damien - thanks for that, fixed.
Also made a couple minor whitespace tweaks/fixes to the debug output.
Before:
After:
Comment #17
star-szrOkay, I think this does need more tests.
Before:
After:
Comment #18
star-szrInterdiff for #17, I accidentally re-uploaded the patch from #16. Sorry, testbot!
CNW for more test coverage, I'll work on that tonight.
Comment #19
star-szrdrupal_find_theme_templates() uses strtr(), not str_replace().
Comment #20
star-szrShould probably be:
Comment #21
star-szrAddressed #20 and added quite a bit more test coverage.
New test-only patch and a patch combining the new test with the patch from #16 to show that it exposes the failure there.
Comment #22
star-szrFixing a comment in the test, sorry for all the noise.
Comment #23
steveoliver CreditAttribution: steveoliver commentedpassing tests, verified output locally.
Comment #24
webchickOh man! This feature is AWESOME! It's like devel themer lite in core. Likely to make the forthcoming Twig conversions much easier on people, as well.
Committed and pushed to 8.x. Thanks!
Comment #25
podarokawesome!
thanks!!!
Comment #26
Fabianx CreditAttribution: Fabianx commentedFantastic!
Should we add this to the Twig change notice? I presume, yes.
Comment #27
podarok#26 yup
Comment #28
podaroktag
Comment #29
podarokbetter title
Comment #30
star-szrWhat a nice surprise to wake up to :)
Can the change notice combine this issue and #1843034: Make Twig settings configurable? That would make the most sense to me.
Comment #31
Fabianx CreditAttribution: Fabianx commentedI think it can just be added to the original Twig change-notice as it really just extends the functionality of Twig.
Comment #32
star-szrMy feeling is that would make for a very long change notice.
Comment #33
Fabianx CreditAttribution: Fabianx commentedOkay, then a separate one for both latest changes together :-D.
Comment #34
star-szrI'll try and have one of our core mentoring participants draft up the change notice tomorrow.
Comment #35
dags CreditAttribution: dags commentedWriting change notice.
Comment #36
dags CreditAttribution: dags commentedChange notice: http://drupal.org/node/1922666
Comment #37
podaroktags
thanks!
Comment #37.0
podarokAdding commit message
Comment #38
star-szrThanks @dags, that's great! I just made a couple very minor tweaks to the change notice.
Comment #39
David_Rothstein CreditAttribution: David_Rothstein commentedThis seems to have been partially rolled back for some reason... http://drupalcode.org/project/drupal.git/commitdiff/1ce3eeb
Comment #40
jenlamptonIt's probably because of #1925822: Stop locking me out of my own sites/default directory if Drupal is already insecure :)
Comment #41
star-szrSo do we want a patch to restore the parts that were rolled back?
Comment #42
steveoliver CreditAttribution: steveoliver commentedYeah.
Comment #43
podarok#42 looks good
Comment #44
webchickThis looks good to me, but assigning to Jennifer for a look-over since it's docs-related.
Comment #45
jhodgdonIt's OK, I guess..
The first list item took me a while to parse, and has a minor which/that grammar problem (if you want the word to be "which" there, it needs to be preceded by a comma; otherwise, the word needs to be "that" -- in this case I think "that" is better?)... maybe it could use a comma before "such as" as well? That would probably help readability.
In the second list item, should the dump() function have () on it rather than '' around it? It seems like it should, but I don't know much about Twig (yet). :)
Probably the first line should not end in :, but it looks like the default.settings.php file is all over the map on that. Some of the "doc blocks" (they aren't really doc blocks anyway) don't have a first-line summary, some do and it ends in ".", some do and it ends in ":". So we probably ought to file a separate issue to clean this up. Anyway none of them should really be /** */ docblocks because they aren't documenting anything we can display in the API module anyway. But ... let's deal with that on a separate issue (can someone file one?).
Comment #46
David_Rothstein CreditAttribution: David_Rothstein commentedDo we have an official Drupal rule about that vs. which? I thought "which" could be used with or without a comma, and that's what the dictionary says too (http://www.ahdictionary.com/word/search.html?id=T5157300).
In any case, "that" works also so let's switch to that. And add a comma before "such as". And change 'dump' to dump()...
Should probably get this in and then leave other things to followups? (Especially because this was already committed with the original text and then rolled back apparently by accident.)
Comment #47
jhodgdonI don't think that dictionary is a great reference for usage and commas.... Check Strunk & White, Chicago Manual of Style, etc. If "which" is used to introduce a clause like in the previous patch, it definitely needs a comma, and that is standard English usage, not a Drupal rule. [My "Index to English" usage reference book says to generally use "that" for restrictive clauses and "which" for non-restrictive unless there are other "that" words being used in the sentence. And it says to set off non-restrictive modifiers with commas and not to use commas with restrictive. Restrictive is when the information is essential to the meaning (you can't leave it out without changing the meaning). So in this case, it's probably restrictive and hence should use "that" and no comma; if you think it is non-restrictive you can use "which" and a comma.]
Meanwhile... the new patch is much better, thanks! +1 for RTBC now.
Comment #48
jhodgdonCommitted to 8.x. Thanks!
Comment #49.0
(not verified) CreditAttribution: commentedRevising whitespace in debug output to match committed patch