The logic to select and alter the $items array seems waaaaay too complex for what it's actually doing. Why not use some standard PHP functions to do this? This also has the benefit that it doesn't 'alter' the delta values for the items.
So if I had this array of field values:
$items = array(
0 => array('value' => 'first'),
1 => array('value' => 'second'),
2 => array('value' => 'third'),
3 => array('value' => 'last'),
);
$conf['delta_reversed'] = TRUE;
$conf['delta_limit'] = 3;
$conf['delta_offset'] = 0;
Currently this would result in:
$items = array(
0 => array('value' => 'last'),
1 => array('value' => 'third'),
2 => array('value' => 'second'),
);
When the desired (unmodified delta values) result is:
$items = array(
3 => array('value' => 'last'),
2 => array('value' => 'third'),
1 => array('value' => 'second'),
);
The field_view_field() function still rendered the items in their actual order, so in this case 'last' gets rendered first, so on and so on.
| Comment | File | Size | Author |
|---|---|---|---|
| #16 | 2336985-ctools-empty-field-collections-16.patch | 605 bytes | mh86 |
| #8 | ctools-do_not_change_field_item_deltas-2336985-8.patch | 696 bytes | peacog |
| #2 | 2336985-do-not-change-field-item-deltas.patch | 1.42 KB | dave reid |
| #1 | 2336985-do-not-change-field-item-deltas.patch | 1.36 KB | dave reid |
Comments
Comment #1
dave reidComment #2
dave reidFixes an issue with using '0' for the $length parameter of array_slice(), and uses NULL instead which according to the docs works on PHP 5.2.4 and up, which is the minimum version required for Drupal 7.
Comment #3
joelpittetThis is a nice clean up and I've been using it in production for a number of months. Thanks @Dave Reid
Comment #4
mrjmd commentedComment #5
japerryWorks good for me. Committed!
Comment #8
peacog commentedThis change breaks rendering of multi-value fields in cases where offset is not 0, for example, if you want to skip the 1st delta and show the 2nd. Allowing array_slice to reset the array indices by removing the preserve_keys TRUE parameter fixes the problem. Here's a patch that does just that.
Comment #9
dave reid@Peacog: Resetting the deltas is exactly what I want to avoid though. Is this reproducible with a minimum amount of contributed modules? Or is it a specific field formatter?
Comment #10
peacog commentedI believe it's easy to reproduce. If I add a simple multi-value text field and use the default formatter, and try to display the 2nd value, skipping the first, nothing is output. I'm using Panelizer.
Comment #11
andyg5000I can confirm this is still an issue as @peacog has described. His patch fixes the problem, but I have no clue if it's the best way to go.
Comment #12
dave reidI would suspect this is a problem somewhere else then that was exposed by this being fixed.
Comment #13
dave reidIt looks like this is a core bug in field_default_view() that resets the numeric field deltas when it does an array_merge().
Comment #14
dave reidI believe that #2430399: field_default_view() resets delta values due to array_merge() resolves the issue here.
Comment #15
peacog commentedI tried out the new patch from #2430399: field_default_view() resets delta values due to array_merge(). It works for me. Thank you.
Comment #16
mh86 commentedUnfortunately patch from #2 causes problems with the field collection module.
The field collection modules has formatters that show and "Add item" link on the entity view page. This add link should of course also be visible if no item has been added yet.
Due to the following code change the add link is not rendered any more
The attached patch reverts this change, not anything else from patch #2.
Comment #17
maximpodorov commentedThe patch #2 creates another problem also: #2411353: Default value of image field is not shown.
Comment #18
kristiaanvandeneynde@Dave Reid
Care to elaborate on why you changed this?
It seems unrelated to what you tried to accomplish for this issue, but broke "show X when empty" type of fields settings. The patch in #2411353: Default value of image field is not shown fixes it, however.
I'm asking because I don't see the logic behind that piece of code other than that it accidentally got added to the patch. If it did serve a purpose, it would be cool to get some info on it so others (including me :) ) can learn from it.
Comment #19
nchase commentedthe patch in 8 (in 16 neither) doesn't solve the problem. When I add 2 times the same field and want the first field to display 1 image from 0 and at the other field I want to show 10 images skipping the first, the first field controles the second. When I allow 10 images at the frist the second one showes the 10 images as well...
Comment #20
peacog commented@Nchase: have you tried the patch in #2430399: field_default_view() resets delta values due to array_merge()? If you revert the patch from this issue and apply the patch from the other I think that will solve the problem.
Comment #21
nchase commented@Paecog, wow, that solved my issue. Thanks! Didn't revert the one against ctools before applying the one against core... thx
Comment #22
peacog commentedTo avoid confusion I'm closing this as a duplicate of #2430399: field_default_view() resets delta values due to array_merge(). The patch there has been confirmed to work by a few people.