Field rendering has 2 small blemishes.
- rendering of items is done with a drupal_render_children() in template_preprocess_field(). Thats a bit jarring. I prefer to just let drupal_render() do its recursion instead of "taking over" at the preprocess layer.
- too much logic in field.tpl.php around iterating over items.
The attached patch introduces field-item.tpl.php which is a template that wraps themed field items (i.e. it is called via $theme_wrappers). So after an item gets themed, it gets wrapped by this template.
In order to get that going, I changed field.tpl.php to a $theme_wrappers as well. Now it basically adds markup around the all of the themed items, and nothing else.
This patch still needs a little work in order to document variables in the new template and perhaps remove a few more existing variables. Finally, I'd like similar template suggestions for field-item as we find in field.
Comment | File | Size | Author |
---|---|---|---|
#50 | fieldrender-545662-50.patch | 12.51 KB | yched |
#48 | fieldrender-545662-48.patch | 12.51 KB | yched |
#42 | fieldrender-545662-39.patch | 10.25 KB | yched |
#40 | fieldrender-545662-40.patch | 11.19 KB | yched |
#39 | fieldrender-545662-39.patch | 10.25 KB | yched |
Comments
Comment #1
moshe weitzman CreditAttribution: moshe weitzman commentedWoops - with patch.
Note - there are more than 2 blemishes but those are the two I've looked at in this patch.
Comment #3
moshe weitzman CreditAttribution: moshe weitzman commentedChanged back to $theme => 'field' instead of #theme_wrappers. Now using render($content) to emit the items. Hurrah for granular theming of field items.
Added docs to field-item.tpl.php
The field attach tests are consuming all my memory so i can't see what the exceptions are. would be great if pifr gave more detail about failures. can anyone get field attach tests to complete? if so, what are the exceptions mentioned above?
I am testing with basic body field. Need to make sure that labels print properly, and multiple fields print properly.
Comment #5
yched CreditAttribution: yched commentedNo time for a proper review right now, but more eyes on this part of the code are highly welcome, thanks moshe !
This being said, I'm a little worried that adding one template call per *value* is going to be fairly expensive. That's originally why we kept formatters as theme functions instead of templates (although back then we could have used the preprocessing for the 'sanitize' step)
Comment #6
yched CreditAttribution: yched commentedAlso: FieldAttachTestCase explodes on my local setup too. When I need to debug something in there I rename a couple tests to focus on the ones I want. Tedious :-(. For the current patch I'd suggest testFieldAttachViewAndPreprocess()...
+1 on "would be great if pifr gave more detail about failures". We should probably also split FieldAttachTestCase, too...
Comment #7
moshe weitzman CreditAttribution: moshe weitzman commentedI updated the new field_default_view() doxygen and got rid of one unneeded #delta property that I had added.
This is now tested on multiple fields and various label positions. Working well.
If benchmarks say so, I could convert field-item to a theme function instead of a template. I guess the easiest best way to benchmark this is on a page with 300 nodes. That introduces 300 fields and 300 items. @catch to the rescue?
The test exceptions are below. Not sure why they are happening though. I can't reproduce them in real fields, so I think it is test specific. Any idea, @yched?
Comment #8
yched CreditAttribution: yched commentedRerolled after #361839: Help needed for some field_attach function docs + added back field-item.tpl.php, missing from #7.
Not able to help on the tests tonight, my WAMP stack refuses to boot...
Comment #10
yched CreditAttribution: yched commentedThe cleanup in template_preprocess_field() is awesome. I'm not sure how 'multiple value' formatters work with the new code - maybe those are the test failures ? There's currently no example of a multiple formatter in core, aside from 'field_test_multiple' in field_test.module, used in testFieldAttachViewAndPreprocess().
I'm still worried about the additional template, and the fact that it possibly needs to support the same template suggestions than field.tpl.php (-fieldname.tpl.php, -bundle.tpl.php, -fieldname-bundle.tpl.php).
Then I thought : the ugliest part in field.tpl.php right now is the way we support 'label inline':
HTML for 'label' inline is the following, and thus bleeds in the 'display items' part:
There is probably a cleaner way to provide the vertical alignment of values at the right of the label, using floated divs or whatever. This would allow us to keep a common structure for the markup in both 'label display' modes, leaving the layout differences to CSS:
If so, field-item.tpl.php doesn't have to mess with label at all, just display the items alone. It becomes so simple it can just be a regular theme function. No additional template overhead.
I think this even opens the way to dead-simple 'multiple-values display modes' : as divs (like currently), as ul, ol, as comma separated - just override the theme function... w00t !
Breaks many D6 custom themes and CSS, probably - but very much worth it IMO.
Comment #11
moshe weitzman CreditAttribution: moshe weitzman commentedalas, I have minimal css chops so i can't implement this fine proposal.
Comment #12
yched CreditAttribution: yched commentedHm. I guess we should ask someone with CSS mojo :-)
Question is: is there a way to have this markup:
styled as:
with vertical alignment of "Value x" whatever the length of "Some string"
Comment #13
webchickComment #14
yched CreditAttribution: yched commentedA quick try with firebug shows this seems to work :
Probably needs to be cross-browser tested, though. We still officially support IE6 ?
Comment #15
yched CreditAttribution: yched commentedcrosspost, tags are welcome
Comment #16
bjaspan CreditAttribution: bjaspan commentedMy CSS is a bit rusty, but:
I do not see where css class clear-block is defined. Assuming clear-block does nothing (which I doubt), the markup above will leave the outer
Perhaps clear-block causes the outer div to expand to contain its floats, but if so I believe it must be doing it in some way (e.g. the css :after selector (I don't remember the details)) that I'm pretty sure does not work on IE and/or some other browsers.
In short, I can't be sure without actually trying it, and I need to go to sleep now. So this is a useless post. :-)
Comment #17
yched CreditAttribution: yched commentedYes, that class was clear-block in D6 and has been renamed clearfix in D7. It uses :after for modern browsers and (I think) inline-block for IE6, so I *think* it works. Could welcome confirmation, though.
Comment #18
yched CreditAttribution: yched commentedTested snippet #14 to work on IE6, IE8 (didn't try IE7 but I'm not really worried), Safari, Chrome, Opera (but I was not worried about those).
[edit: and Firefox, of course]
So it looks like the path is clear :-)
Comment #19
yched CreditAttribution: yched commentedWork-in progress patch.
Simplified to the HTML in #14, still TODO: add the CSS, turn field-item.tpl.php into a theme function.
Comment #20
yched CreditAttribution: yched commentedEr, without the commented out tests. The patch should also fix test failures for 'multiple' formatter.
CNR just to check that.
Comment #21
yched CreditAttribution: yched commentedI'll be mainly AFK for the next 3-4 days, so anyone is welcome to take this further meanwhile ;-)
Comment #22
yched CreditAttribution: yched commentedAdded the CSS, turned field-item.tpl into a theme function.
Comment #23
yched CreditAttribution: yched commentedForgot one documentation TODO
Comment #24
yched CreditAttribution: yched commentedAnd forgot to remove field-item.tpl.php...
Comment #25
moshe weitzman CreditAttribution: moshe weitzman commentedLooks like cruft -
Otherwise, looking nice ... I contemplated if we can use our new preprocess feature for theme functions but I don't see any big win for field-item. Perhaps yched does.
Comment #26
moshe weitzman CreditAttribution: moshe weitzman commentedRemoved that crufty comment. This is now RTBC. Please wait for commit until bot says green.
Comment #27
yched CreditAttribution: yched commentedLet's wait a little, I'm mulling over alternatives around this TODO.
The idea is :
- the current theme_field_item() (or template in earlier iterations) is not really interesting at this level. Theming the whole list of items would be much more powerful to allow 'output values as divs, ul, ol, comma-separated...', and would not add much complexity in the theme func (mainly a foreach).
theme_field_itemS() becomes the #theme of the field array, and field.tpl.php is the #theme_wrapper.
- I don't think we want to go through the hassle of formalizing the notion of 'multiple values display mode (div, ul, ol, char-separated... like we have for 'label display (inline, above...)' (which we keep for legacy reasons), and reflect that in the UI for display settings. This can be purely theme-override stuff.
After the simplification of the overall html in field.tpl.php, I'm wondering if this 'display items' thing deserves a separate theme call at all. Could all happen in field.tpl.php. You want values for field_foo as comma separated ? Easy, override field-field_foo.tpl.php and do it over there.
So, patch #26 works, I'll try to provide an alternative for the above later today.
Comment #28
webchickBumping down to needs work.
Comment #29
yched CreditAttribution: yched commentedSo, alternate proposal, as explained in #27.
Gets back to everything inside field.tpl.php. Not a complete roundtrip, in the process:
- the HTML got much simpler to override to do stuff like 'display the values of field_foo as a ul, ol or custom-char-separated list'.
Before:
After:
- template_preprocess_field() got terribly streamlined.
There could be additional cleanup/reorganizing in the field.tpl.php documentation, I'll do that if this approach is agreed.
Comment #30
moshe weitzman CreditAttribution: moshe weitzman commentedYeah, this is a great place to end up. It was indeed a litttle awkward to put items in a theme function thats separate from field.tpl.php.
This is RTBC. yched's doc polishing can always land in a follow-up patch in case driesChick beats him to it.
Comment #31
yched CreditAttribution: yched commentedDone additional docs cleanup, leaving RTBC.
Comment #33
yched CreditAttribution: yched commentedReroll after Translatable fields (after having to be rerolled so many times, TF patch gets his revenge by forcing every other Field patch to reroll...)
CNR for the bot, then RTBC.
Comment #35
moshe weitzman CreditAttribution: moshe weitzman commentedI tried to look into the breakage here but I am getting apache segfault on when i am adding/editing field and on node/add/article page. it could be my setup. anyway, we have a failure from testbot. @yched - can you please fix and reroll?
Comment #36
bcn CreditAttribution: bcn commentedre-roll...
Comment #38
moshe weitzman CreditAttribution: moshe weitzman commentedthanks noahb. unfortunately, test bot found a couple fails and a boatload of exceptions.
Comment #39
yched CreditAttribution: yched commentedIndeed, forgot one template variable in the reroll. That one should be better.
Comment #40
yched CreditAttribution: yched commentedSneaking an additional cleanup : field.css now attached in #attached_css on forms and displayed fields (instead of in field_init(), yuck)
Comment #42
yched CreditAttribution: yched commentedI don't get this. 'Multiple formatters' are screwed, and there's now way this is related to the #attached_css change in #40.
Actually, I also get the failure locally with #39, which came back green. Reposting #39 for a re-test.
Comment #44
yched CreditAttribution: yched commentedReally strange. I can't see anything in the recent commits that would cause #39 to raise failures while it came back green 2 hours ago.
Anyway. That's all for today :-/
Comment #45
yched CreditAttribution: yched commentedGah ! Testbot didn't run 'Field attach' tests on #39: http://testing.drupal.org/pifr/file/1/fieldrender-545662-39.patch
Anyone is able to submit a patch that badly breaks Field API and get it committed :-p.
Very possibly FieldAttachTestCase being too crowded and exploding some slaves... Should be split up, but not by me tonight :-/
Comment #46
moshe weitzman CreditAttribution: moshe weitzman commented@yched. i think that if you do more 'menu_rebuild_needed' inside of the crud functions you might find it lightens simpletest load.
Comment #47
yched CreditAttribution: yched commentedmoshe, there's no menu_rebuild() in field.module anymore : #164 / #171 in #516138: Move CCK HEAD in core.
Back to needs work, BTW.
Comment #48
yched CreditAttribution: yched commentedThis should unbreak 'multiple formatters'.
If the formatter is multiple (typical example is: dots on a computed png), the field template only sees one 'item', which will be rendered into the 'all values' output.
Comment #50
yched CreditAttribution: yched commentedStupid typo.
Comment #51
yched CreditAttribution: yched commentedGreen at last. Back to RTBC.
Comment #52
Dries CreditAttribution: Dries commentedCommitted to CVS HEAD. Thanks.