humanmade / network-media-library

Network Media Library plugin for WordPress Multisite
MIT License
290 stars 55 forks source link

Images in ACF repeaters returns the same image (last one) for any repeating element #73

Open elmarbransch opened 4 years ago

elmarbransch commented 4 years ago

If you create an ACF repeater containing an image it will correctly return the structure of the sub fields, but the value for image is all the same for each repeater row. And it is the value of the last image in the repeater set.

The fix is fairly easy, but I cannot provide a real PR right now. Instead I can document the required change here. The reason is that the two hooks in place here

add_filter( "acf/load_value/type={$type}", [ $this, 'filter_acf_attachment_load_value' ], 0, 3 );
add_filter( "acf/format_value/type={$type}", [ $this, 'filter_acf_attachment_format_value' ], 9999, 3 );

are not called for each repeating row alternately, but it will call n times load_value and n times format_value. To solve this I simply pushed the values an an array and grab them later from that array.

so I declared:

protected $values = []; // null; and then assigned this way $this->values[] = $image; // $this->value = $image;

and finally pulled the value here return array_shift( $this->values ); // return $this->value; i hope this is easy enough to understand to put this back into the main code repo,

Best -Elmar

elmarbransch commented 4 years ago

I found a similar approach (better than mine) in this pull request here

https://github.com/humanmade/network-media-library/pull/67/commits/8dbfcf36bdca8a9e657fb9a57a82494aa491ec4e

does anyone watch these?

kleins commented 3 years ago

Please merge this pull request. Thanks

jackjonesNOW commented 2 years ago

I found it quite difficult to get the image url or post object to display using flexible content. My work around was to comment out this ACF addition and just call in the image via the ID. and do something similar to this:


<?php $hero = get_sub_field('background_image');
$size = 'large';
$image = wp_get_attachment_image_src($hero, $size); ?>
<?php echo $image[0]; ?> 
chrispink commented 1 year ago

Just to note this is also an issue with Flexible Content fields where the image returned in all positions is the last one in the Flexible Content set. Presumably the same issue.