2

Question is similar to some others I've looked through but their solutions don't work as there are some differences.

Currently this doesn't output any of the fields I try to access within the internal matrix.

{% for entry in craft.entries.section('sectionHandle').find() %}
    {% for block in entry.matrixHandle.limit(1) %}
        {% for entry in block.entryFieldMatrixHandle.first() %}
            {% for block in entry.entrysMatrixHandle.limit(1) %}
                {{ block.fieldHandle }}
            {% endfor %}
        {% endfor %}
    {% endfor %}
{% endfor %}
2
  • 1
    Not sure if it will solve your problem or not, but your inner {% for entry and {% for block loops are overwriting the output entry and block variables that are defined in the outer loops. You'll want to rename them to something else. – Brad Bell Sep 16 '16 at 17:35
  • Yep! Thanks! Didn't realize that's how it worked! – Indigo Viking Sep 21 '16 at 11:10
1

There's a couple of problems with your current code – for one, you're (as Brad mentions in the comments above) doing some variable overwriting by reusing the entry and block names. Also, you're attempting to do a for loop on the result from a call to first(), which won't work (first() returns a single element, not an array).

In any case, I'd suggest getting rid of all the for loops (you're looping on a resultset with a single element in each of your inner loops), in an attempt to make the code a tad more clear.

Something like this should work:

{% for entry in craft.entries.section('sectionHandle') %}
    {% set outerBlock = entry.matrixHandle.first() %}
    {% set outerBlockEntry = outerBlock ? outerBlock.entryFieldMatrixHandle.first() %}
    {% set innerBlock = outerBlockEntry ? outerBlockEntry.entrysMatrixHandle.first() %}
    {{ innerBlock ? innerBlock.fieldHandle }}
{% endfor %}

That's a lot of {% set %} statements, though – you could also do this (although I'm honestly not sure which option is more readable):

{% for entry in craft.entries.section('sectionHandle') %}
    {% set block = entry.matrixHandle.first().entryFieldMatrixHandle.first().entrysMatrixHandle.first()|default %}
    {{ block ? block.fieldHandle }}
{% endfor %}

Finally, as an alternative approach altogether you could use the relatedTo param:

{% for entry in craft.entries.section('sectionHandle') %}

    {% set block = craft.entries.relatedTo({
        sourceElement: entry.matrixHandle.first(),
        field: 'entryFieldMatrixHandle'
    }).first().entrysMatrixHandle.first()|default %}

    {{ block ? block.fieldHandle }}

{% endfor %}

None of the above examples are tested, and now my head hurts – but hopefully it'll prove helpful :)

1
  • Thanks! I didn't realize the variable name could be change at the beginning of the for loop. I thought it was stuck as "block" and "entry". All set now! – Indigo Viking Sep 21 '16 at 11:10

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.