3

Craft 3.0.16.1

I have a relationship that is outputting entries, but the entries are not in the order as selected.

{# not sorting correctly  - **fieldName** is a variable passed via include #}
{% set relatedEntries = publications.relatedTo(
  {sourceElement: country, field: fieldName}
) %}

I copied this code from the documentation and change field names to work

{# copied from docs but template errors #}
{% set relatedEntries = craft.entries({
    relatedTo: { sourceElement: country, field: fieldName },
    orderBy:     "sortOrder",
    limit:     null
}) %}

but this throws a database exception error

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'sortOrder' in 'order clause'

How do I get related entries to sort on the order as selected in the entries field?

********************UPDATE**********************

HERE IS THE FULL TEMPLATE

  <li class="accordion-item {#{% if loop.first %}is-active{% endif %}#}" data-accordion-item>
    <!-- Accordion tab title -->
    <a href="#" class="accordion-title">
      {{ categoryTitle  }}
      <i class="icon-chevron-right"></i><!-- /.arrow right -->
    </a>

    <!-- Accordion tab content: it would start in the open state due to using the `is-active` state class. -->
    <div class="accordion-content" data-tab-content>

      {# start tabbed content #}
      <ul class="tabs" data-tabs id="cat-{{ hash }} ">

       {# inside categories list countries #}
      {% for country in countries.all() %}  
        <li class="tabs-title {#{% if loop.first %}is-active{% endif %}#}"><a href="#{{ country.slug }}" aria-selected="true" data-tabs-target="{{ country.slug }}">{{ country.title }}</a></li>
      {% endfor %} {# end countries loop #}
      </ul>


      <div class="tabs-content" data-tabs-content="cat-{{ hash }}">
        {% for country in countries.all() %}
        <div class="tabs-panel {#{% if loop.first %}is-active{% endif %}#}" id="{{ country.slug }}">
          <ul class="no-bullet pubs full-width">

            {# finally output entries that are in the category and country #}

            {# not sorting correctly #}
            {% set relatedEntries = publications.relatedTo(                   {sourceElement: country, field: fieldName}
            ) %}

            {# error about method ('all') on an array #}
            {# {% set relatedEntries = country[fieldName].all() %} #}

            {# 
            // copied from docs but template errors
            {% set relatedEntries = craft.entries({
                relatedTo: { sourceElement: country, field: fieldName },
                orderBy:     "sortOrder",
                limit:     null
            }) %} #}



            {% for entry in relatedEntries.all() %}

              {% for block in entry.Documents.all() %}
                {% if block.externalLink|length %}
                  {# use external link field #}
                  {% set link = block.externalLink %}
                {% else %}
                  {# use uploaded file #}
                  {% set link = block.pdfOrFile.one().url ?? null%}
                {% endif %}

                <li><a href="{{ link }}" target="_blank">
                  <p>{{ entry.title }}</p>
                  <div class="icon-holder">
                    <img src="/assets/img/next.svg" alt="" class="go-icon">
                  </div><!-- /.icon-holder --></a>
                </li>
              {% endfor %}{#end link #}

              {% else %}
              {# output nothing for no results #}
            {% endfor %}

          </ul><!-- /.no-bullet pubs -->
        </div>
        {% endfor %}
      </div>
      {# end tabbed content #}      

    </div><!--accordion content-->
  </li>
  • You trying to output a list of entries that were selected in an Entries field (in the order selected), is that right? – Simon Kuran Jul 20 '18 at 18:31
  • @SimonKuran Yes that is correct – CreateSean Jul 24 '18 at 13:17
3

When you include templates and pass multiple different variables to them with different types that may have a different field layout it is required to code defensively and check for variable types. Actually you should never print something without further checking. Anything bad that could happen will happen. Especially in such a case

{% if country[fieldhandle] is defined and country[fieldhandle] is not empty and  %}
    {% for element in country.getFieldValue(fieldhandle).all() %}
        {{ element.title }}
    {% endfor %}
{% endif %}

You may as well include a instance of check

{% if element.getFieldValue(fieldHandle) is instance of('craft\\elements\\db\\ElementQuery') %}
    {% set relatedElements = element.getFieldValue(fieldHandle).all() %}
{% else %}
    // eager loading, the result is an array, no need to fetch it
    {% set relatedElements = element.getFieldValue(fieldHandle) %}
{% endif %}
  • Robin - I"m not sure what you mean. but I tried your code and got an error `impossible to invoke a method ("all") on an array. I really need to be able to have these entries output in a specific order. – CreateSean Jul 24 '18 at 13:12
  • Do you eager load them? Because that would be a reason why it is not working, normally that field is a default query.. You can sort the results as you like – Robin Schambach Jul 24 '18 at 13:13
  • Not eager loading - have updated the original post with the full template code. – CreateSean Jul 24 '18 at 13:15
  • Where do you set the fieldName variable? I'm a little bit confused, because you just have to call entry.field.all() – Robin Schambach Jul 24 '18 at 13:17
  • That's coming through an include ``` {% include '_includes/pub-section' with { "categoryTitle" : "Market Monitor", "hash" : "market-monitor", "fieldName" : "marketMonitor" } %}``` – CreateSean Jul 24 '18 at 13:26

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.