Craft CMS Asked on August 12, 2021
I’m trying to get a simple Reverse Entries working, nothing fancy, but I’m stumped.
I created a little personal project just to help me stretch and learn Craft better. I picked the NFL Draft just because it was an easy dataset (and the draft is fun). This isn’t a live project, just something running in MAMP for learning purposes.
This is the structure:
I have 3 Channels (no singles):
*nflDraft2021
(each draft pick, with Entries fields for the other 2
channels)
*draftClass2021
(the available players)
*nflTeams
(the teams selecting)
The nflDraft2021
channel has a Matrix field:
Matrix Field Handle: draftInfo
Block handle: SelectionInfo
Fields in block: two Entries fields: playerSelected
and draftedBy
(Those relate to the draftClass2021 and nflTeams channels).
What I’m trying to do is on each Team page (the 32 entries in nflTeams
channel), have the Related Draft Picks for that team.
I’ve tried to follow along to this tutorial and came up with this attempt:
{% 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) %}
{% for player in players %}
{{ player.title }}
{% endfor %}
I also tried using entry like this post, but still nothing.
I am trying to get the title of the related entry first before attempting to get something from the Matrix field, but that would be my next goal if I can get the first step working.
The team page title shows up but that’s it, the reverse entries don’t do anything, not even an error. So, I’m obviously misunderstanding something but I’m not sure what. Any advice or help appreciated.
Your query isn't working because you're querying the section, not the individual matrix blocks. I think that's the root cause of the issue.
Sidenote: Since each matrix block is an element of its own, your nflDraft2021
is only indirectly related to the other two sections (i.e. the nflDraft2021
is related to a matrix block which is related to draftClass2021
and nflTeams
entries).
For the record, here's how to use the related parameter if you're querying for related entries inside a matrix block. You need to explicitly pass the field:
{% set nflDraft2021 = craft.entries()
.section('nflDraft2021')
.relatedTo({
targetElement: entry,
field: 'SelectionInfo.draftedBy',
})
.all()
%}
See Going Through Matrix in the documentation
However, if I'm understanding your setup correctly, nflDraft2021
is a single? In this case, you don't want to find the nflDraft2021
entry, but the individual SelectionInfo
matrix blocks in which the current team is selected in the draftedBy
field, correct?
In this case, the entries query won't work since it can only return entries, not individual matrix blocks. You need to use a Matrix Block Query instead:
{% set nflDraft2021 = craft.entries.section('nflDraft2021').one() %}
{% set currentTeamPicks = craft.matrixBlocks()
.owner(nflDraft2021)
.field('SelectionInfo')
.draftedBy(entry)
.all()
%}
Now this will give you an array of matrix blocks, not an array of picked players, so you'll need to access the playerSelected
field to get the actual players:
{% set currentTeamPlayers = currentTeamPicks|map(pick => pick.playerSelected.one() %}
Still not sure if I understand your setup completely, so there might be some errors in the examples, but hopefully this will help you get back on track!
Correct answer by MoritzLost on August 12, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP