I'm trying to remove duplicate image objects from an array, but can't figure out why it won't find it:
{% for image in product.productGallery %}
{% set productGalleryArray = productGalleryArray|merge([image]) %}
{% endfor %}
{% for variant in product.variants %}
{% set variantImage = variant.productVariantImage.first() %}
{% if variantImage|length and variantImage not in productGalleryArray %}
{% set productGalleryArray = productGalleryArray|merge([variantImage]) %}
{% endif %}
{% endfor %}
{% if productGalleryArray|length %}
<div class="slideshow">
{% for image in productGalleryArray %}
<img src="{{ image.url }}" alt="{{ image.title }}">
{% endfor %}
</div>
{% endif %}
Essentially what I'm trying to do is have a productGallery
field which you can add images to, but also add an image to the product variant via productVariantImage
.
This can cause the issue where by theres duplicate of the same image being output from the slideshow after it's merged the two fields together.
So I need to ideally:
- Remove the duplicates before the array is merged, OR
- Find and remove duplicates after the array is merged (Via a Twig filter?)