1

I'm trying to list entries in a section which are documents. Those documents are grouped by checkbox. I'd like to list the document title and the asset in the entry's url. I only want to list assets which have a specific checkbox ticket.

I was able to output and group the assets by checkbox, but I'm struggling to find that entries asset url. Any thoughts?

This is what I have so far:

{% set entries = craft.entries.section('documentLibrary')%}

{# get all checkbox options #}
{% set options = entries.first().documentType.options %}
{% set assets = entry.documents %}

 {% for entry in entries if entry.documentType.contains('Report') %}
    {% for asset in assets %}
       <a href="{{ asset.url }}" type="application/pdf;" class="list-group-item active">{{ entry.title }}</a>
    {% endfor %}
 {% endfor %}

Unfortunately as expected the {{ asset.url }} returns the url of the entry and not of the asset documents which is attached to the entry.

1

Your assets variable is defined outside the entries loop so it is still referring to the page entry. If you move it inside the loop it should work as expected.

{% set entries = craft.entries.section('documentLibrary') %}
{% for entry in entries if entry.documentType.contains('Report') %}
    {% for asset in entry.documents %}
       <a href="{{ asset.url }}" type="application/pdf;" class="list-group-item active">{{ entry.title }}</a>
    {% endfor %}
{% endfor %}

Rather than doing this manually however, you might also have a look at the group filter, which can group the entries array by document type.

{% set entries = craft.entries.section('documentLibrary').find() %}
{% set groupedEntries = entries|group('documentType') %}

{% for type, entriesByType in groupedEntries %}
    <h2>{{ type }}</h2>
    <ul>
        {% for entry in entriesByType %}
            {% for asset in entry.documents %}
                <a href="{{ asset.url }}" type="application/pdf;" class="list-group-item active">{{ entry.title }}</a>
            {% endfor %}
        {% endfor %}
    </ul>
{% endfor %}

Note: Also if each 'document' only contain one and only one 'asset' (i.e. pdf), then you can also get rid of the asset loop and just use {{ entry.document.first.url }}.

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.