1

I have a matrix field with an entries field which ties into another matrix field. Is this possible? Here's what I have so far...

{% for block in entry.contentMatrix %}

  {# Tabs Block Type #}
  {% if block.type == "tabs" %}
    {% set entries = craft.entries.relatedTo(block.tabEntry) %}

    {% for entry in entries %}
      {# Matrix Content #}
      {% for block in entry.tabsMatrix.type('videoChannel, videoPlayer, copy') %}
        {{ block.title }}
      {% endfor %}
    {% endfor %}
  {% endif %}

{% endfor %}

This does not produce an error, just nothing.

4
  • As an aside, is the switch tag better suited than the conditional for looping through the block types? Is there a performance difference? – Ben Shoults Jan 26 '16 at 22:34
  • 1
    No performance difference in conditional vs switch. Switch is just a bit cleaner sometimes. – Douglas McDonald Jan 26 '16 at 23:14
  • 2
    Your code looks ok to me. You might want to dump variables (or test length) within each loop to see if your getting results. i.e. {{ dump(entries) }} or {{ entries|length }} – Douglas McDonald Jan 26 '16 at 23:27
  • The dump helped me realize that nothing was getting passed before getting to the matrix loop...thanks – Ben Shoults Jan 27 '16 at 16:10
1

Do you need to filter by block type?

Maybe something like this:

{% for block in entry.contentMatrix %}

    {# Tabs Block Type #}
    {% if block.type == "tabs" %}

        {% set entries = craft.entries.relatedTo(block.tabEntry) %}

        {% for entry in entries %}
            {# Matrix Content #}
            {% for content in entry.tabsMatrix.type('videoChannel, videoPlayer, copy') %}

                {% if content.type == "videoChannel" %}
                    {{ content.title }}
                {% elseif content.type == "videoPlayer" %}
                    {{ content.url }}
                {% else %}
                    {{ content.text }}
                {% endif %}

            {% endfor %}

        {% endfor %}

    {% endif %}

{% endfor %}

Note: Matrix Content vars (*.title, *url, *text) are just examples.

1
  • My understanding is that we can add type to the for to choose multiple matrix types. – Ben Shoults Jan 27 '16 at 16:04
1

According to the documentation, I just have to pass the matrix fields handle block instead of block.tabEntry. Craft will automatically look for a relationship field (how smart). If you have multiple relation fields, you have to use the dot notation with the field name. (→ see Relations – Going Through Matrix)

I still don't understand why using the dot notation would break it, but it apparently does. I checked my field name several times. Anywho, thanks for the help all!

{% for block in entry.contentMatrix %}

  {# Tabs Block Type #}
  {% if block.type == "tabs" %}
  {% set entries = craft.entries.relatedTo(block) %}

    {% for entry in entries %}
      {# Matrix Content #}
      {% for block in entry.tabsMatrix.type('videoChannel, videoPlayer, copy') %}
        {{ block.title }}
      {% endfor %}
    {% endfor %}
  {% endif %}

{% endfor %}

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.