1

I'm attempting to query for all assets associated with a specific form submission like this, where 'submission' has been set based on a URL parameter:

{% for field in submission.fieldMetadata %}
    {% if field.type == "file" %}
        {% set assetId = attribute(submission, field.handle) %}
        {% set assets = craft.assets.id(assetId).all() %}
           {% for asset in assets %}
                {% if asset %}
                    {% if asset.kind == "image" %}
                        <img src='{{ asset.url }}'>
                    {% endif %}
                {% endif %}
            {% endfor %}
    {% endif %}
{% endfor %}

The above code returns all site assets rather than just the assets associated with the submission. All other fields associated with the submission are returned correctly. Is this a bug or is my template incorrect?

1

The following template code resolved this issue for me:

{% for field in submission.fieldMetadata %}
     {% if field.type == "file" %}
        {% set assetIds = attribute(submission, field.handle).value %}
        {% if assetIds %}
           {% for assetId in assetIds %}
              {% set asset = craft.assets.id(assetId).one() %}
              {% if asset %}
                 {% if asset.kind == "image" %}
                    <img src="{{ asset.url }}" />
                 {% else %}
                    <a href="{{ asset.url }}">{{ asset.filename }}</a>
                 {% endif %}
              {% endif %}
           {% endfor %}
        {% endif %}

     {% else %}
        {{ attribute(submission, field.handle) }}
     {% endif %}
{% endfor %}

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.