Field rendering has 2 small blemishes.

  1. 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.
  2. 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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

moshe weitzman’s picture

FileSize
6.62 KB

Woops - with patch.

Note - there are more than 2 blemishes but those are the two I've looked at in this patch.

Status: Needs review » Needs work

The last submitted patch failed testing.

moshe weitzman’s picture

Status: Needs work » Needs review
FileSize
6.91 KB

Changed 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.

Status: Needs review » Needs work

The last submitted patch failed testing.

yched’s picture

No 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)

yched’s picture

Also: 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...

moshe weitzman’s picture

FileSize
7.56 KB

I 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?

Undefined index: #item	Notice	field.module	557	template_preprocess_field_item()	
Undefined index: #label	Notice	field.module	558	template_preprocess_field_item()	
Undefined index: #label_display	Notice	field.module	559	template_preprocess_field_item()
yched’s picture

Status: Needs work » Needs review
FileSize
8.28 KB

Rerolled 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...

Status: Needs review » Needs work

The last submitted patch failed testing.

yched’s picture

Status: Needs work » Needs review

The 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':

Label: Value 1
       Value 2
As opposed to 'label above':
Label: 
Value 1
Value 2

HTML for 'label' inline is the following, and thus bleeds in the 'display items' part:

<div class="field">
  <div class="field-items">
    <div class="field-item">
      <div class="field-label-inline-first">Label:&nbsp;</div>Value 1
    </div>
    <div class="field-item">
      <div class="field-label-inline" **hidden by CSS, just provides vertical alignment of values**>Label:&nbsp;</div>Value 2
    </div>
  </div>
</div>

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:

<div class="field">
  <div class="field-label">Label:&nbsp;</div>
  <div class="field-items">
    <div class="field-item">Value 1</div>
    <div class="field-item">Value 2</div>
  </div>
</div>

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.

moshe weitzman’s picture

alas, I have minimal css chops so i can't implement this fine proposal.

yched’s picture

Hm. I guess we should ask someone with CSS mojo :-)

Question is: is there a way to have this markup:

<div class="field">
  <div class="field-label">Some string:&nbsp;</div>
  <div class="field-items">
    <div class="field-item">Value 1</div>
    <div class="field-item">Value 2</div>
    <div class="field-item">Value 3</div>
  </div>
</div>

styled as:

Some string: Value 1
             Value 2
             Value 3

with vertical alignment of "Value x" whatever the length of "Some string"

webchick’s picture

Issue tags: +Needs themer review
yched’s picture

Issue tags: -Needs themer review

A quick try with firebug shows this seems to work :

<div class="field clear-block">
  <div style="float: left;" class="field-label">Some string:&nbsp;</div>
  <div style="float: left;" class="field-items">
    <div class="field-item">Value 1</div>
    <div class="field-item">Value 2</div>
    <div class="field-item">Value 3</div>
  </div>
</div>

Probably needs to be cross-browser tested, though. We still officially support IE6 ?

yched’s picture

Issue tags: +Needs themer review

crosspost, tags are welcome

bjaspan’s picture

My 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

with zero height, because it will not automatically expand to contain its floated contents (except on some versions of IE). This will not be what we want.

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. :-)

yched’s picture

Yes, 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.

yched’s picture

Tested 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 :-)

yched’s picture

Status: Needs review » Needs work
FileSize
17.23 KB

Work-in progress patch.

Simplified to the HTML in #14, still TODO: add the CSS, turn field-item.tpl.php into a theme function.

yched’s picture

Status: Needs work » Needs review
FileSize
9.08 KB

Er, without the commented out tests. The patch should also fix test failures for 'multiple' formatter.
CNR just to check that.

yched’s picture

I'll be mainly AFK for the next 3-4 days, so anyone is welcome to take this further meanwhile ;-)

yched’s picture

FileSize
11.01 KB

Added the CSS, turned field-item.tpl into a theme function.

yched’s picture

FileSize
11.86 KB

Forgot one documentation TODO

yched’s picture

FileSize
11.11 KB

And forgot to remove field-item.tpl.php...

moshe weitzman’s picture

Status: Needs review » Needs work

Looks like cruft -


