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 #1898062 by c4rl, jenlampton, shlapa, joelpittet, Cottser, duellj, swentel: Convert field module to Twig.
Task
Convert PHPTemplate templates to Twig templates
Remaining
- Patch needs review
- Profiling
Template path | Conversion status |
---|---|
core/modules/field/templates/field.tpl.php | converted |
To test this code:
- Add a few custom fields to the Article content type, at least one single value field and one multiple value field. Make sure that at least one field has a visible label.
- Create a new Article node and review the markup for the field edit widgets. These are generated by core/modules/field/templates/field-multiple-value-form.html.twig - "multiple cardinality" references fields that can take more than one value and display a table of form input elements.
- Save the Article node and review the markup of all ".field" elements. These are generated by core/modules/field/templates/field.html.twig.
Related
#1757550: [Meta] Convert core theme functions to Twig templates
#1986116: Improve performance by replacing very small and simple templates and theme function with "Markup Utility Functions"
Comment | File | Size | Author |
---|---|---|---|
#92 | 1898062-field-module-92.patch | 9.78 KB | tlattimore |
#92 | interdiff.txt | 1.21 KB | tlattimore |
#90 | config.tar_.gz | 21.45 KB | jerdavis |
#86 | 1898062-86.patch | 10.6 KB | star-szr |
#86 | interdiff.txt | 4 KB | star-szr |
Comments
Comment #1
c4rl CreditAttribution: c4rl commentedTagging
Comment #2
c4rl CreditAttribution: c4rl commentedAssigning
Comment #3
c4rl CreditAttribution: c4rl commentedComment #5
c4rl CreditAttribution: c4rl commentedI thought I could get away with getting rid of template_process_field() in the first patch, but alas. :)
Also implemented whitespace fix for RDFa test.
Comment #6
star-szrAt a glance, all I see is that these should end in parens per http://drupal.org/node/1354#see. Other than that nitpick this is looking good!
Comment #7
star-szrAfter reviewing this patch again, assigning so I can roll a new patch to fix a few things relating to http://drupal.org/node/1920746 and documentation (including #6). Will post a new patch and interdiff within a couple days.
Comment #8
star-szrComment #9
star-szrThis part might be a bit ugly, drupal_render() was in some cases hiding the fact that $add_more_button was not set at all. Improvements welcome!
Comment #11
duellj CreditAttribution: duellj commentedRe-roll with the following changes:
- Updated preprocess docs per #1913208: [policy] Standardize template preprocess function documentation
- Fixed failing tests with multiform widgets. The problem was that the weight field element was added to both the field element column and the weight column, so it needs to be removed from the field element column.
- I've also condensed some of the code referenced in #9. The additional $add_more_button variable is unnecessary. Hopefully this is what you had in mind, Cottser :).
Comment #12
star-szrGreat work @duellj, thanks! $variables['button'] looks much nicer now :)
I think this should have a comment to explain why it's being unset here.
Comment #13
star-szrAdded commit message based on git blame and looking at sandbox issues. Please update if I missed anyone.
Comment #14
star-szrComment #15
duellj CreditAttribution: duellj commentedAdded comment message per #12
Comment #17
star-szr#15: 1898062-15-twig-field.patch queued for re-testing.
Comment #18
duellj CreditAttribution: duellj commented#15: 1898062-15-twig-field.patch queued for re-testing.
Comment #20
duellj CreditAttribution: duellj commentedRerolled patch #15 against HEAD, since it no longer applies cleanly.
Comment #21
fregan CreditAttribution: fregan commentedComment #22
fregan CreditAttribution: fregan commentedComment #23
fregan CreditAttribution: fregan commentedApplied patch and reviewed. Output looks the same on comparison of tpl.php and html.twig files.
Comment #24
steveoliver CreditAttribution: steveoliver commentedAll I see are some minor theme() -> render and nitpicky docs issues:
Instead of "... field.html.twig", let's say "This hook is invoked while preprocessing field templates."
Prepare*s*.
Re: this @todo: Yeah, we can return a render array here.
Let's remove this last @see field.html.twig.
Change "will be added using field.tpl.php (or equivalent)." to "...will be added by field templates."
Comment #25
duellj CreditAttribution: duellj commentedAttached patch makes changes from #24.
How can we use a render array here, if the $required marker is used in the table header. Can table headers take render arrays now?
Comment #26
steveoliver CreditAttribution: steveoliver commentedm'hmm -- http://api.drupal.org/api/drupal/core!includes!theme.inc/function/_theme...
Comment #27
duellj CreditAttribution: duellj commentedVery cool, thanks @steveoliver. I've removed the call to drupal_render and converted the header to a render array.
Comment #29
duellj CreditAttribution: duellj commented#27: 1898062-27-twig-field.patch queued for re-testing.
Comment #31
jenlampton#27: 1898062-27-twig-field.patch queued for re-testing.
Comment #32
jenlamptonmore nits:
field.html.twig
- Twig docblocks should not mention variable type (string, array, etc)
- Variables in Twig docblock need to be correct
- Twig whitespace controllers
{{-
need to be removed.field-multiple-value-form.html.twig
- for loop syntax is incorrect
- we should put the if/else logic on separate lines than the markup, but if that messes up the markup we can use the spaceless tag:
template_preprocess_field
- available variables in docblock should only list the ones that were there before preprocess added them.
template_preprocess_field_multiple_value_form
- looks awesome! :)
Comment #33
jenlamptonnew patch!
Comment #35
duellj CreditAttribution: duellj commented#33: core-update_field_to_twig-1898062-33.patch queued for re-testing.
Comment #37
joelpittet#33: core-update_field_to_twig-1898062-33.patch queued for re-testing.
Comment #38
star-szrBoth @joelpittet and myself have been unable to reproduce these failing tests locally. I also tested with a table prefix like testbot does and the failing tests still passed. #20 passed and I don't see between #20 and #25 what would cause the
failuresexceptions.Comment #41
swentel CreditAttribution: swentel commentedTests indeed are passing when using the web browser, however not with run-tests.sh. I'm suspecting some kind of leaking somewhere, but I'm too tired to debug now, and if we want to fix that, that should happen in a separate issue anyway.
Patch attached fixes both tests as there's a call to
user_access
inuser_template_preprocess_default_variables_alter
and the role_permission table didn't exist in these Drupal Webunit tests.Comment #42
star-szrThanks @swentel. I did some git bisect and determined that when #1932382: Use DrupalUnitTestBase for Field API tests. was committed, this issue started seeing those failures (but only through run-tests.sh as you said). I'm not sure what this patch is doing to throw those exceptions, but thank you very much for stepping in with the fix :)
Comment #43
BerdirYes, seen this before. #1950684: Mock and protect $GLOBALS['user'] in DUTB tests should fix it in making it consistently fail.
The reason is that DUBT tests currently don't switch the global user object, so it is whatever you're running tests with. In the UI, very likely uid 1, with run-tests.sh it is uid 0. uid 1 means access checks are skipped and permissions for the given roles aren't loaded.
You're more than welcome to confirm that with the patch from that issue and with the interdiff from #41, it should fail consistently in the UI and CLI.
Comment #43.0
BerdirAdding commit message
Comment #43.1
swentel CreditAttribution: swentel commentedUpdated issue summary.
Comment #44
star-szrTagging.
Comment #45
thedavidmeister CreditAttribution: thedavidmeister commentedI'm going to test this today.
Comment #46
thedavidmeister CreditAttribution: thedavidmeister commentedCreating a new "Article" node and comparing the markup of the default fields and a few custom fields, the "class" attribute has a trailing space that it did not previously for .field-label, .field-items and .field-item.
Other than that, I've gone over a bunch of different combinations of fields and compared the markup. I haven't thoroughly reviewed the code in the patch yet but the markup looks good :)
I'll update the issue description with steps for testing if anyone else wants to look but I'm removing the testing tag for now.
Comment #46.0
thedavidmeister CreditAttribution: thedavidmeister commentedAdd conversion summary table
Comment #47
thedavidmeister CreditAttribution: thedavidmeister commentedAlso updating the issue summary to reflect that theme_field() has been removed, not converted.
Comment #47.0
thedavidmeister CreditAttribution: thedavidmeister commentedAdded instructions for manual testing.
Comment #48
thedavidmeister CreditAttribution: thedavidmeister commentedUnfortunately, while the markup looks fine (other than the extra space in class attributes) I think the patch needs more work :(
Why does template_process_field() still exist when theme_field() has been removed. If we keep it, at the least the documentation for it needs updating, if we remove it we have to merge the functionality into template_preprocess_field(). I vote we remove it as we have no need for two functions processing variables. This is what is there currently:
The extra space in the class attribute is caused by lines in the template that look like this:
<div class="field-label {{ title_attributes.class }}"{{ title_attributes }}>{{ label }}:</div>
When foo_attributes.class is empty we end up with extra whitespace. The "field-label", "field-items", "field-item" classes in field.html.twig all should be merged in as default classes in the preprocess function rather than hardcoded into the Twig template.
In field-multiple-value-form.html.twig:
* - button: "Add more" button.
Actually, it's an "Add another item" button.
In field-multiple-value-form.html.twig:
Is
<div>
not supposed to be indented when it's within an {% if %}?Comment #49
thedavidmeister CreditAttribution: thedavidmeister commentedComment #50
thedavidmeister CreditAttribution: thedavidmeister commentedI'm done testing this for now.
Comment #51
star-szrRolling a new patch based on #48. I'm afraid of what will happen with RDF when we remove the process function but we'll see what testbot will say :)
Comment #52
duellj CreditAttribution: duellj commentedUnfortunately, bartik_field__taxonomy_term_reference() needs to be converted here too. It can't be done in #1938840: bartik.theme - Convert PHPTemplate templates to Twig without the change to field_theme().
Comment #53
star-szrThis (proof of concept) patch just merges template_preprocess_field() and template_process_field(), uploading so others can see. Fails some RDF tests locally, haven't run any others. Will set to CNR when I upload the patch based on the other feedback in #48.
Comment #54
star-szrI'm leaving the hardcoded classes for now, I could go either way on those.
Updated docs and indent level though, including indenting the contents of {% spaceless %}.
Comment #55
star-szrCNW to add the Bartik conversion here.
Comment #55.0
star-szrtheme_field() was removed not converted.
Comment #55.1
star-szrUpdate remaining
Comment #56
duellj CreditAttribution: duellj commentedConverted taxonomy field override in Bartik. One question: there's a difference in whitespace now that the template is in twig, as the old theme function added no newlines. Is this something we need to enforce here, since it looks a lot better with newlines and indentation?
Comment #57
star-szrAssigning to @steveoliver for another review.
Comment #57.0
star-szrAdd bartik function to conversion table
Comment #58
c4rl CreditAttribution: c4rl commentedPer #1757550-44: [Meta] Convert core theme functions to Twig templates, retitling to indicate this issue applies to templates rather than theme_ functions. See #1987398: field.module - Convert theme_ functions to Twig for theme_ function conversion.
Comment #58.0
c4rl CreditAttribution: c4rl commentedRemove sandbox link
Comment #58.1
c4rl CreditAttribution: c4rl commentedUpdated issue summary.
Comment #58.2
c4rl CreditAttribution: c4rl commentedUpdated issue summary.
Comment #58.3
c4rl CreditAttribution: c4rl commentedClarify issue summary
Comment #59
star-szrComment #60
star-szrHere is just the .tpl.php conversion.
Comment #60.0
star-szrUpdate conversion table
Comment #61
star-szrBack to you @steveoliver.
Comment #62
steveoliver CreditAttribution: steveoliver commented1. Let's remove these docs from preprocess and merge them with the docs that already exist for field.tpl.php/html.twig, *keeping* the "THIS FILE IS NOT USED..." notice.
2. Remove @todo.
3. I could be convinced otherwise (this /is/ an example template), but I think we want to just print {{ *attributes }}, and not call out .class on it's own.
4. 'field-label' and 'field-items' classes can be added in preprocess. Why don't we do that?
5. Are we sure "cycle(["even", "odd"], delta)" is right? I'd think it goes 'odd', 'even' ? (Needs manual testing) ;)
It's not in this patch, but
6. template_process_field() looks completely pointless. Actually it is. See #1843728: Remove template_process_field().
Comment #63
steveoliver CreditAttribution: steveoliver commentedAlso,
7. change @see template_process_field() to @see theme_field().
Comment #64
c4rl CreditAttribution: c4rl commentedI'll implement suggestions from #62
Comment #65
c4rl CreditAttribution: c4rl commentedFew improvements here:
Comment #67
joelpittet#65 seemed to fail because RDF module is expceting item_attributes... at least part of the bot failures.
Comment #68
star-szrThat item_attributes_list change seems a bit like an API change, how about doing that after conversion lands, especially if that's what's breaking tests?
Comment #69
c4rl CreditAttribution: c4rl commentedFine by me.
Comment #70
thedavidmeister CreditAttribution: thedavidmeister commentedI think I'm missing something really obvious, but why are we overriding 'title_attributes' and 'content_attributes'? don't we mean to concatenate our class onto them rather than replace the existing, passed in attributes?
Comment #71
star-szrAgreed, and since these are standard variables we don't need to instantiate attribute objects so it should be something like:
$variables['title_attributes']['class'][] = 'field-label';
$variables['content_attributes']['class'][] = 'field-items';
We can also lose the extra line before the function's closing brace.
Comment #72
c4rl CreditAttribution: c4rl commentedI'll fix these. Speaking of attributes, see my comment #1982018-1: [meta] Refactor template_preprocess() but that bikeshed can wait post-conversion :)
Comment #73
c4rl CreditAttribution: c4rl commentedImplemented changes from #71.
Comment #74
star-szrCan you take another look at this one @steveoliver?
Comment #75
steveoliver CreditAttribution: steveoliver commentedPasses manual testing and code review. Needs only profiling before RTBC.
Comment #76
steveoliver CreditAttribution: steveoliver commentedprofiling...
Comment #77
steveoliver CreditAttribution: steveoliver commentedAdded three fields to Article.
20 full nodes on home page, 8.x vs. #73:
baseline check:
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=5198f7eab7067&...
patch #73 comparison:
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=5198f7eab7067&...
/node/add/article, 8.x vs. #73:
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=519a4774889dd&...
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=519a4774889dd&...
The wall time fluctuation on node/add/article is discouraging -- what's up with that? I'm running a Core2 Duo with APC enabled...
Leaving tagged "Needs profiling" so someone else can take a look.
Comment #78
ezeedub CreditAttribution: ezeedub commentedComment #79
ezeedub CreditAttribution: ezeedub commentedScenario: 20 nodes with 3 fields, in a view. Cottser pointed out that the template is not used unless it's in your theme.. So, using stark, copied field.tpl.php to stark theme folder in 8.x branch, and field.html.twig to stark folder in feature branch, and commit.
Comparing 8.x (96b23549) to #73...
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=519aaadf29b61&...
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=519aaadf29b61&...
not sure if 7.8% is too much, so leaving needs profiling tag. I think the node/add/article test is not needed b/c it doesn't use the template?
Comment #80
ezeedub CreditAttribution: ezeedub commentedWent over the result in #79 with Cottser, and discovered a) looks like there were 250 fields, not 20 * 4 (I think i devel generated without deleting prev content), and b) I made fields with unlimited cardinality (thinking more is better!).
Here's a new test with 40 nodes with 5 fields (including body), all cardinality = 1. Same branches as #79.
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=519abb02c8da6&...
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=519abb02c8da6&...
Still bad at 6.5%, but this is possibly okay for this template?
Comment #81
thedavidmeister CreditAttribution: thedavidmeister commentedThat's pretty bad. Can anyone explain what's going on here?
Comment #82
rcaracaus CreditAttribution: rcaracaus commentedComment #83
star-szrThis is one of the only ones left for re-profiling, so the more data the better. Profile away!
Comment #84
star-szr…and @rcaracaus is having dinner with us so he is not profiling right now :)
Comment #85
star-szrRe-profiled with the scenario from #80:
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=519dd86a86681&...
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=519dd86a86681&...
Comment #86
star-szrThis conversion got a bit out of scope in trying to fix an existing core bug/lacking - the inability to add classes to the attributes arrays/objects in this template. There is a lot of extra Attribute object instantiation in preprocess that is actually not needed as well. So this reverts some of those changes for the sake of performance, and can we please address that bug in another issue.
Also removed some unneeded code from template_process_field() that is no longer needed now that #1982024: Lazy-load Attribute objects later in the rendering process only if needed is in.
Profiling results for attached patch, same scenario as above:
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=519de8fbb9479&...
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=519de8fbb9479&...
Comment #88
joelpittet@Cottser, the lazy loaded attributes only work for the templates not the theme_ functions, maybe it should also work for the theme_*() functions as well?
Comment #89
tlattimore CreditAttribution: tlattimore commentedAfter some discussion with @carl and @joelpittet I think we can get this to pass by moving the location that
Attributes
is instantiated intheme.inc
. I will work on a re-roll of #86 with this change.Comment #90
jerdavisRan another profiling pass following #80
Scenario
* Create 8.x branch, copy field.tpl.php to core/themes/stark/templates
* Create test branch, copy field.html.twig to core/themes/stark/templates
* Configure Article with 5 fields, including body (see attached config)
* Generate 40 nodes
* Configure frontpage view to show full node content of 40 nodes
* Place block with full node view in sidebar
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=519e62c02d767&...
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=519e62c02d767&...
Comment #91
star-szrAh right. Thanks everyone. To be clear, the profiling in #90 is showing 110 fields, not 240.
@tlattimore et al - I don't think removing those Attribute instantiations from template_process_field() saved us much anyway so we could just add them back.
Comment #92
tlattimore CreditAttribution: tlattimore commentedHere is change to the patch from #86 that adds back in some variables for
template_process_field()
that we need to prevent exceptions being thrown.Comment #93
tlattimore CreditAttribution: tlattimore commentedThe patch from #92 also needs profiling.
Comment #94
star-szrRe-profiled, exact same scenario.
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=519ea884c38e9&...
http://www.lionsad.de/xhprof-kit/xhprof/xhprof_html/?run1=519ea884c38e9&...
Comment #95
Fabianx CreditAttribution: Fabianx commentedLooks good, re-reviewed the patch, profiling data is solid, the field.html.twig is not used by default in core. Follow-up issue has been created.
=> RTBC
Comment #96
alexpottCommitted 624f9e6 and pushed to 8.x. Thanks!
Comment #97.0
(not verified) CreditAttribution: commentedUpdated issue summary.
Comment #97.1
YesCT CreditAttribution: YesCT commentedadd related as mentioned in issue summary of #1987398: field.module - Convert theme_ functions to Twig