2

Good afternoon All,

Im trying to setup the ability for users to delete 1 or more of their own images from a multi photo asset field and have got stuck.

This is what is have so far, I assumed adding a blank value would remove the asset in question

{% set images = myAssetQuery.all() %}

{% for image in images %}
    <span><img src="{{ image.getUrl('listingthumb') }}" alt="{{ image.title }}"></span>

    <input type="checkbox" name="fields[businessStandardImages][]" value="" />

{% endfor %}

Any help would be greatfully received

2

Assuming you are on a front end entry form editing an entry, there are a few steps to this.

First get all assets a user can choose from:

{% set allImages = myAssetQuery.all() %}

Next, get the ids of already chosen assets:

{% set alreadyChosenImages = entry.businessStandardImages.ids() %}

Loop over all images, check the ones already selected:

{% for image in allImages %}
<div>
    <label>{{ image.title }}
        {{ input(
            'checkbox',
            'fields[businessStandardImages][]',
            image.id,
            { checked: image.id in alreadyChosenImages }
        ) }}
    </label>
</div>
{% endfor %}

Altogether:

{# Get all possible assets #}
{% set allImages = myAssetQuery.all() %}

{# Get the currently related asset IDs #}
{% set alreadyChosenImages = entry is defined
    ? entry.businessStandardImages.ids()
    : [] %}

{# Loop over images and check any already selected ones #}
{% for image in allImages %}
<div>
    <label>{{ image.title }}
        {{ input(
            'checkbox',
            'fields[businessStandardImages][]',
            image.id,
            { checked: image.id in alreadyChosenImages }
        ) }}
    </label>
</div>
{% endfor %}

There is also a complete example in the documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.