Closed (won't fix)
Project:
Drupal core
Version:
8.0.x-dev
Component:
theme system
Priority:
Minor
Category:
Bug report
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
23 Mar 2011 at 21:48 UTC
Updated:
29 Jul 2014 at 19:27 UTC
Jump to comment: Most recent file
Comments
Comment #1
tim.plunkettWorst case scenario, just switch it to
$classes = 'field-item ' . ($delta % 2 ? 'even' : 'odd');Comment #2
Jeff Burnz commentedDone we just need to switch the classes around, this is what I'm doing in my theme_field overrides:
edit: lol, took me so long to get around to this tim got there first, nice one man.
Comment #3
fubhy commentedWhile swapping the classes is maybe the easier approach we should consider using the code snippet from theme_table as suggested in my initial post. Its probably also faster.
Comment #4
timb commentedBump, would love to see this implemented.
Comment #5
idflood commentedHere is a patch with the cool $flip thing.
Comment #6
jbrown commentedJeff's approach is much cleaner.
Is $delta guaranteed to be sequential?
Comment #7
fubhy commentedI have to disagree. I find that the $flip code is much cleaner and really slick. Afaik $delta should be guaranteed to be sequential.
Comment #8
tim.plunkettIt's already being used in
theme_table(), and it's much easier to follow than a modular arithmetic wrapped in a ternary if statement. As evidenced by this issue, it's easy to get wrong!Comment #9
idflood commentedAt least we should have the same method for theme_field and theme_table. I don't see any reason why these should be handled differently.
Another idea would be to add a little helper function like get_even_odd_class($index). This would be a DRY approach but it would add a little overhead. And the benefits from having a function aren't so big, so the patch proposed in #5 is the best approach I think.
Comment #10
damien tournoud commentedWorks for me. The sooner the better here, or themes will all implement it wrong :)
Comment #11
idflood commentedJust in case, here is the d7 backport patch ( exactly identical as the one for d8 in #5 )
Comment #12
jbrown commentedThis should really be in a separate API function. Saves developers from re-implementing it everywhere.
Having the inline ternary statement is a performance optimization.
Having to implement this $flip array every time we need an odd/even class is not a good idea.
Comment #13
fubhy commentedDo you have evidence that the modular arithmetic is faster?
Comment #14
damien tournoud commentedThis is a pattern. We don't need nor want a function here.
Comment #15
jbrown commented@fuhby my comment about performance was about calling a function versus in-lining. There would wouldn't be a measurable performance difference between using modular arithmetic versus the $flip array.
One of the goals of a pattern is that it is easy to replicate. Copying and pasting a tertiary operator is easy. Implementing $flip means you have to think every time. It is also more code.
In theme_table() it makes sense to use $flip as not every row has a zebra class.
The reason the existing tertiary pattern doesn't start with 'odd' is because the counter starts at 0, not 1. Themers want the first row (0) to be odd.
My patch replaces the 7 instances of the tertiary pattern with
I made a slight modification to theme_table() so it is clearer it will start with odd.
Do we document patterns anywhere?
Comment #16
idflood commented#15: Make sense to use a simple and common pattern. I will keep the $flip in my mind. The patch looks good but I wasn't able to apply it with 'git apply file.patch' so I added "a/" and "b/" and reformatted a little the patch while keeping the exact same content.
I've tested this patch against the current issue related to fields and it is working. Not sure about the other changes it introduce.
Comment #17
jbrown commented#16: zebra-1103340-16.patch queued for re-testing.
Comment #18
c4rl commentedThis isn't quite right. In template_preprocess() and template_preprocess_block(), the counter starts with 1, not 0.
If we start counting with 0, we need
if we start counting with 1, we need
Comment #19
c4rl commentedPatch takes #18 into account.
Comment #20
sunLooks good to me.
Not sure whether this can be backported. Potentially breaks custom themes.
Comment #21
dries commentedPatch looks good but needs a quick re-roll for 8.x. Mark RTBC after the re-roll.
Comment #22
tim.plunkett#999040-18: Coding standards: Alternative control structure syntax in theme templates was all that prevented it from applying cleanly.
Comment #23
fubhy commentedJust wondering why we have ($row % 2) now everywhere instead of a plain $row % 2 (without the additional brackets) as it is not needed here.
Comment #24
jbrown commentedI just put that in to make it clearer. Relying on the precedence of tertiary operators is very error prone.
Comment #25
tim.plunkettPHP ternaries are backwards (left associative). The more we can do to clear up ambiguities, the better
Comment #26
fubhy commentedThat is true, yes, but it still wouldn't be needed here. But I don't object to it and it probably gives us better readability. So lets get it commited.
Comment #27
catchThis feels like a lot of copy and paste
<?php print ($delta % 2) ? 'even' : 'odd'What about a helper for that?
Comment #28
catchAlso there's no CSS changes here, do we really have no table striping CSS in core?
Comment #29
tim.plunkettThere is already even and odd CSS styles, the point is just that the first row is picking up the even styles.
I don't know if a helper is really necessary, but that's just me.
Comment #30
catchIf we had a helper, we could've fixed this in one place though no instead of 5?
With the CSS, won't this be a visual change though? i.e. do we want to switch those around. If the answer to that is no that's fine with me, just asking ;)
Comment #31
jbrown commentedIt was suggested in #14 that it is better being a pattern. I think this is best. A function call would be as simple as the pattern, but with the pattern you can see what it does without having to go hunting. Also saves a function call.
The CSS should probably be switched also, otherwise tables will be starting with the wrong colour.
Comment #32
danquah commentedPatch #22 rerolled it for current 8.x HEAD ( 8deaba40a24436c3b2ecacdfe51c2f1a5a0f4f4e ).
Comment #33
therobyouknow commentedTried to apply patch in drupal 8 dev today and would not apply:
Comment #34
therobyouknow commentedComment #35
joelpittetCould this suggestion be all done in CSS?
Remove first/last/odd/even classes in favor of CSS3 pseudo selectors
See http://drupal.org/node/1192156
Comment #36
mbutsko commented#32: zebra-1103340-22-reroll-32.patch queued for re-testing.
Comment #37
mbutsko commentedThe patch in #32 applied for me today in 8.x HEAD { 522188692156731724b59bcfc06207b5b0d1e3ec }. Re-test.
Comment #38
fubhy commentedYes, this is being handled over here:
#1649780: Remove first/last/odd/even classes in favor of CSS3 pseudo selectors
We will never commit this to D7 because of this potentially destroying existing D7 theming. So let's just close this as won't fix and instead work on #1649780: Remove first/last/odd/even classes in favor of CSS3 pseudo selectors.