<p>I have another approach that I prefer. This does not involve multiple matrixes, but instead clever templating. Start with your main matrix. Add the blocks from your slide matrix to the main matrix. When editing the matrix, add slide blocks underneath each Content Slider block. When you're done with slides just add the other blocks as usual.</p>

<p><strong>The template is where the magic happens!</strong></p>

<pre><code>{% case "contentSlider" %}
    {% set openSlider = true %}
    &lt;div class="contentSlider"&gt;

{% case "imageSlide" %}
    {% if openSlider %}
        {{ block.heading }}
    {% endif %}

{% case "richTextBlock" %}
    {% if openSlider %}
        {% set openSlider = false %}
        &lt;/div&gt;
    {% endif %}
    {{ block.richTextField }}
</code></pre>

<p>So what's happening here is when you start a new slider, the openSlider variable tells other blocks when there's a slider being built. When a non-slide shows up, it closes the slider before adding its own content. For slides, they only show their content when there's an open slider. This keeps them from showing up elsewhere in case they are misplaced.</p>

CC BY-SA 3.0
2015-02-10T08:09:04.453Z
I guess this will work as well, thanks for sharing your approach! For my current project I'll stick with @carlcs solution, but your approach can be a better fit for another site. Does it work well with Live Preview?
Alf Vestre
CC BY-SA 3.0
2015-02-10T13:49:00.073Z
I haven't tested it (I don't use Live Preview much), but I don't see why it wouldn't work.
Bryan Redeagle
CC BY-SA 3.0
2015-02-09T19:53:11.067Z
2015-02-09T19:53:11.067Z
Bryan Redeagle
<p>Here is my initial code with @carlcs solution included:</p>

