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 }}<br>
{% 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 }}<br>
{% 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.relatedTo(tagsToFind).order('score').find()
altogether and only do{% set relatedSlides = entry.slidesMatrix %}
, does it return all slides?