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 #1961872 by Cottser, widukind, foopang, shanethehat, nikkubhai, c4rl, thedavidmeister: Convert html.tpl.php to Twig.
(as of #50)
Task
Use Twig instead of PHPTemplate
Remaining
- Code review
Related
#1885560: [meta] Convert everything in theme.inc to Twig
#1987510: [meta] Convert all core *.tpl.php templates to Twig as singular patch
Comment | File | Size | Author |
---|---|---|---|
#49 | 1961872-49.patch | 6.26 KB | star-szr |
#46 | 1961872-46.patch | 6.31 KB | star-szr |
#46 | interdiff.txt | 528 bytes | star-szr |
#42 | interdiff.txt | 1007 bytes | shanethehat |
#42 | twig-theme-html-1961872-42.patch | 6.34 KB | shanethehat |
Comments
Comment #1
foopang CreditAttribution: foopang commentedComment #2
foopang CreditAttribution: foopang commentedComment #4
nikkubhai CreditAttribution: nikkubhai commentedComment #6
star-szr@nikkubhai - Welcome to Twigland! An interdiff is nice when making changes to patches :) I think this hunk from #2 is correct though, there should be no space before {{ html_attributes }}:
Running the failing tests locally should help track down where the failures and exceptions are coming from.
Comment #7
star-szrInformation on enabling simpletest module and running tests locally:
http://drupal.org/node/519364
http://drupal.org/node/30036
Comment #8
nikkubhai CreditAttribution: nikkubhai commentedThe exception message:
page_top could not be found in _context in "core/modules/system/templates/html.html.twig" at line 67
can be removed by using
Comment #9
star-szrTagging.
Comment #10
star-szrI'm looking into this one, definitely some work needed in the preprocess/process layers.
Comment #11
star-szrUpdated docs throughout and t filter per http://drupal.org/node/1823416#filters.
The template_process_html() tweak should fix most of the test failures. Drupal\system\Tests\Bootstrap\PageCacheTest is still failing locally for unknown reasons, let's see if anything else is broken.
Comment #12
star-szrTurns out Drupal\system\Tests\Bootstrap\PageCacheTest fails locally on HEAD for me.
Comment #13
thedavidmeister CreditAttribution: thedavidmeister commented#12 - I get that too! but for every issue I work on and it's been happening for months.
Comment #14
thedavidmeister CreditAttribution: thedavidmeister commented#11: I have some questions..
why do we need to drupal_render the page top/bottom in a process function?
why do we need a process function?
also, please remove any references to PHP data types from Twig template docs.
Comment #15
star-szrRemoving the drupal_render() from page top/page bottom results in half of the CSS and JavaScript files not being included, so I think it's fine to keep them. Should have noted that here though. We avoid drupal_render() so that we pass render arrays rather than strings to templates (and avoid unnecessary rendering), but in this case I think any alterations to page_top and page_bottom need to happen in the preprocess/process layers.
I haven't experimented yet but I think moving the logic in template_process_html() to the end of template_preprocess_html() at this point would break things. I'm thinking RDF for example, I'll give it a shot though.
Edited to expand on first paragraph.
Comment #16
star-szrMoving the logic from template_process_html() to the end of template_preprocess_html() didn't obviously break anything, but in my opinion doing so at this time would limit the usefulness of MODULE_preprocess_html(), THEME_preprocess_html(), and related hooks. At this stage I think we should keep template_process_html() and consider removing it as part of a greater shift - #1843650: Remove the process layer (hook_process and hook_process_HOOK).
Updated docs in html.html.twig per #14 and http://drupal.org/node/1823416#datatypes, and added
@see template_process_html()
.Comment #17
thedavidmeister CreditAttribution: thedavidmeister commented#16 - I'm happy with those revisions to the docs and your explanation of the process stuff.
Is this really a manual testing issue?
If this template didn't work at all you'd be looking at about 50k failed tests. If there's some part of the template you're worried about, like a valid DOCTYPE or attributes being applied to the html and body tags correctly, and we don't already have automated tests for those then we should be writing them right now as they're very important to get right.
Comment #18
thedavidmeister CreditAttribution: thedavidmeister commentedlooking into tests.
Comment #19
thedavidmeister CreditAttribution: thedavidmeister commentedThere's an error being thrown on my local:
"User warning: page_top could not be found in _context in "core/modules/system/templates/html.html.twig" at line 49 in Drupal\Core\Template\TwigTemplate->getContextReference() (line 50 of core/lib/Drupal/Core/Template/TwigTemplate.php)."
It only seems to appear when navigating the admin sections using the Overlay - I think overlay_page_alter() or a related function is causing trouble. The error appears in either the overlay or in the parent page behind the overlay. Seeing the error and refreshing the page can cause it to disappear if the overlay is not active. If the overlay is active and the error has not appeared then refreshing the page causes it to appear in the parent page.
Adding this to the template resolves the issue for me but I don't know enough about the problem to know whether this is the best solution:
Comment #20
star-szrGreat find @thedavidmeister, I'm on it.
Comment #21
thedavidmeister CreditAttribution: thedavidmeister commentedI guess I also answered my own question.
Comment #22
widukind CreditAttribution: widukind commentedreassigning this to myself, OKed with @cottser.
Comment #23
star-szrWe can remove this @see, template_process() itself is gone in D8 - #1290694: Provide consistency for attributes and classes arrays provided by template_preprocess().
Comment #24
widukind CreditAttribution: widukind commentedAs @thedavidmeister was suspecting, the overlay module is causing the error.
overlay_page_alter()
is setting the "#access" flag to FALSE in the "page_top" renderable array, since it identifies it as a region to be skipped.this is causing
drupal_render()
to return NULL further downstream intemplate_process_html()
when it is trying to render "page_top".Not sure what to do about this, other than checking for a truthy value of "page_top" before outputting it in the template.
("page_bottom" does not seem to be affected by that, so I left it as-is)
Comment #26
c4rl CreditAttribution: c4rl commented#24: 1961872-24.patch queued for re-testing.
Comment #27.0
(not verified) CreditAttribution: commentedUpdate remaining
Comment #28
star-szrI double checked the patch to see if it was just testbot being cranky and it wasn't. This just fixes the syntax on the if for now.
Comment #29
thedavidmeister CreditAttribution: thedavidmeister commented#24: 1961872-24.patch queued for re-testing.
Comment #30
thedavidmeister CreditAttribution: thedavidmeister commentedWrapping the page_top in an if inside the Twig template is a hack, especially if it's undocumented and the page_bottom isn't also wrapped in an if - Apologies for suggesting it in the first place.
FWIW, I tried to replicate the same bug for page_bottom and could only do it when I removed
$variables['page_bottom'] .= drupal_get_js('footer');
from the process function - showing that this really is just an issue with data types, and a clue for a fix :)What about casting the variables to strings in template_process_html() instead, like this:
// Render page_top and page_bottom into top level variables.
$variables['page_top'] = isset($variables['page']['page_top']) ? (string) drupal_render($variables['page']['page_top']) : '';
$variables['page_bottom'] = isset($variables['page']['page_bottom']) ? (string) drupal_render($variables['page']['page_bottom']) : '';
Or even this - #1975442: drupal_render() should always return a string..
Comment #31
widukind CreditAttribution: widukind commentedAs outlined in comment #30, I removed the conditional from the template.
For the sake of moving along, I added type casting to template_process_html().
Modifying
drupal_render()
to always return a string is clearly the way to go. If and when this passes then the type casting becomes obsolete and should be removed.Comment #32
thedavidmeister CreditAttribution: thedavidmeister commented#31 - This #1975462: Allow and test for NULL and integer 0 values in Twig templates. would help too.
Comment #33
thedavidmeister CreditAttribution: thedavidmeister commented#1975442: drupal_render() should always return a string. just got committed. We should be able to remove the type casting now.
Comment #34
widukind CreditAttribution: widukind commentedremoved type casting.
Comment #35
thedavidmeister CreditAttribution: thedavidmeister commented+ * variable, already prepared to be output as TITLE tag. Ths variable may
Ths?
+ * - slogan: The slogan of the site, if any, and if there is no title.
This sentence is wildly awkward.
I feel like these variables could be better documented in the order they are required to be rendered - page_top, page, page_bottom.
Comment #36
c4rl CreditAttribution: c4rl commentedMade documentation changes as suggested in #35.
Interdiff with respect to #34.
Comment #37
c4rl CreditAttribution: c4rl commentedComment #38
c4rl CreditAttribution: c4rl commentedComment #39
shanethehat CreditAttribution: shanethehat commentedI can only see two little nitpicks:
There's no need to @see the twig file that this function belongs to. That's what the 'Default template' statement is for.
It's enough to just refer to the preprocess, not the template.
Comment #40
star-szrAgreed with #39, good catches.
When referring to the page variable in page_top and page_bottom, let's put 'page' in single quotes per http://drupal.org/node/1823416#variables-inline.
Comment #41
shanethehat CreditAttribution: shanethehat commentedFixed the two minor points from #39
Comment #42
shanethehat CreditAttribution: shanethehat commentedAnd from #40
Comment #43
thedavidmeister CreditAttribution: thedavidmeister commentedRTBC then?
Comment #44
shanethehat CreditAttribution: shanethehat commented+1
Comment #44.0
shanethehat CreditAttribution: shanethehat commentedWe'll cleanup scattered docs later.
Comment #44.1
jenlamptonremove sand
Comment #45
alexpottThe conversion looks good... we just need to some profiling done.
Comment #46
star-szrSmall performance tweak attached, but even without it I think the performance is definitely acceptable.
Profiling done on a node page using the Stark theme and APC class loader enabled.
The first is HEAD vs. itself - to show that the baseline run used for profiling is accurate.
The second is HEAD vs. #42.
The third is HEAD vs. the attached patch.
=== html-head..html-head compared (518e77f228c59..518e79892b1de):
ct : 29,106|29,106|0|0.0%
wt : 298,247|298,197|-50|-0.0%
cpu : 275,468|275,421|-47|-0.0%
mu : 27,190,952|27,190,952|0|0.0%
pmu : 27,246,256|27,246,256|0|0.0%
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=518e77f228c59&...
=== html-head..html-1961872-42 compared (518e77f228c59..518e79fd1bf6e):
ct : 29,106|29,201|95|0.3%
wt : 298,247|299,203|956|0.3%
cpu : 275,468|276,890|1,422|0.5%
mu : 27,190,952|27,230,136|39,184|0.1%
pmu : 27,246,256|27,280,208|33,952|0.1%
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=518e77f228c59&...
=== html-head..html-1961872-46 compared (518e77f228c59..518e79c023047):
ct : 29,106|29,193|87|0.3%
wt : 298,247|298,789|542|0.2%
cpu : 275,468|274,673|-795|-0.3%
mu : 27,190,952|27,226,680|35,728|0.1%
pmu : 27,246,256|27,276,592|30,336|0.1%
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=518e77f228c59&...
Comment #47
alexpott+1. Ready for #1987510: [meta] Convert all core *.tpl.php templates to Twig as singular patch
Comment #48
alexpottFixing title...
Comment #49
star-szrJust a reroll for #1993350: Don't add modernizr everywhere.
Comment #50
star-szrComment #50.0
star-szrUpdated issue summary.
Comment #51
alexpottCommitted 8ec70c2 and pushed to 8.x. Thanks!
Comment #52.0
(not verified) CreditAttribution: commentedUpdated issue summary.