1

I have a Neo field set up like so:

-Row
--Column
-Body

Columns can also contain body.

With this:

{% set modules = entry.pageBuilder
        .with([
            'moduleBody',
            'moduleRow',
            'moduleColumn',
        ])
        .level(1)
        .all()
%}

{% for module in modules %}
    {% if module.type != 'moduleColumn' %}
        {% include '_partials/page-builder/_' ~ module.type %}
    {% endif %}
{% endfor %}

{% set moduleArray = [] %}

{# Remove duplicate module types from array #}
{% for moduleType in modules %}
    {% if moduleType.type not in moduleArray %}
        {% set moduleArray = moduleArray|merge([moduleType.type]) %}
    {% endif %}
{% endfor %}

_moduleBody.twig

<div class="module-body mb-8">
  {% if module.heading %}<h2>{{ module.heading }}</h2>{% endif %}
  {{ module.body }}
</div>

I get body blocks output fine if they're not nested in a row/column.

_moduleRow.twig

<div class="row flex flex-wrap">
    {% set columns = module.children %}
    {% for column in columns %}
    <div class="md:w-1/{{column.width}} ">
    {% for item in column.children %}
        {% include '_partials/page-builder/_' ~ item.type %}
    {% endfor %}
    </div>
    {% endfor %}
</div>

With the above, I get an error:

Looping through element queries directly has been deprecated. Use the all() function to fetch the query results before looping over them.

If I change set columns = module.children to set columns = module.children.all() I get the same thing. If I change it to

{% set columns = module.children
        .with(['moduleColumn'])
        .level(1)
        .all()
%}

there's no error, but no output either (other than the HTML for the row which is outside the code for the column contents).

What do I need to do to get the columns to output?

I guess I'm also going to have a problem with for item in column.children because inside moduleBody it's calling module.variable not item.variable but if the outer loop uses module, I can't use it for the inner loop too.

1

For anyone else having a similar problem, the solution was this:

    {% for module in entry.pageBuilder.all() %}
        {% do module.useMemoized(entry.pageBuilder) %}
    {% endfor %}

    {% for module in entry.pageBuilder.all() if module.level == 1 %}
        {% if module.type != 'moduleColumn' %}
            {% include '_partials/page-builder/_' ~ module.type %}
        {% endif %}
    {% endfor %}

and then inside _moduleRow.twig

    <div class="row flex flex-wrap">
        {% for module in module.children.all() %}
            {% if module.type == 'moduleColumn' %}
                <div class="md:w-1/{{module.width}} ">
                {% for module in module.children.all() %}
                    {% include '_partials/page-builder/_' ~ module.type %}
                {% endfor %}
                </div>
            {% else %}
                {% include '_partials/page-builder/_' ~ module.type %}
            {% endif %}
        {% endfor %}
    </div>
| improve this answer | |

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.