Jump to content

images in WireArray with same name


Macrura
 Share

Recommended Posts

came across a strange edge case;

I'm creating a new WireArray to collect images from various pages, which all get output into a gallery.

In this particular site, the client had several images all with the exact same name on different pages, and when those were imported into the WireArray, they are de-duplicated, even though they are different images on different pages, so those images were not showing.

I worked around it by renaming the images on the fly, and i can use the custom upload names to prevent this from happening in the future, but it does seem like someone else might run into this, not realizing that the wireArray is assuming the same named images are the same on import;

i guess maybe i could have imported them into the wireArray using this syntax?

    $key = 0;
    foreach($p->product_images as $image) {
        $productImages[$key] = $image;
        $key++;
    }
Link to comment
Share on other sites

I was using import - is that the reason why it was de-duplicating on the file name?

this is the code

$productImages = new WireArray();

foreach( $page->children as $p ) {
   $productImages->import($p->product_images);
}

$productImages->import($page->product_images_master);
Link to comment
Share on other sites

I was using import - is that the reason why it was de-duplicating on the file name?

Yep. WireArray::import() des check if the value is already in the array, if so, it does not import it again.

In your case, I guess the PageImage::toString() method returns the same filename, so ProcessWire assumes that it's the same image, although it's from a different page.

What you could do is to add them to a normal PHP array and then use WireArray::setArray() to pass them (not tested):

$productImages = new WireArray();

$temp = array();
foreach ($page->childern as $p) {
  $temp = array_merge($temp, $p->product_images->getArray());
}

$productImages->setArray($temp);

Cheers

  • Like 1
Link to comment
Share on other sites

I think i may actually require that these go directly into WireArray, because when they are being output, i am using a lot of the properties, like $image->page, and then all of the inherited fields of the page that the image was pulled from; if i were to put them into a plain array, i would lose the $page property...

maybe there needs to be a parameter for import which allows duplicates, especially in the case of page images?

EDIT: or maybe your example does preserve the properties, with the getArray() ? will have to get more up to speed on that..

Link to comment
Share on other sites

cool... will do some testing and report back;

wondering if this should be documented somewhere, because i can see people assuming that the WireArray would differentiate images with the same name if they were keyed to different pages..

Link to comment
Share on other sites

Why do you need WireArray? It can be a normal array and store images in there.

Why not? :)

WireArray offers way more possibilities for manipulating/filtering data, which is always nice to have.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...