2

On our play pages (ex. http://actors-express.com.s102768.gridserver.com/plays/liaisons), we list our each of the cast members, their headshot and role. We did this by creating a matrix field that pulls in the Entry for each person and relates it to the specifics for the show. List of Cast Members, Headshot and Role

We would like to write code on the template for their bio page (ex. http://actors-express.com.s102768.gridserver.com/people/kristin-butler) that will scan through all those matrices on play pages and find out which plays they have been in, then display a list of plays with links to each of their URLs. It would be nice to pull in what their role was in the play, but that's not a requirement at this point.

List of Past Plays and Widget for Upcoming Play

We would also perform a lookup and see if one of the plays was currently on stage (a flag on our play entry type), or if it was upcoming to pull it into the module above on the right.

1 Answer 1

4

To access related entries stored in a matrix field, you need to specify the targetElement (actor/entry) and the matrix field (using dot syntax 'matrixFieldHandle.entriesFieldHandle') from 'plays'.

In your actor biography template:

Actor: {{ entry.title }}<br><br>

{% set plays = craft.entries.section('plays').relatedTo({
    targetElement: entry,
    field: 'actorsMatrixFieldHandle.actor'
}) %}

Plays:
<ul>
    {% for play in plays %}
        <li>
            {{ play.title }}<br>
            {% if play.onStage %}
                <span class="onstage">ON STAGE</span>
            {% endif %}
        </li>
    {% endfor %}
</ul>

To get the role is little more difficult — you first need to find the actor's matrix block in the related 'play' record. We can do this by testing each block to see if it matches the current entry (actor), or use an if clause in the loop.

<ul>
    {% for play in plays %}

        {% set role = '' %}
        {% for block in play.actorsMatrixFieldHandle if block.actor.first.title == entry.title %}
            {% set role = block.role %}
        {% endfor %}

        <li>
            {{ play.title }}<br> 
            {{ role }}<br>
            {% if play.onStage %}
                <span class="onstage">ON STAGE</span>
            {% endif %}
        </li>

    {% endfor %}
</ul>

You could also try to set the title field as a parameter to the Matrix block query:

{% set actorBlocks = play.actorsMatrixFieldHandle.title(entry.title) %}
{% set role = actorBlocks|length ? actorBlocks.first().role : '' %}
4
  • It might be possible to add a myCustomField parameter to the Matrix block query to get rid of the conditional / extra loop.
    – carlcs
    Commented Apr 19, 2015 at 8:48
  • @carlcs Sounds great - but not sure what your suggesting exactly. (Feel free to add another code snippet to the answer.) Commented Apr 19, 2015 at 15:07
  • Douglas, Craft 2.3 introduced setting custom fields as a param to Matrix block queries, so it's not only possible to filter by block type but any other block property (→ 2.3 release notes under templating).
    – carlcs
    Commented Apr 19, 2015 at 18:04
  • Didn't know that — much better solution! Thanks. Commented Apr 19, 2015 at 18:54

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.