1

I have an Entry called Groups (I'm wondering if this might be some sort of reserved word issue) and, with Twig, I can bring out the base fields of id and title with no issue but when I try to pull out the first user-defined field: entry.groupChair, it sends a bunch of jibberish (looks like code). Snippet of Jibberish:

  O:28:"craft\elements\db\EntryQuery":86:{s:8:"editable";b:0;s:9:"sectionId";N;s:6:"typeId";N;s:8:"authorId";N;s:13:"authorGroupId";N;s:8:"postDate";N;s:6:"before";N;s:5:"after";N;s:10:"expiryDate";N;s:17:"*defaultOrderBy";a:1:{s:16:"entries.postDate";i:3;}s:11:"elementType";s:20:"craft\elements\Entry";s:5:"query";N;s:8:"subQuery";N;s:12:"contentTable";s:12:"[...much more omitted]  

I have tried clearing the layout and reloading it. I have permanently deleted the Groups entry and brought it back in with FeedMe. Still have the issue. Groups look and operate fine in the front end. I was concerned that maybe the variable wasn't defined and added a test for that, but with the jibberish, it's considered defined and it prints out on screen.

{% set myEntryQuery = craft.entries()
    .section('groups')
    .limit(40) 
    .order('title')
    %}
    
{% set entries = myEntryQuery.all() %}

{% block content %}

<ul>
    {% for entry in entries %}
        <li>
            <label>
               Group:({{entry.Id}}) {{ entry.title}} <br>

           {% if entry.groupChair is defined %} 
              &nbsp; Group Chair: {{ entry.groupChair}}  |
           {% endif %}
            </label>
        </li>
    {% endfor %}
</ul>

{% endblock %}
1
  • What field type is your groupChair field?
    – MoritzLost
    Jul 9, 2021 at 7:19

1 Answer 1

1

I'm assuming that groupChair is an entries field, correct? So what you're expecting from groupChair is a related entry associated with the current entry through that field?

The output you're seeing is the result of outputting an element query directly, which is a common mistake. To actually get to the related entry, you need to execute the query using .all() or .one() (depending on whether you want one or multiple related entries).

Those methods will execute the query and return either an array of entries (.all()) or a single entry or null (.one()) if there is no result.

The reason it works this way is that all relationship fields in Craft (Asset, Category, Entry fields, etc) return an element query instead of returning the related elements directly. This gives you an opportunity to modify the query parameters before executing the query.

Finally, once you have your related entry, you still need to specify a field to output – for example, the title. Here's an example that should work:

{% set groupChair = entry.groupChair.one() %}
{% if groupChair %} 
    Group Chair: {{ groupChair .title }}
{% endif %}

See the documentation pages linked above for more information.

1
  • 1
    Thank you, my colleague immediately shot back an answer about what was going on. Your answer worked. I was able to expand on that and pull in a bunch of data. Jul 22, 2021 at 19:03

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.