TransWikia.com

Why does getting a single block in NEO prevent getting the remainder of the blocks?

Craft CMS Asked by Vaughn D. Taylor on February 1, 2021

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 Answers

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 %}

Answered by Oli on February 1, 2021

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.

Answered by Vaughn D. Taylor on February 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP