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.
Task
Use Twig instead of PHPTemplate
Remaining
- Replace all theme functions and templates with .html.twig equivalent templates
- Add new preprocess functions for the .html.twig equivalent templates
- Update all hook_theme definitions
Related
#1885560: [meta] Convert everything in theme.inc to Twig
#311011: Replace links.html.twig with item-list--links.html.twig
Comment | File | Size | Author |
---|---|---|---|
#112 | interdiff.txt | 3.74 KB | joelpittet |
#112 | 1939064-twig-links-112.patch | 16.62 KB | joelpittet |
#107 | 1939064-twig-links-106-reroll.patch | 13.17 KB | joelpittet |
#102 | interdiff.txt | 1.31 KB | star-szr |
#102 | 1939064-102.patch | 13.73 KB | star-szr |
Comments
Comment #1
psynaptic CreditAttribution: psynaptic commentedComment #2
joelpittetLeft the debug messages where I am having trouble with the active link tests.
Tests that are failing are at
/Users/joel/Sites/d8/html/core/modules/system/lib/Drupal/system/Tests/Theme/FunctionsTest.php
one is because of template_process line #2642
The other is because they expect being an active link in the tests.
Have a crack:)
Comment #3
joelpittetWhoops add template
Comment #4
tlattimore CreditAttribution: tlattimore commentedNot sure if this is supposed to be tagged for review, but I don't see anything noting that it shouldn't be.
Comment #6
joelpittetNeeds this patch for part of this
#1938430: Don't add a default theme hook class in template_preprocess() where the extra class is added.
And may need this patch as well (which could be tested) for the active class on the links...
#1922454: Gut l(), fix theme('link'), have a working framework for consistently generated links inside and outside of Twig
@steveoliver was having a look at those two items as well. Correct me if I am wrong but you came to the same conclusion?
Comment #7
star-szrI would think #1938430: Don't add a default theme hook class in template_preprocess() can be worked around/overridden in preprocess just by instantiating a new Attribute object, but I can definitely understand postponing on #1922454: Gut l(), fix theme('link'), have a working framework for consistently generated links inside and outside of Twig.
Comment #8
joelpittet@Cottser, would overriding the template_preprocess() need to be removed at a later date?
Comment #9
star-szrNot necessarily, but I see your point.
Comment #10
star-szrThis could use manual testing once we pick it back up.
Comment #11
star-szrAs discussed on IRC, let's pick this back up again, rolling a patch combined with #1922454: Gut l(), fix theme('link'), have a working framework for consistently generated links inside and outside of Twig and if need be, #1938430: Don't add a default theme hook class in template_preprocess().
Comment #12
joelpittettweaked up the preprocess bit to remove the links... but having issues on the active class on Theme functions in Theme tests.
Comment #14
joelpittetOk so still don't know what to do about the active links test but this should solve the exception.
Comment #16
joelpittet@Cottser, thanks for the suggestion to look at the span attributes, it was pulling the wrong bits and I spotted another missing bit. item.text was missing {{}} on the last part.
So with this patch the only bit left to fix is the 'active' class I hope.
Comment #17.0
(not verified) CreditAttribution: commentedadded item_list issue
Comment #18
jenlamptonLooks like a few tests need to be updated. on it.
Comment #19
jenlamptonOkay, well it wasn't the tests that were the problem. For some reason the {% spaceless %} tag doesn't seem to do anything within the for loop. I added some whitepsace controlers in there to take care of the line breaks and spaces.
The tests are also testing for the ability to add an ID for the heading. We didn't have any code in our preprocess for IDs, only classes, so I added that.
It also looks like for some reason our front page link is getting an active class (both the A tag and the LI tag) when tests are running. Not sure why that is, but it's definitely an issue. in our preprocess we have
$is_current_path = ($link['href'] == current_path() || ($link['href'] == '<front>' && drupal_is_front_page()));
Not sure why this wold return true during testing, but it does.I'm not sure the tests are actually running our preprocess function either, even after making the changes noted above, the tests still fail with missing IDs on the heading tags.
still needs work.
Comment #20
joelpittetChanges from #19 incorporated. Thank you @jenlampton docs and whitepsace. Then reverted my edits for the header[id] bug you found.
Comment #21
joelpittet@jenlampton sorry my interdiff was messed up. This it the diff between #19-#20.
Comment #23
joelpittetOk so drupal_is_front_page() call wasn't releasing the static variable so I added a bounce sheet to the tests so that it would pickup the new "front page". i.e.
drupal_static_reset('drupal_is_front_page');
This should pass test as they passed locally. *crosses fingers*
Comment #24
star-szrGreen! Fantastic work @joelpittet and @jenlampton!
Comment #25
jenlamptonreviewing :)
Comment #26
jenlamptonManual testing:
The only difference in the markup is the order of the classes on the UL tag, great job guys!
before:
after:
Docs look pretty good, verbose, but more is better than less. :)
Comment #27
star-szrJust fixing indent level and removing a consolidation-type @todo, see #1757550-38: [Meta] Convert core theme functions to Twig templates for reasoning on the @todo removal.
Comment #29
star-szr#27: 1939064-27.patch queued for re-testing.
Comment #30
star-szrAnd back to RTBC per #26.
Comment #31
alexpottComment #32
joelpittetpatch does not apply, needs a re-roll on #27
Comment #33
pwieck CreditAttribution: pwieck commentedRe-rolling #27 now
Comment #34
pwieck CreditAttribution: pwieck commentedre-rolled
Comment #36
pwieck CreditAttribution: pwieck commentedI will fix this re-roll my mistake.
Comment #37
pwieck CreditAttribution: pwieck commentedBy mistake, in re-roll #34, I removed $language_url = language(Language::TYPE_URL);
Comment #38
pwieck CreditAttribution: pwieck commented#37 passed and ready for review
Comment #39
tlattimore CreditAttribution: tlattimore commentedHere's some cleanup that needs to be done.
Does this @todo still apply? Looks like #1938430: Don't add a default theme hook class in template_preprocess() is fixed.
I have never seen += used for concatenation PHP, nor do I believe it will work in. To add additinal keys to an array shouldn't we be doing something like
$items[] = array();
?This needs to updated per #1363112: Simplify names of "element-x" helper classes to mention 'visually-hidden' and not 'element-invisible'.
Comment #40
pwieck CreditAttribution: pwieck commentedFixing patch
Comment #41
pwieck CreditAttribution: pwieck commentedMade corrections stated in #39.
Did not change: '$item += array(' because this is proper per Cottser.
Comment #42
pwieck CreditAttribution: pwieck commented#41 Passed still needs profiling by someone.
Comment #43
joelpittetThis whole chunk can be removed.
Shouldn't say the type in a twig doc, array.
Just remove this line.
This should not say 'array' this should read a list.
Comment #44
pwieck CreditAttribution: pwieck commentedMade changes from #43
Comment #45
pwieck CreditAttribution: pwieck commented#44 passed still needs review and profiling.
Comment #46
siccababes CreditAttribution: siccababes commentedI added three articles to my site, and I found that they all had read more links. These links seem to be rendered through theme_links(). This patch seems to be working fine.
Comment #47
star-szrThanks for reviewing @siccababes :) this patch still needs to be profiled to see how it performs in comparison to the theme function.
Comment #48
jerdavisProfiling
Comment #49
jerdavisScenario
- Devel generate
- Generate 50 menu links in the Main Navigation menu. Allow up to 50 links on the menu with a depth of 1
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=51ec5ca916d7d&...
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=51ec5ca916d7d&...
Comment #50
jerdavisRemoving the needs profiling tag
Comment #51
jibran#44: 1939064-44.patch queued for re-testing.
Comment #53
joelpittetComment #54
joelpittetRe-rolled and removed extra whitespace modifiers and intented the contents of the spaceless control. Also removed the @see template_preprocess() in the docblock.
Comment #56
joelpittetHmm forgot that the modifiers are need inside the loop. Spaceless doesn't penetrate the for loop.
Comment #58
joelpittet#56: 1939064-56-twig-links.patch queued for re-testing.
Comment #59
joelpittetI re-ran the profiling similar but I turned on all permissions for anonymous and did 250 links on all menus using devel. So this should also grab a bit of menus that are generated in admin as well.
Without the spaceless things looks better on the # of function calls.
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=52353d82849e7&...
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=52353d82849e7&...
Comment #60
jibranWe can use
String::checkPlain
now.Comment #61
thedavidmeister CreditAttribution: thedavidmeister commentedSo, the function theme_links() no longer exists in this patch yet we have a bunch of documentation that still references it. I found 14 matches for 'theme_links()' with this patch applied.
Also, in reference to #60, as well as check_plain() being used, it's also referenced in the documentation of links.html.twig
Comment #62
joelpittetThanks @jibran and @thedavidmeister! Good catches!
Comment #64
jibran#62: 1939064-62-twig-links.patch queued for re-testing.
Comment #66
joelpittet#62: 1939064-62-twig-links.patch queued for re-testing.
Comment #67
farrington CreditAttribution: farrington commentedIn the preprocess the heading.level is not assigned with any value, and on the other hand in the old theme_links() it only sett statically to 'h2'.
So there seems be no reason to keep any of that in the preprocess nor in the twig file.
The documentation is also edited to reflect these changes.
Comment #69
farrington CreditAttribution: farrington commentedA small error got in to the previous patch file. (The twig file was missing)
Here's the correct file.
Comment #71
farrington CreditAttribution: farrington commentedUse the correct (?) method of adding new files "git diff"...
Comment #72
BarisW CreditAttribution: BarisW commentedRelated: #1777332: Replace theme_menu_link() and menu-tree.html.twig with a single Twig template
Comment #74
joelpittet@farrington could you please provide an interdiff for the changes between #62 - #69 and #69to #71?
That way we can easily what you changed between the patches.
Thank you for working on this!
Comment #75
BarisW CreditAttribution: BarisW commentedHere are the interdiffs.
Comment #76
drupalninja99 CreditAttribution: drupalninja99 commentedI had to do a reroll of the patch as it would no longer apply.
Comment #78
joelpittetI really like that the template now has the h2 in it instead of a variable for it's level. Thanks @farrington:) and thank you @drupalninja99 for the re-roll. I'm going to see if I can't get rid of those errors, now which are very likely related to the removal of the level variable. This may constitute as an API change... but let's hope not because it shouldn't be so.
Comment #79
joelpittetOn second thought, I'd really like to keep this issue as a straight conversion issue, get it into core first. I've created a follow up issue once this is complete and in core.
#2116359: Leave the heading level in the template and out of the variables for links.html.twig
Meantime this is the re-rolled and reverted those changes.
Comment #79.0
joelpittetRemove sandbox link
Comment #80
joelpittet79: 1939064-79-twig-links.patch queued for re-testing.
Comment #82
mark.labrecqueComment #83
mark.labrecqueRerolled the latest patch
Comment #85
mark.labrecqueComment #86
mark.labrecqueComment #88
joelpittet86: 1939064-85-convert-theme-links.patch queued for re-testing.
Comment #90
joelpittetlooks like menu_contextual_links snuck back into this patch. I doubt that has to do with the errors but compare yours to the one you are rerolling and you may spot the answer.
Comment #91
joelpittetSorry, @mark.labrecque this issue is a bit trickier than I originally thought for the re-roll. A bunch of router related stuff for links got in there and some of the code improvements were made similarly when those got in. I'll try to do a re-roll here from scratch.
Comment #92
joelpittetI am sure this will fail, but it seems to be because the active classes aren't working... again. That static reset for the homepage didn't seem to fix it so I removed that for now. I also removed
spaceless
for now and just added more modifiers but would rather have no whitespace modifiers it's quite a task to rewrite all the tests to be xpath or something that doesn't care about whitespace.It has to do with the fallback in _current_path in current_path... this is referencing #1269742: Make path lookup code into a pluggable class.
Let's see what the errors look like though from testbot...
Comment #93
joelpittetHere's the failed output for reference:
Result:
'<h2>Links heading</h2><ul id="somelinks"><li class="a-link odd first"><a href="/a/link">A <link></a></li><li class="plain-text even">Plain "text"</li><li class="front-page odd"><a href="/">Front page</a></li><li class="router-test even last"><a href="/router_test/test1">Test route</a></li></ul>'
Expected:
'<h2>Links heading</h2><ul id="somelinks"><li class="a-link odd first"><a href="/a/link">A <link></a></li><li class="plain-text even">Plain "text"</li><li class="front-page odd active"><a href="/" class="active">Front page</a></li><li class="router-test even last"><a href="/router_test/test1">Test route</a></li></ul>'
Comment #95
joelpittetThe good news: there are only 4 fails and they are the ones I knew would fail.
The bad news: I've no idea how to make the drupal_get_front_page() know it's active inside the test.
Help please!
Comment #96
joelpittet@larowlan++ helped me out here. Turns out the l() was not being active because it was getting the query params from the Request service. So in the test I disabled them.
Comment #97
star-szrTagging for reroll.
Comment #98
RainbowArrayReroll.
Comment #100
InternetDevels CreditAttribution: InternetDevels commentedComment #101
joelpittetI've got some better performance this time, likely due to the twig engine updates to 1.15.
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=52e6fc5cc809e&...
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=52e6fc5cc809e&...
Also did another markup review and there are no differences there too.
Passes testbot, this one is ready to go.
Comment #102
star-szrThis line is over 80 characters now so should be wrapped.
This should probably say "an Attribute object".
Rolling in those changes since they are so minor. For #1 I just reworded the comment.
Comment #103
joelpittetStill RTBC:) Thanks @Cottser
Comment #104
alexpott1939064-102.patch no longer applies.
Comment #105
alexpottComment #106
joelpittetThat was a tricky re-roll for such a simple change. Just dropped the hunk:
Back to RTBC.
Comment #107
joelpittetOh yeah here's the patch.
Comment #108
xjmI think this probably needs some change notice action, or maybe an update to an existing change notice for theme_*() removals? Can someone list change records that might need an update here? Otherwise, if we should draft a new one instead, we can do that now!
https://groups.drupal.org/node/402688
Comment #109
star-szrTalked to @xjm briefly in IRC about this.
This is just a conversion so probably not an API change (calling code still specifies
'#theme' => 'links'
in a render array), but we'll want to update the change records listed at the following link (ones that referencetheme_links()
) after commit:https://drupal.org/list-changes/published/drupal?keywords_description=th...
Comment #110
joelpittetI don't think we did that because we just remove the theme function and swap with a template file. Maybe should just haven't done it yet for any of them in #1757550: [Meta] Convert core theme functions to Twig templates
Comment #111
alexpottAfter applying this patch there are still 4 mentions of theme_links() in the code. As this function is being removed that's surprising.
Comment #112
joelpittetThanks @alexpott those must have snuck in there from the looks of them when some of the url routing changes and theme_links type=>link consolidation was going on.
Comment #113
star-szr+1, looks good. Thanks @joelpittet and thanks for catching that @alexpott!
I re-ran the grep from #111 and just got some .patch files I had kicking around from this issue :)
Comment #116
joelpittet112: 1939064-twig-links-112.patch queued for re-testing.
Comment #117
joelpittetRandom testbot fails.
Comment #119
joelpittetTestbot you can do it!
Comment #120
joelpittet112: 1939064-twig-links-112.patch queued for re-testing.
Comment #121
star-szrComment #122
star-szrComment #123
webchickCommitted and pushed to 8.x. Thanks!