<pre><code>{% case "contentSlider" %}
    {% set tagsToFind = block.showTags %}

    {# Match the tags between the current matrix block and another matrix field  #}
    {% set relatedSlides = entry.slidesMatrix.relatedTo({
        targetElement: tagsToFind
    }) %}

    {% for slide in relatedSlides %}
        Matched Tag in: {{ slide.heading }}&lt;br&gt;
    {% endfor %}
</code></pre>

<p>This works great, but I still have my fingers crossed for native support for nested matrix support in Craft! It would be a killer feature and is on the top of my wish-list.</p>

CC BY-SA 3.0
2015-02-09T18:37:54.153Z
2015-02-09T18:37:54.153Z
Alf Vestre
<p>I just tried to replicate your setup (in simplified form), because I thought this is in fact an interesting and creative approach
to do Matrix nesting that might come handy.</p>

<p>Here's my code that works great for me. I hope it helps, even though I actually don't see the difference to what you have?!</p>

<pre><code>{# Go through our Main Matrix blocks #}
{% for block in entry.mainMatrix.type('mainRelationsBlock') %}

    {# Get the tags selected in the Main Matrix block #}
    {% set tagsToFind = block.mainTags %}

    {# Try filtering Matrix blocks of our Sub Matrix using `relatedTo` #}
    {% set relatedBlocks = entry.subMatrix.relatedTo({
        targetElement: tagsToFind
    }) %}

    {# Show them #}
    {% for relatedBlock in relatedBlocks %}
        &lt;li&gt;{{ relatedBlock.text }}&lt;/li&gt;
    {% endfor %}

{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-02-09T18:16:54.467Z
This line did the magic:  
`{% set relatedBlocks = entry.subMatrix.relatedTo({ targetElement: tagsToFind }) %}`
Alf Vestre
CC BY-SA 3.0
2015-02-09T18:43:03.790Z
I can't see how this code `{% set relatedBlocks = entry.subMatrix.relatedTo({ targetElement: tagsToFind }) %}` targets the `subMatrix.tags`field it seems to target the entire `subMatrix`. If you can explain this, it might be easier to understand how this work?
Alf Vestre
CC BY-SA 3.0
2015-02-09T18:57:59.960Z
@AlfVestre sorry, I can't explain this to you and there's no word on it in the docs. I actually thought that there's only a very limited set of filters available to filter Matrix blocks: `type('myBlockType')`, `first()`, `nth('3')` and since Craft 2.3 the new field filter `myLightSwitchField('1')` or `myDateTimeField('> 2014-01-01')`. I did not expect `relatedTo()` to work at all, but it seems like it gets all the Matrix blocks that are related. Nice find!
carlcs
CC BY-SA 3.0
2015-02-09T09:23:46.587Z
2015-02-09T09:23:46.587Z
carlcs
<p>I have an entry with two matrix fields and I want to use relations between them using Tags to create a fake nested Matrix functionality for one block type. The "main" matrix field is for creating a typical banded page (several horisontal stripes of content). One of the blocks in this main matrix has to contain a contentSlider, and since in this specific case adding an Assets field is not sufficient we have to nest or merge two matrix fields.</p>

<p>While looping through the main matrix field using the Switch method I have a block type named "contentSlider". In this block type I have a Tags field named "showTags". In my second matrix field named "slidesMatrix" I have a field named "tags". I want to find blocks in the second matrix (slidesMatrix) related to the current block in the main matrix.</p>

<hr>

<p>Here is my attempt, it is not giving me any errors but it is not outputting the "slide.heading" field either so something must be wrong:</p>

<pre><code>{% case "contentSlider" %}
    {% set tagsToFind = block.showTags %}
    {% set relatedSlides = entry.slidesMatrix.tags.relatedTo(tagsToFind).order('score').find() %}
    {% for slide in relatedSlides %}
        Matched Tag in: {{ slide.heading }}&lt;br&gt;
    {% endfor %}
</code></pre>

<hr>

<p>This alternative code works, but it is only matching the first Tag from the current block to all the Tags in the second matrix block (slidesMatrix.tags) and switching to one of the native Craft methods like "relatedTo" would be a more powerful solution.</p>

<pre><code>{% case "contentSlider" %}
    {% set tagsToFind = block.showTags %}
    {% set tagToFind = tagsToFind.first.title %}
    {% for slide in entry.slidesMatrix if tagToFind in slide.tags %}
        Matched Tag in: {{ slide.heading }}&lt;br&gt;
    {% endfor %}            
</code></pre>

<hr>

<p>To sum this up into one question;<br>
<strong>How can I loop through a matrix field filtered by Tags contained in a tags fields inside the matrix blocks I want to loop through, using a native Craft feature like 'relatedTo'?</strong></p>

CC BY-SA 3.0
2015-02-09T02:37:08.223Z
Why isn't an Assets field sufficient? You can have custom fields on assets.
Bryan Redeagle
CC BY-SA 3.0
2015-02-09T07:36:03.317Z
We need some more fields in addition to the asset field in the slides. I know we can make all the fields we need on the asset itself, but if I'm not wrong those fields are not unique per entry they are placed in which mean they have to be the same every time they are used (global). For some slides there might not be an image at all. This is more a "content slider" than an image slideshow. It must support an header, image, richtext body and a entry-link with a related textfield (to be output as a button with custom text).
Alf Vestre
CC BY-SA 3.0
2015-02-09T12:18:49.850Z
If you remove `tags.relatedTo(tagsToFind).order('score').find()` altogether and only do `{% set relatedSlides = entry.slidesMatrix %}`, does it return all slides?
carlcs
CC BY-SA 3.0
2015-02-09T17:30:09.327Z
@carlcs, yes your code return all slides.
Alf Vestre
<matrix><relations><tags>
Nested Matrix functionality for one block type using Tags
# Nested Matrix functionality for one block type using Tags

(Asked by: username_0 on 2015-02-08)

I have an entry with two matrix fields and I want to use relations between them using Tags to create a fake nested Matrix functionality for one block type. The "main" matrix field is for creating a typical banded page (several horisontal stripes of content). One of the blocks in this main matrix has to contain a contentSlider, and since in this specific case adding an Assets field is not sufficient we have to nest or merge two matrix fields.
While looping through the main matrix field using the Switch method I have a block type named "contentSlider". In this block type I have a Tags field named "showTags". In my second matrix field named "slidesMatrix" I have a field named "tags". I want to find blocks in the second matrix (slidesMatrix) related to the current block in the main matrix.

Here is my attempt, it is not giving me any errors but it is not outputting the "slide.heading" field either so something must be wrong:
{% case "contentSlider" %}
    {% set tagsToFind = block.showTags %}
    {% set relatedSlides = entry.slidesMatrix.tags.relatedTo(tagsToFind).order('score').find() %}
    {% for slide in relatedSlides %}
        Matched Tag in: {{ slide.heading }}
    {% endfor %}


This alternative code works, but it is only matching the first Tag from the current block to all the Tags in the second matrix block (slidesMatrix.tags) and switching to one of the native Craft methods like "relatedTo" would be a more powerful solution.
{% case "contentSlider" %}
    {% set tagsToFind = block.showTags %}
    {% set tagToFind = tagsToFind.first.title %}
    {% for slide in entry.slidesMatrix if tagToFind in slide.tags %}
        Matched Tag in: {{ slide.heading }}
    {% endfor %}            


To sum this up into one question;
How can I loop through a matrix field filtered by Tags contained in a tags fields inside the matrix blocks I want to loop through, using a native Craft feature like 'relatedTo'?

**Tags:** matrix, relations, tags
> **Comments:**
> **username_1**: Why isn't an Assets field sufficient? You can have custom fields on assets.  
> **username_0**: We need some more fields in addition to the asset field in the slides. I know we can make all the fields we need on the asset itself, but if I'm not wrong those fields are not unique per entry they are placed in which mean they have to be the same every time they are used (global). For some slides there might not be an image at all. This is more a "content slider" than an image slideshow. It must support an header, image, richtext body and a entry-link with a related textfield (to be output as a button with custom text).  
> **username_2**: If you remove `tags.relatedTo(tagsToFind).order('score').find()` altogether and only do `{% set relatedSlides = entry.slidesMatrix %}`, does it return all slides?  
> **username_0**: @carlcs, yes your code return all slides.  

---

## 3 answers

### Answer by username_2 on 2015-02-09. Score: 4 | Accepted answer

I just tried to replicate your setup (in simplified form), because I thought this is in fact an interesting and creative approach
to do Matrix nesting that might come handy.
Here's my code that works great for me. I hope it helps, even though I actually don't see the difference to what you have?!
{# Go through our Main Matrix blocks #}
{% for block in entry.mainMatrix.type('mainRelationsBlock') %}

    {# Get the tags selected in the Main Matrix block #}
    {% set tagsToFind = block.mainTags %}

    {# Try filtering Matrix blocks of our Sub Matrix using `relatedTo` #}
    {% set relatedBlocks = entry.subMatrix.relatedTo({
        targetElement: tagsToFind
    }) %}

    {# Show them #}
    {% for relatedBlock in relatedBlocks %}
        {{ relatedBlock.text }}
    {% endfor %}

{% endfor %}

> **Comments:**
> **username_0**: This line did the magic:   `{% set relatedBlocks = entry.subMatrix.relatedTo({ targetElement: tagsToFind }) %}`  
> **username_0**: I can't see how this code `{% set relatedBlocks = entry.subMatrix.relatedTo({ targetElement: tagsToFind }) %}` targets the `subMatrix.tags`field it seems to target the entire `subMatrix`. If you can explain this, it might be easier to understand how this work?  
> **username_2**: @AlfVestre sorry, I can't explain this to you and there's no word on it in the docs. I actually thought that there's only a very limited set of filters available to filter Matrix blocks: `type('myBlockType')`, `first()`, `nth('3')` and since Craft 2.3 the new field filter `myLightSwitchField('1')` or `myDateTimeField('> 2014-01-01')`. I did not expect `relatedTo()` to work at all, but it seems like it gets all the Matrix blocks that are related. Nice find!  

---

### Answer by username_1 on 2015-02-09. Score: 1

I have another approach that I prefer. This does not involve multiple matrixes, but instead clever templating. Start with your main matrix. Add the blocks from your slide matrix to the main matrix. When editing the matrix, add slide blocks underneath each Content Slider block. When you're done with slides just add the other blocks as usual.
The template is where the magic happens!
{% case "contentSlider" %}
    {% set openSlider = true %}
    

{% case "imageSlide" %}
    {% if openSlider %}
        {{ block.heading }}
    {% endif %}

{% case "richTextBlock" %}
    {% if openSlider %}
        {% set openSlider = false %}
        
    {% endif %}
    {{ block.richTextField }}

So what's happening here is when you start a new slider, the openSlider variable tells other blocks when there's a slider being built. When a non-slide shows up, it closes the slider before adding its own content. For slides, they only show their content when there's an open slider. This keeps them from showing up elsewhere in case they are misplaced.

> **Comments:**
> **username_0**: I guess this will work as well, thanks for sharing your approach! For my current project I'll stick with @carlcs solution, but your approach can be a better fit for another site. Does it work well with Live Preview?  
> **username_1**: I haven't tested it (I don't use Live Preview much), but I don't see why it wouldn't work.  

---

### Answer by username_0 on 2015-02-09. Score: 0

Here is my initial code with @carlcs solution included:
{% case "contentSlider" %}
    {% set tagsToFind = block.showTags %}

    {# Match the tags between the current matrix block and another matrix field  #}
    {% set relatedSlides = entry.slidesMatrix.relatedTo({
        targetElement: tagsToFind
    }) %}

    {% for slide in relatedSlides %}
        Matched Tag in: {{ slide.heading }}
    {% endfor %}

This works great, but I still have my fingers crossed for native support for nested matrix support in Craft! It would be a killer feature and is on the top of my wish-list.


---

