3

I am using Entries Field to display related content. I have several Entries Field in an article and i also want to display the fields that are empty, but with their section name.... Is this even possible? I am quite new to Craft, so don't know if I am even in a right direction... Help will be highly appriciated!

{% set assets = craft.entries({
  relatedTo: { 
    sourceElement: entry, 
      field: ['entry','entry2','entry3']
    },
    order:     "sortOrder",
    limit:     null
  })  
%}

{% if assets %}
  {% for asset in assets %}
      {% if asset|title %}
        {{ asset.section.name }}: {{ asset.title }}
      {% else }
        {{ asset.section.name }}: Empty
      {% endif }
  {% endfor %}
{% endif %}
4
  • Can you clarify a bit what you are trying to do? Are the different entry fields for different sections, and you want to show the section title even if there are no selected related entries? Commented Aug 21, 2015 at 1:14
  • "display the fields that are empty, but with their section name" – what do you mean with section name? The field's title? Or did you limit the entries field to one section only and want to show that?
    – carlcs
    Commented Aug 21, 2015 at 5:25
  • Yes, that is completely right. I want to show the section name even if there are no selected entries.
    – Fredrik
    Commented Aug 21, 2015 at 6:07
  • 1
    I could make them as separates Asset Fields in the template and put a header ove hit. But i really want they displayed in an array. Because it could be a lot of sections (not only 3 as now).
    – Fredrik
    Commented Aug 21, 2015 at 10:28

1 Answer 1

1

It's probably not possible to get the sections you limited your entries field to from the template and if there's no entry selected that's what you'd need to do.

But what I'd recommend and what's generally a pretty solid approach in such situations is to build an associative array with all the necessary data and loop through that array:

{% set relationsGroups = {
    'entriesField1': 'Heading Relations 1',
    'entriesField2': 'Heading Relations 2',
    'entriesField3': 'Heading Relations 3',
} %}

{% for fieldHandle, heading in relationsGroups %}
    {% set assets = craft.entries({
        relatedTo: {
            sourceElement: entry,
            field: fieldHandle
        }
        order: 'sortOrder',
        limit: null
    }) %}

    {% for asset in assets %}
        <li>{{ heading }}: {{ asset.title }}</li>
    {% else %}
        <li>{{ heading }}: Empty</li>
    {% endfor %}
{% endfor %}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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