1

I'm trying to pull in the contents of a matrixField block and merge it with some default info —

{% set interviews = craft.entries.section('interviews') %}

{% for interview in interviews %}

    {% set blocks = interview.thumb %}
    {% for block in blocks %}

        {% set options = block|merge({
            'class': 'episode',
            'more': 'Listen to this interview...',
            'slug': craft.request.firstSegment,
            'linkUrl': interview.url,
        }) %}

        {% include '_includes/blocks/half-block.html' with { options: options } %}

    {% endfor %}
{% endfor %}

I get the error: Key "body" for array with keys "id, enabled, archived, locale, localeEnabled, slug, uri, dateCreated, dateUpdated, root, lft, rgt, level, searchScore, fieldId, ownerId, ownerLocale, typeId, sortOrder, collapsed, class, more, reverse, linkUrl" does not exist

Why am I getting block with the values of the entry instead of that single block of type thumb?

(where thumb has a field called body)

I had tried {% set block = interview.thumb.first() %} instead of {% set blocks = interview.thumb %}{% for block in blocks %} but was trying to be more explicit to see if it made a difference.

  • To clarify— I think all those properties you see (I assume when trying to call options.body in the partial?) are common to all elements in Craft, so it may look like an EntryModel but quack like a MatrixBlockModel… That's the expression, right? – August Miller Nov 23 '16 at 0:07
2

I suspect this isn't so much a problem with the strategy as the implementation of the MatrixBlockModel, which is the object type you're trying to merge with a standard Twig "object" or associative array (not sure exactly what Twig uses, behind the scenes).

If you're trying to provide some defaults for various Matrix fields, those ought to be set within the fields, individually.

Otherwise, I think the solution you need is something like this:

{# ... #}
{% set blocks = entry.matrixBlockField %}
{% for block in blocks %}

  {% set options = {
    'class': 'episode',
    'more': 'Listen to this interview...',
    'slug': craft.request.firstSegment,
    'linkUrl': interview.url
  } %}

  {% include '_includes/blocks/half-block.html' with {
    options: options,
    data: block
  } %}
{% endfor %}
{# ... #}

In your partial, you have a bit more work ahead of you, but the special behavior of Twig's ternary operator should help:

{{ data.field ?: options.fallbackField }}

I may be misunderstanding the problem, though— is there something about fetching the MatrixBlockModel elements that appears to be going wrong? As a general rule, I try and avoid using any Twig/Craft reserved words/terms for variable names, but I don't think that's the root of the problem here…

|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.