Hi,
Just in case of someone needs that too…
I needed to color-code some fields collections, so I've added
$element[$delta]['#attributes']['class'][] = 'delta-' . (1+$delta);
on line 845 of field_collection.module (in field_collection_field_formatter_view()).
Now it outputs delta-1, delta-2, …
Comments
Comment #1
duckzland commentedIMO you shouldn't touch the module code for themeing issue.
This will give you delta without touching the module code (although if the module maintainer can include this in the next release will be great!)
Comment #2
jdanthinne commented@duckzland : the theming function doesn't work. It outputs deltas but the wrong ones (the collection id instead of a per-page delta).
So it would be great if my single line patch is included in the next release… still waiting.
Comment #3
michaelfillier commentedThis is definitely useful for themeing. I was displaying collection items with grid classes and I need to adjust the margin on the nth item. Adding the delta number makes this a snap. I would propose a less ambiguous class name though, I use:
Also, as mentioned, the override of field_collection_view does not work at the theme level. I believe it needs to be in a module. This would be preferred over hacking the module, until the module gets updated to include the delta class.
Comment #4
RobW commentedYou can also use function
YOURTHEME_preprocess_field(&$variables)in template.php to add delta classes to the field template.Comment #5
jmuzz commentedThis would be something for theming to handle, not the module that provides the field. Field modules generally don't put such things in the markup by default because the option is there and not everybody will need it. Here's a change to duckzland's example that should make it do what you had in mind.
Comment #6
iLLin commentedNot sure I completely agree with the module not implementing. There should also be zebra striping. Views does all this automatically, and by adding it to the module it doesn't hurt anything and gives the themer more classes to work with without having to use hooks. The question is why not just add it for all?
I opened this back up for more discussion, but I don't see any harm in adding something like this. Thoughts?
Comment #7
jmuzz commentedThe theme isn't just CSS, it supplies the HTML too, including extra classes such as zebra striping. In this way a theme can provide these classes for image fields, text fields, field collections, number fields, date fields, any field or all fields. If every module that provides a field provided it's own version of zebra striping it would be more difficult to maintain those modules and unless they all implemented it exactly the same way it would be more difficult for the themers to work with too.
Views is in a position of providing something relatively unique. Field collections is just one of many optional field modules. If you want to add something like this to it you should try to understand why the other field modules such as link fields don't already include it first.
Comment #8
duckzland commentednow days better use css3 nth-child(x) for zebra stripping.