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 #2152213 by steveoliver, joelpittet, hussainweb, shanethehat, jenlampton, kpa, AnythonyR, EVIIILJ, kgoel, Cottser, dsdeiz, hanpersand: Convert theme_form_element() to Twig
Task
Convert theme_form_element() to a Twig template.
Remaining tasks
- Patch
- Patch review
- Manual testing
- Profiling
Steps to test
@todo
Comment | File | Size | Author |
---|---|---|---|
#21 | interdiff.txt | 1.1 KB | joelpittet |
#21 | 2152213-twig-theme_form_element-21.patch | 9.13 KB | joelpittet |
#14 | interdiff.txt | 10.59 KB | joelpittet |
#14 | 2152213-twig-theme_form_element-14.patch | 9.32 KB | joelpittet |
#4 | 2152213-twig-theme_form_element-4.patch | 13.09 KB | joelpittet |
Comments
Comment #1
star-szrAdding a commit message to the issue summary so the folks who already worked on #1898480: [meta] form.inc - Convert theme_ functions to Twig and in the Twig sandbox get credit.
Comment #2
joscartesar CreditAttribution: joscartesar commentedComment #3
joscartesar CreditAttribution: joscartesar commentedComment #4
joelpittetThis includes the form_element_label because a separate theme function for the label is resource wasteful and this theme function is the only one to call it with duplicate logic.
Comment #5
jessebeach CreditAttribution: jessebeach commentedThis conversion helps us get past sticking points in #2177653: Replace theme() with drupal_render() in form.inc, which is itself blocking #2006152: [meta] Don't call theme() directly anywhere outside drupal_render(), so I'd like to see it get in.
From a code standpoint, the changes are straightforward shifting from
theme()
topreprocess
for a template. Tests pass and some manual clicking around on forms surfaces no display regressions.Comment #6
joelpittetJust so we don't get this bumped down to needs work as quick;)
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=52e96dacba2bc&...
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=52e96dacba2bc&...
Comment #7
joelpittetHere's a better one, form pages profiling get a bit tricky because of PDOExecute statements get larger as key_value_expire table fills up.
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=52e97b490de91&...
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=52e97b490de91&...
Comment #8
sunHm. Is there a way to retain theme_form_element_label() has a separate theme function/template?
E.g., as a separate label.html.twig template, which is included from the form-element.html.twig template?
I'm working on some accessibility issues elsewhere, and I need the currently existing standalone label theme function for that.
Comment #9
joelpittet@sun there is, but I it seems wasteful resource wise to have tag based templates. The most common use case and the only use case in core is to have a label in a form_element. if you need a label.html.twig it would be simple enough to do in a contrib theme to make a tag based twig template. You can also do an extend easier this way with a template override, or include.
Point to the issue in question and we can help you out.
Comment #10
sunTo clarify, I'm working on D8 core, not on contrib.
My plan is to introduce a new #type label to resolve an accessibility problem with the tableselect facility of #type table, which was discovered in #1938926: Convert simpletest theme tables to table #type and thus currently happens in that issue (although I might be going to split that into a separate issue).
I'm able to achieve what I need to do with current HEAD, because I can use the existing theme_form_element_label() for #type label, which is just a label and does not need the wrapping form element output.
This patch here removes that theme function, which presents a problem/regression for the code I'm working on.
When I last discussed some Twig/templating improvements with @mortendk and others at DrupalCamp Vienna last November, we actively used the
include
,extend
, andblock
facilities of Twig within core templates already, which worked like a charm.IIRC, template variables are correctly forwarded already, so all you'd need to do is to add a new
label.html.twig
template file and include it from the form element template, like this:Doing so would allow us to continue to use the label theme function/template as a standalone element.
Comment #11
joelpittet@sun don't have to clarify, you are interCore famous:) ala interweb.
webchick found lots of contrib using that theme function too, so to keep things simple I'll split them back up. Maybe we can look at better ways to deal with those things in d9 land.
Comment #12
star-szrSo back to needs review at least. For the record core Twig templates don't use block, include, or extend. Not yet anyway.
Comment #13
joelpittet@jessebeach you're other patch helped a lot in this next patch, thank you! So far I've not had to deal with theme functions with 'render element' and now with the help of @tim.plunkett and that remove theme() calls patch I see why this didn't work as expected.
which is what the theme function pretends to expect... and instead it had to be:
Which is kinda what I'd prefer anyways to write... but wish the function didn't have that whole '#element' key deal... anybody know if I could just change them all to 'variables' instead of 'render element'?
Comment #14
joelpittetOk so this needs another review.
Comment #16
joelpittet14: 2152213-twig-theme_form_element-14.patch queued for re-testing.
Comment #18
joelpittet14: 2152213-twig-theme_form_element-14.patch queued for re-testing.
Comment #19
joelpittet@jessebeach would you like to re-RTBC? Or anybody for that matter?
Comment #20
sunThank you! Only one remark:
Given that the values are not altered in any way, and because I think the values should be NULL when not set, an easier + more accurate approach would be:
That takes over all of the properties exactly as-is from $element - but only if they are set.
Comment #21
joelpittetYeah that is nice and keeps the NULL as they were which is also good since we fixed that in twig:)
Thank you for that cleaner code snippet. Passes tests locally so here it is again.
Comment #22
sunGreat, thank you. :)
Comment #23
catchCommitted/pushed to 8.x, thanks!