The TL;DR summary: if I have a related Entries field that I access, can I then access an Entries field within that entry?
I created a learning project running on MAMP to help me learn Craft, and one of the things I'm trying to wrap my head around is some of the ways I can use Relationships. Originally I had a Matrix field but I deleted that because it was adding too much complexity.
I picked the NFL draft as a data set because it's easily available and has many different cross relationships.
The basic concept is this, you have 32 NFL teams, which have their own channel nflTeams
(not singles, regular channel), a channel for the players that are available (draftClass2021
), a channel for the schools the players attended (colleges
), and finally a channel for that years Draft (nflDraft2021
).
None of my sections are singles, everything is a regular channel.
Within that Sections structure I would have 2 Entries fields in the nflDraft2021
section:
draftPick
(the player selected, from section draftClass2021
)
teamDraftedby
(the team that selected him, from section nflTeams
)
Each entry in the draftClass2021
would also have an Entries field playersSchool
, which links to the colleges
section.
What I am trying to figure out is, using reverse relationships, can I have a page for each NFL team that lists all the players they selected in the draft, AND the school each attended?
I can get the first part, the players selected, and I can even pull the data from other fields in that related entry, but not the Entries field that links to the school.
I created a graphic to help visualize the structure:
Here is the code:
{% set myEntryQuery = craft.entries()
.section('nflTeams')
.all() %}
{# title for team page #}
<h1>{{ entry.title }}</h1>
{# get the related entries from the nflDraft2021 channel #}
{% set players = craft.entries.section('nflDraft2021').relatedTo(entry).all() %}
{% for player in players %}
{% set relatedEntries = player.draftPick.all() %}
{% if relatedEntries|length %}
<ul>
{% for rel in relatedEntries %}
<li>{{ player.draftSlot }} <a href="{{ rel.url }}">{{ rel.title }}</a> <br>{{rel.combineOrProDay }}
{{ rel.playerPosition }} <br>
{# This is the Entries field that is related to the player #}
{{ rel.playersSchool }}
</li>
{% endfor %}
</ul>
{% endif %}
{% endfor %}
I've read the documentation on Relationships and Entries fields but I couldn't figure out from that if what I was trying to do was even possible. Any thoughts greatly appreciated.