1

I have multiple entries fields that I want to use in the front-end. In the field settings I'll set up a sections source for each field. On the frontend I want a dropdown select box with all the entries from section I'll setup for each entries field. If I'll dump the field sources from the field layout I'll get this:

array(1) {
  [0]=>
  string(44) "section:a0977adf3fd-bd50adf-479cadf-90cd-8d5ef46de79c"
}

How to get entries related to the section source above?

{% entries = craft.entries.section(field.sources).all() %} // This is not working

This is the code where I'll need it for some context

{% for tab in entry.fieldLayout.tabs %}
    {{ tab.name }}
    {% for field in tab.getFields() %}
            <label for="{{ field.handle }}">{{ field.name }}</label>
            {% if field.className == 'craft\\fields\\PlainText' %}
                {# field text settings #}
            {% elseif field.className == 'craft\\fields\\Number' %}
                {# field number settings #}
            {% elseif field.className == 'craft\\fields\\Entries' %}
                {% set entries = craft.entries.section(field.sources).all() %} 
                {# entries is empty how to fix? #}
                <select name="fields[{{ field.handle }}][]">
                    <option value="">Choose</option>
                    {% for entry in entries %}
                        <option value="{{ entry.id }}">{{ entry.title }}</option>
                    {% endfor %}
                </select>
            {% endif %}
    {% endfor %}
{% endfor %}
2

UIDs are not yet usable in queries. You would need to extract the UID of the section from the string then fetch the sectionId to use in your query.

Something like this:

{% for tab in entry.fieldLayout.tabs %}
    {{ tab.name }}
    {% for field in tab.getFields() %}
        <label for="{{ field.handle }}">{{ field.name }}</label> <br>
        {% if field.className == 'craft\\fields\\PlainText' %}
        {# field text settings #}
        {% elseif field.className == 'craft\\fields\\Number' %}
        {# field number settings #}
        {% elseif field.className == 'craft\\fields\\Entries' %}

            {% set sources = [] %}

            {# Start our `entries` query #}
            {% set entries = craft.entries %}

            {# Iterate over sources #}
            {% for source in field.sources %}

                {# If All sources is selected in field definition, skip this #}
                {% if source != '*' %}
                    {# Extract UID from the string #}
                    {% set sectionUid = source | replace({'section:': ''}) %}

                    {# Fetch sectionId from UID and push in `sources` array #}
                    {% set sources = sources | merge([craft.app.sections.getSectionByUid(sectionUid).id]) %}
                {% endif %}
            {% endfor %}

            {# If we have `sources` then add to our query, else, fetch everything #}
            {% set entries = sources ? entries.sectionId(sources) : entries %}
            <select name="fields[{{ field.handle }}][]">
                <option value="">Choose</option>
                {% for entry in entries.all() %}
                    <option value="{{ entry.id }}">{{ entry.title }}</option>
                {% endfor %}
            </select>
        {% endif %}
    {% endfor %}
{% endfor %}

This will cater for multiple sources as well as All being chosen in your field definition

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.