+ * Theme preprocess function for field-item.tpl.php.
+ * TODO : itemS ?
+ */

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.

moshe weitzman’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
10.91 KB

Removed that crufty comment. This is now RTBC. Please wait for commit until bot says green.

yched’s picture

Let'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.

webchick’s picture

Status: Reviewed & tested by the community » Needs work

Bumping down to needs work.

yched’s picture

Status: Needs work » Needs review
FileSize
9.39 KB

So, 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:

<?php if (!$field_empty) : ?>
<div class="field field-type-<?php print $field_type_css ?> field-<?php print $field_name_css ?>">
  <?php if ($label_display == 'above') : ?>
    <div class="field-label"><?php print t($label) ?>:&nbsp;</div>
  <?php endif;?>
  <div class="field-items">
    <?php $count = 1;
    foreach ($items as $delta => $item) :
      if (!$item['empty']) : ?>
        <div class="field-item <?php print ($count % 2 ? 'odd' : 'even') ?>">
          <?php if ($label_display == 'inline') { ?>
            <div class="field-label-inline<?php print($delta ? '' : '-first')?>">
              <?php print t($label) ?>:&nbsp;</div>
          <?php } ?>
          <?php print $item['view'] ?>
        </div>
      <?php $count++;
      endif;
    endforeach;?>
  </div>
</div>
<?php endif; ?>

After:

<?php if ($items) : ?>
  <div class="field <?php print $classes; ?> clearfix">
    <?php if (!$label_hidden) : ?>
      <div class="field-label"><?php print $label ?>:&nbsp;</div>
    <?php endif; ?>
    <div class="field-items">
      <?php foreach ($items as $delta => $item) : ?>
        <div class="field-item <?php print $delta % 2 ? 'odd' : 'even'; ?>"><?php print render($item); ?></div>
      <?php endforeach; ?>
    </div>
  </div>
<?php endif; ?>

- 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.

moshe weitzman’s picture

Status: Needs review » Reviewed & tested by the community

Yeah, 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.

yched’s picture

FileSize
9.55 KB

Done additional docs cleanup, leaving RTBC.

Status: Reviewed & tested by the community » Needs work

The last submitted patch failed testing.

yched’s picture

Status: Needs work » Needs review
FileSize
10.19 KB

Reroll 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.

Status: Needs review » Needs work

The last submitted patch failed testing.

moshe weitzman’s picture

I 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?

bcn’s picture

Status: Needs work » Needs review
FileSize
10.25 KB

re-roll...

Status: Needs review » Needs work

The last submitted patch failed testing.

moshe weitzman’s picture

thanks noahb. unfortunately, test bot found a couple fails and a boatload of exceptions.

yched’s picture

Status: Needs work » Needs review
FileSize
10.25 KB

Indeed, forgot one template variable in the reroll. That one should be better.

yched’s picture

FileSize
11.19 KB

Sneaking an additional cleanup : field.css now attached in #attached_css on forms and displayed fields (instead of in field_init(), yuck)

Status: Needs review » Needs work

The last submitted patch failed testing.

yched’s picture

Status: Needs work » Needs review
FileSize
10.25 KB

I 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.

Status: Needs review » Needs work

The last submitted patch failed testing.

yched’s picture

Status: Needs work » Needs review

Really 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 :-/

yched’s picture

Gah ! 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 :-/

moshe weitzman’s picture

@yched. i think that if you do more 'menu_rebuild_needed' inside of the crud functions you might find it lightens simpletest load.

yched’s picture

Status: Needs review » Needs work

moshe, there's no menu_rebuild() in field.module anymore : #164 / #171 in #516138: Move CCK HEAD in core.
Back to needs work, BTW.

yched’s picture

Status: Needs work » Needs review
FileSize
12.51 KB

This 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.

Status: Needs review » Needs work

The last submitted patch failed testing.

yched’s picture

Status: Needs work » Needs review
FileSize
12.51 KB

Stupid typo.

yched’s picture

Status: Needs review » Reviewed & tested by the community

Green at last. Back to RTBC.

Dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to CVS HEAD. Thanks.

Status: Fixed » Closed (fixed)
Issue tags: -Needs themer review, -Field in Core

Automatically closed -- issue fixed for 2 weeks with no activity.