1

I have blocks set up for nutritional values: Calories, Total Fat, Carbohydrates, etc. These are set up in NEO blocks.

I can successfully loop through all of the NEO blocks (about 20 rows total) and output the content using this for loop:

{% for block in entry.nutritionBuilder.level(1).all() %}
    ...
{% endfor %}

Later, I added a line ABOVE this loop to retrieve the content of a single block:

{% set item = entry.nutritionBuilder.type('blockCalories').one() %}
{{ item.nutritionValue }}

This works, in that it will output the value I need to retrieve. However, the for loop below this line now only outputs a single row from nutritionBuilder which is blockCalories row.

Can anyone see why this is happening?

2

This is because of a change in Craft 3 and you need to clone your query (docs).

I have not tested this but the below should work:

{% set blocks = entry.nutritionBuilder %}

{% set item = clone(blocks).type('blockCalories').one() %}
{{ item.nutritionValue }}

{% for block in clone(blocks).level(1).all() %}
    ...
{% endfor %}
| improve this answer | |
0

Oli, thanks for pointing me in the right direction. Your answer was close, but there's a slight modification:

{% set blocks = entry.nutritionBuilder %}

{% set item = clone(blocks).type('blockCalories').one() %}
{{ item.nutrionValue }}

{% for block in clone(blocks).level(1).all() %}
...
{% endfor %}

The cloning needs to happen when setting the item variable as well as in the for loop.

| improve this answer | |
  • Ha, too quick :) I've corrected the answer. – Oli Oct 16 at 4:57

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.