1

I have a content builder matrix that I'm looping through to build my page. That code works with no problems.

{% set content = entry.pageBuilder.all() %}
{% for block in content %}
    {% include "blocks/_" ~ block.type ignore missing %}
{% endfor %}

Above that block, in the same template, I'm trying to create a text blurb from one particular field type within that matrix. When I try to define what field to pull the blurb from, suddenly the above code only outputs the content of the blurb field. This is the code that breaks my template:

{% set blurb = entry.pageBuilder.type('copy,faq').limit(1).all() %}

But, if I move it below the "set content..." chunk, it works as expected.

It seems like these should be two completely unrelated chunks of code, and I don't understand why the "set blurb..." code is interfering with the other code when placed above it. Am I missing something?

1
  • If you change ‘blurb’ to another word does it have any effect? If you print the block.type rather than import the partial does it display the types as expected?
    – Joe Prisk
    Mar 27, 2021 at 6:52

1 Answer 1

2

This is a change in behaviour from Craft 2 to Craft 3 and here is the explanation.

In short, you need to clone your query (docs), something like the below should work:

{# Initiate your query #}
{% set content = entry.pageBuilder %}

{# Clone the original query to get your blurb #}
{% set blurb = clone(content).type('copy,faq').limit(1).all() %}

{# Get everything #}
{% for block in content.all() %}
    {% include "blocks/_" ~ block.type ignore missing %}
{% endfor %}
1
  • Thanks! I missed that change, and somehow it hasn't come up for me until now.
    – artmem
    Mar 29, 2021 at 14:36

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.