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 #1963476 by widukind, tlattimore, joelpittet, kattekrab, carsonevans | Cottser: datetime.module - Convert theme_ functions to Twig.
Task
Use Twig instead of PHPTemplate
Remaining
- Code review
- Manual testing
Theme function name/template path | Conversion status |
---|---|
theme_datelist_form | converted |
theme_datetime_form | converted |
theme_datetime_wrapper | converted |
Testing steps
- Navigate to node/add/article, compare the markup for the 'Authored on' field in the 'Authoring information' section in the right sidebar. The wrapper should be rendered by datetime-wrapper.html.twig and the datetime element by datetime-form.html.twig.
- Add a date field to the article content type (select list widget) and navigate to node/add/article. Compare the markup for the date field you added. The wrapper should be rendered by datetime-wrapper.html.twig and the datelist element by datelist-form.html.twig.
Related
#1757550: [Meta] Convert core theme functions to Twig templates
Comment | File | Size | Author |
---|---|---|---|
#76 | twig-datetime_theme_render-1963476-76.patch | 6.62 KB | jenlampton |
#76 | interdiff.txt | 2.78 KB | jenlampton |
#71 | 1963476-71-twig-datetime-theme.patch | 7.41 KB | joelpittet |
#71 | interdiff.txt | 945 bytes | joelpittet |
#67 | interdiff.txt | 703 bytes | joelpittet |
Comments
Comment #1
tlattimore CreditAttribution: tlattimore commentedI'll take a stab at this.
Comment #2
star-szrAwesome, thanks @tlattimore. If you get stuck come on by #drupal-twig on IRC :)
Comment #3
star-szrTagging.
Comment #4
star-szrAny news @tlattimore, or can you share a partial patch?
Comment #5
tlattimore CreditAttribution: tlattimore commentedI have a working patch. But still need to do some cleanup. Hope to post it later today.
Comment #6
tlattimore CreditAttribution: tlattimore commentedIt looks like I may not be able to get this done so I am going to un-assign myself.
Comment #7
tlattimore CreditAttribution: tlattimore commentedHere is my 1st at converting datetime module to twig.
Comment #8
tlattimore CreditAttribution: tlattimore commentedThis patch removes brackets around
title
in datetime-wrapper. Still wrestling with php habits..Comment #9
star-szrThank you very much @tlattimore! I will review this tonight unless someone else gets to it before me.
Comment #10
widukind CreditAttribution: widukind commentedComment #11
widukind CreditAttribution: widukind commentedThis holds up, good work @tlattimore.
A few minor tweaks may still be beneficial:
1.
- convert this call to
theme()
into a renderable.- call
t()
in the template.2. wrap content of twig templates into
{% spaceless %}
tags.working on it...
Comment #12
star-szrIndeed, at a glance this looks very good!
Minor doc nitpick - this wraps early and is not indented.
@widukind - I don't see any reason why {% spaceless %} is needed here, unless I'm missing something :)
Comment #13
widukind CreditAttribution: widukind commented@Cottser,
{% spaceless %}
is not needed - just thought I throw it in there for a more compact output. On second thought, the extra function call is probably not worth it. So thanks for questioning this.Anyway, here's another patch addressing #11 (sans whitespace removal) and #12.
On a related note, I was not too stoked on the twig syntax for passing an assoc. array as function argument after I moved the translation call into the template
IMO, this would be much easier to read in PHP code.
Is there a guideline on making these translation calls, template vs theme_preprocess function?
So far, I've been going by what I've seen, which is calling
t()
from within the template.Thanks!
Comment #14
star-szr@widukind - I agree it's not the prettiest syntax, normally you could use {% set %} to create the array first then pass it to the t filter; unfortunately we currently have a bug around that, see #1916834: Twig implementation: passing array to set tag creates a TwigReference object.
As for calling t() in the template vs. preprocess, that is explained in http://drupal.org/node/1920746#utility and we've decided template is the way to go.
Changes are looking good at a glance, this needs a slight tweak:
$required should be initialized as an array. Previously $required was always a string but we are converting to a render array so the variable should always be an array so we're not mixing datatypes.
Comment #15
widukind CreditAttribution: widukind commented@Cottser, thanks for the quick answer.
Comment #16
widukind CreditAttribution: widukind commentedComment #17
star-szrThanks for keeping this one moving! :) I had a chance to look over the latest patch as a whole, here's some more feedback.
I hadn't looked at what was being removed in the preprocess here, namely what the previous t() string was. This is a case where we currently can't do render arrays in the preprocess.
We don't want to change what we are passing to t(), so we should still be passing
!title!required
to t(). I think we can just use a theme() call in preprocess to create the string for 'required' to be added to the t filter already in the template.Like this:
This is indented correctly now, but wraps early. Comments should be as long as possible within the 80 character limit per http://drupal.org/node/1354#drupal.
Instead of talking about 'renderable' and 'empty array' here (see http://drupal.org/node/1823416#datatypes) I suggest something like this (especially because as in #1 I think it will need to be a string for now):
- required: (optional) A required marker indicating that the form element is required.
Comment #18
star-szr(double post, carry on)
Comment #19
star-szrAnd one more tiny thing…
"Prepares variables *for*…" per #1913208: [policy] Standardize template preprocess function documentation.
Comment #20
widukind CreditAttribution: widukind commentedPlease see attached patch and interdiff for the changes requested in #17-19.
I also attached an alternative implementation that restores the original translation value but also removes the need to theme the marker within the preprocess function (see -alt files).
Comment #21
widukind CreditAttribution: widukind commentedtwo more minor tweaks:
1. labeled "required" template variable as optional (as requested in comment #17)
2. in the alt. implementation, i moved the temp. variable assignment/required-rendering inside the if statement.
Comment #21.0
widukind CreditAttribution: widukind commentedAdd conversion summary table
Comment #22
star-szrThanks @widukind, and sorry for the delay in getting back to this. I'm not sure in this case that we're gaining much from the alternative implementation, I vote for now that we either keep the theme() call in the preprocess or convert to a render array and then drupal_render() it. I think the theme function should be fine for now though.
I'll come back for a proper review, but in the meantime there's a couple small coding standards things that can be fixed up:
We should remove these @ingroup themeable from the preprocess functions per #1913208: [policy] Standardize template preprocess function documentation.
This comment should be wrapped at 80 characters per http://drupal.org/node/1354#drupal.
Comment #22.0
star-szrUpdate conversion table
Comment #23
widukind CreditAttribution: widukind commentedComment #24
jwilson3I'm not entirely sure the documentation was originally correct, because
theme_datetime_form
mentioned "wrapper" in its docblock, whereastheme_datetime_wrapper()
ironically did not. But maybe it makes sense to not lose this comment line talking about "Wrapper around date element" which would help to distinguish and clarify the difference between the two preprocess comments, which in the current patch only differ by the presence of a '#' in front of the word datetime, which isn't very clear at all.Comment #25
jwilson3Comment #26
widukind CreditAttribution: widukind commentedComment #27
widukind CreditAttribution: widukind commentedComment #28
widukind CreditAttribution: widukind commentedAlright. I salvaged the previously removed "wrapper" code comment and applied it to new comments. Hopefully this will provide more clarity on what these functions all are about.
I also changed any reference to the "#datetime" form element into "datetime" in the code comments throughout the script for consistency reasons.
Please review again, thanks!
Comment #28.0
widukind CreditAttribution: widukind commentedUpdate remaining
Comment #29
star-szrThanks @widukind!
I compared markup on node/add/article and it looks like we have an extra wrapper div -
<div class="datetime-wrapper">
. It doesn't look like theme_datetime_wrapper() actually adds a div, it just prefixes an<h4>
if there is a title to be printed. We'll need to update docs to match this as well (i.e. remove 'attributes' from datetime-wrapper.html.twig, and possibly other updates?).Manual testing steps added to the summary, and here is a docs review:
There needs to be a blank line after the summary line per http://drupal.org/node/1354#functions.
What about something like 'Prepares variables for datetime form wrapper templates.'? Less wordy and ends in 'templates'.
"for a datetime…" or maybe "of a datetime…"?
Too many 'required' I think :) I suggest removing the first 'required' in the variable description, "A marker indicating…"
There needs to be a blank line after the summary line per http://drupal.org/node/1354#functions.
What about just 'Prepares variables for datetime wrapper templates.'?
Comment #30
widukind CreditAttribution: widukind commentedThanks @Cottser.
Improved the code comments as outlined above.
Comment #31
widukind CreditAttribution: widukind commentedComment #32
c4rl CreditAttribution: c4rl commentedPer #1757550-44: [Meta] Convert core theme functions to Twig templates, retitling to indicate this issue applies to theme_ functions, which are lower in priority than PHPTemplate conversion issues.
Comment #33
intergalactic overlords CreditAttribution: intergalactic overlords commentedOn node/add/article, the div-wrapper is still there. It doesn't have a class anymore though.
Comment #34
joelpittetRemoved drupal_render_children as per #1920886 and new Attribute() as per #1982024
Comment #35
StryKaizerPatch #34 still adds a div-wrapper on node/add/article, without a class
Comment #36
intergalactic overlords CreditAttribution: intergalactic overlords commentedIndeed, div-wrapper is still there.
Comment #37
widukind CreditAttribution: widukind commentedComment #38
widukind CreditAttribution: widukind commentedremoved the extraneous div.
Comment #39
widukind CreditAttribution: widukind commentedminor tweak to the patch in comment #38 - removed unwanted blank line. otherwise this is identical.
Comment #40
intergalactic overlords CreditAttribution: intergalactic overlords commentedManually checked, the html output is good. Ready for profiling.
Comment #41
StryKaizerpatch in #39 passed my manual testing and daisydiff. Testscenario used as described in first post.
Comment #42
StryKaizerDid not profile, sorry, my mistake ;)
Comment #43
widukind CreditAttribution: widukind commentedComment #44
widukind CreditAttribution: widukind commentedattempting to profile...
Comment #45
epersonae2 CreditAttribution: epersonae2 commented#39: 1963476-39.patch queued for re-testing.
Comment #46
kattekrab CreditAttribution: kattekrab commentedManually testing this one now.
Comment #47
widukind CreditAttribution: widukind commentedi give up on profiling, can't seem to get any consistency in the baseline from xhprof at all.
someone else with a more stable environment please try, thanks.
Comment #48
kattekrab CreditAttribution: kattekrab commentedHTML output matches.
diff attached.
Comment #49
kattekrab CreditAttribution: kattekrab commentedsave clash... unassigning widukind again.
Still needs profiling. Not something I'm going to try and attempt on a crusty little netbook.
Comment #50
carsonevans CreditAttribution: carsonevans commentedI will attempt to profile now
Comment #51
carsonevans CreditAttribution: carsonevans commentedbbranches 519ff5087988b 1963476-39.patch
Warning: Must specify directory location for XHProf runs. Trying /tmp as default. You can either pass the directory location as an argument to the constructor for XHProfRuns_Default() or set xhprof.output_dir ini param.
=== 8.x..8.x compared (519ff5087988b..519ff54646296):
ct : 40,386|40,386|0|0.0%
wt : 447,276|447,067|-209|-0.0%
cpu : 432,313|432,833|520|0.1%
mu : 40,759,592|40,759,592|0|0.0%
pmu : 40,892,112|40,892,112|0|0.0%
Profiler output
Warning: Must specify directory location for XHProf runs. Trying /tmp as default. You can either pass the directory location as an argument to the constructor for XHProfRuns_Default() or set xhprof.output_dir ini param.
=== 8.x..1963476-39.patch compared (519ff5087988b..519ff588c96fd):
ct : 40,386|40,386|0|0.0%
wt : 447,276|448,273|997|0.2%
cpu : 432,313|431,743|-570|-0.1%
mu : 40,759,592|40,758,456|-1,136|-0.0%
pmu : 40,892,112|40,890,624|-1,488|-0.0%
Profiler output
---
ct = function calls, wt = wall time, cpu = cpu time used, mu = memory usage, pmu = peak memory usage
Comment #53
carsonevans CreditAttribution: carsonevans commented=== 8.x..8.x compared (51a0079b7e32a..51a008005daf0):
ct : 76,176|76,176|0|0.0%
wt : 659,364|661,572|2,208|0.3%
cpu : 632,898|633,364|466|0.1%
mu : 41,026,312|41,026,312|0|0.0%
pmu : 41,187,832|41,187,832|0|0.0%
Profiler output
=== 8.x..1963476-39.patch compared (51a0079b7e32a..51a0086001e4c):
ct : 76,176|76,491|315|0.4%
wt : 659,364|664,510|5,146|0.8%
cpu : 632,898|636,615|3,717|0.6%
mu : 41,026,312|41,139,800|113,488|0.3%
pmu : 41,187,832|41,301,000|113,168|0.3%
Profiler output
---
ct = function calls, wt = wall time, cpu = cpu time used, mu = memory usage, pmu = peak memory usage
Comment #53.0
carsonevans CreditAttribution: carsonevans commentedAdd testing steps to summary
Comment #54
gnugetSeems to the correct patch is #39 so, i'm going to mark this back to "needs review"
Comment #54.0
gnugetUpdated issue summary.
Comment #55
siccababes CreditAttribution: siccababes commentedThe patch needed a re-roll. I took a stab at it. Hopefully this will do the trick.
Comment #57
siccababes CreditAttribution: siccababes commentedComment #58
pwieck CreditAttribution: pwieck commented- Reroll conflict - Somethings missing here, this doesn't look right.
In file core/modules/datetime/datetime.module
Comment #59
adamcowboy CreditAttribution: adamcowboy commentedRe-rolling again.
Comment #61
pwieck CreditAttribution: pwieck commentedPlease see #58 on re-roll issue
Comment #62
star-szrThanks for the rerolls folks :) removing needs reroll tag for now.
I looked at this with @pwieck today in IRC.
@siccababes - the patch uploaded was the same as the one in #39 - wrong file maybe?
@adamcowboy - The reroll in #59 is pretty close! The main issue is that the three Twig templates are missing (apply the before reroll patch with --index to prevent that as shown in the reroll instructions).
Some tabs snuck in so those will need to be removed per http://drupal.org/coding-standards#indenting.
Conflict pointed out in #58:
We should be keeping this (determined via git blame that it was added via #2006484-25: Remove dependency on datetime from node). We can convert it to a 'description' variable and then add it to the Twig template. The markup
<div class="description"></div>
can be moved to the Twig template. We'll probably want to add another {% if %} to the datetime-Twig template similar to how the title is handled.We may be able to simplify the handling of the required marker in template_preprocess_datetime_wrapper() but let's get tests passing first :)
So if someone wants to clean up the reroll in #59 or a do a fresh one that would be awesome!
Comment #63
jenlamptonon it
Comment #64
jenlamptonrerolled, added description variable, removed @see template_preprocess. Needs another review :)
Comment #65
jenlamptonno no, that's not even the right patch...
Comment #67
joelpittetThe required variable needs to be defined, so I set it to NULL.
Comment #69
joelpittet#67: 1963476-67-twig-datetime-theme.patch queued for re-testing.
Comment #71
joelpittetSeems disabled attribute is getting passed to the wrapper attributes, so I had to add back in the wrapper attributes array init.
Comment #72
azinoman CreditAttribution: azinoman commentedComment #74
sbudker1 CreditAttribution: sbudker1 commentedIt looks like datetime-wrapper.html.twig and datelist-form.html.twig. are both being used in both the "Authoring Information" and the "Date field" while datetime-form.html.twig. does not seem to be used in either. What if we removed the datetime-form.html.twig.? The datetime-form.html.twig. is also identical to the datelist-form.html.twig.
Comment #75
sbudker1 CreditAttribution: sbudker1 commented#71: 1963476-71-twig-datetime-theme.patch queued for re-testing.
Comment #76
jenlamptonGood idea, but I'd prefer to remove datelist-form just for naming reasons. Taking a stab at that now.
Comment #77
jenlamptonoh yeah, status change.
Comment #78
sbudker1 CreditAttribution: sbudker1 commenteddibs!
Comment #79
jenlamptonreviewing, I assume ;)
Comment #80
sbudker1 CreditAttribution: sbudker1 commentedPatch worked! It succeeded testing with the steps below and overall no issues or problems were evident.
Testing steps
-Navigate to node/add/article, compare the markup for the 'Authored on' field in the 'Authoring information' section in the right sidebar. The wrapper should be rendered by datetime-wrapper.html.twig and the datetime element by datetime-form.html.twig.
-Add a date field to the article content type (select list widget) and navigate to node/add/article. Compare the markup for the date field you added. The wrapper should be rendered by datetime-wrapper.html.twig and the datelist element by datelist-form.html.twig.
Comment #81
alexpottCan we get a new profile as this patch has changed since it was done...
Comment #82
jenlamptonmarking needs review, since it will need a profiling review too after that gets done, but the patch doesn't currently need work.
Comment #83
joelpittetScenario
Theme: stark
permissions: anonymous can "Administer content" and "Bypass content access control "
url: /node/add/article
Added a date field to the article type.
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=51e30f7568a45&...
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=51e30f7568a45&...
Comment #84
kattekrab CreditAttribution: kattekrab commentedThanks @joelpittet for running the profile!
So - is that within acceptable limits?
Comment #85
joelpittet@kattekrab I'd say yes, it's 0.3% (1.6ms) difference on the Wall Time between the two runs.
And it removes a theme_ function!
Comment #86
alexpottCommitted 15d3012 and pushed to 8.x. Thanks!
Comment #87.0
(not verified) CreditAttribution: commentedCorrect case in commit message