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.

Comments

dave reid’s picture

Status: Active » Needs review
StatusFileSize
new1.36 KB
dave reid’s picture

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

joelpittet’s picture

Status: Needs review » Reviewed & tested by the community

This is a nice clean up and I've been using it in production for a number of months. Thanks @Dave Reid

mrjmd’s picture

japerry’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: +SprintWeekend2015

Works good for me. Committed!

  • japerry committed fafbd5c on authored by Dave Reid
    Issue #2336985 by Dave Reid: ctools_entity_field_content_type_render()...

Status: Fixed » Closed (fixed)

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

peacog’s picture

Status: Closed (fixed) » Needs review
Related issues: +#2418921: Field formatter settings for multi-value fields broken
StatusFileSize
new696 bytes

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

dave reid’s picture

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

peacog’s picture

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

andyg5000’s picture

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

dave reid’s picture

I would suspect this is a problem somewhere else then that was exposed by this being fixed.

dave reid’s picture

It looks like this is a core bug in field_default_view() that resets the numeric field deltas when it does an array_merge().

dave reid’s picture

peacog’s picture

I tried out the new patch from #2430399: field_default_view() resets delta values due to array_merge(). It works for me. Thank you.

mh86’s picture

Unfortunately 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

   if (!is_array($all_values)) {
-    $all_values = array();
+    // Do not render if the field is empty.
+    return;
   }

The attached patch reverts this change, not anything else from patch #2.

maximpodorov’s picture

kristiaanvandeneynde’s picture

@Dave Reid
Care to elaborate on why you changed this?

   if (!is_array($all_values)) {
-    $all_values = array();
+    // Do not render if the field is empty.
+    return;
   }

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.

nchase’s picture

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

peacog’s picture

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

nchase’s picture

@Paecog, wow, that solved my issue. Thanks! Didn't revert the one against ctools before applying the one against core... thx

peacog’s picture

Status: Needs review » Closed (duplicate)

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