1

Newbie here, still grappling with basics, let alone elegance. My aim is to pull content from four different fields held in two different Matrix blocks and pass the output in a nice human readable format to populate the meta-description tag in my layouts and elsewhere later on the entry page.

I'm getting close but still no beer. I'm having trouble with merging everything. Only one or the other MatrixHandles outputs the correct content:

{# Fetch MatrixBlock fields content for artistName and birthDeathLocationDate #}
{% set artistNameQuery = craft.matrixBlocks()
    .owner(entry)
    .field('artistNameBirthDeath')
    .type('artist') %}

{# Fetch MatrixBlock fields content for materials and dimensions #}
{% set artworkMaterialsQuery = craft.matrixBlocks()
    .owner(entry)
    .field('artworkDateMatDim')
    .type('artworkDetails') %}    

{# Try to merge the two above queries, with some merge voodoo #}
{% set artNameMat = artistNameQuery|merge(artworkMaterialsQuery) %}

{# Don't know why but this helps trigger the below, and sends it to my base.twig layout tmp #}
{% set metaDescribe = '' %} 

{# Print all of the above into a pretty, human/machine readable format for re-use in base.twig or elsewhere #}
{% for block in artNameMat %}
    {% set metaDescribe = block.artistName ~ ' ' ~ '(' ~ block.birthDeathLocationDate ~ ')' ~ ' ' ~ block.materials ~ ' ' ~  block.dimensions  %}
{% endfor %}

And then over in my base.twig template I've got:

<meta name="description" content="
{% if metaDescribe is defined %}{{ metaDescribe }}{% else %} no meta desc {% endif %}" />

So far it's outputting only the artistNameQuery. If I edit my not totally working merge line to just {% set artNameMat = artworkMaterialsQuery %}, it too sends the correct content for that Matrix handle/block to my base.twig.

Other notes. The above code is written within the template for displaying single artists' artwork (entry), outside of the {% block main %}. Running Craft 3.5.11.1.

Any pointers would be much appreciated!

Update:

I just tried making it all a lot more cleaner / elegant replacing this:

{% set artistNameQuery = craft.matrixBlocks()
    .owner(entry)
    .field('artistNameBirthDeath')
    .type('artist') %}

{% set artworkMaterialsQuery = craft.matrixBlocks()
    .owner(entry)
    .field('artworkDateMatDim')
    .type('artworkDetails') %} 

With this:

{% set artNameMat = entry.artistNameBirthDeath.type('artist').all()
    |merge(entry.artworkDateMatDim.type('artworkDetails').all())
%}

But I'm still getting only "one half" of the Matrix fields that I'm after.

2

I think your problem might be that you're overwriting the metaDescribe variable on each iteration of your for loop at the end of your first example. Given that you know you'll only be dealing with two blocks, you could try replacing the loop with something like this:

{% set metaDescribe = artNameMat[0].artistName ~ ' ' ~ '(' ~ artNameMat[0].birthDeathLocationDate ~ ')' ~ ' ' ~ artNameMat[1].materials ~ ' ' ~  artNameMat[1].dimensions  %}

[Aside: it may also be worth revisiting your content model, as it sounds a little convoluted! Investigate the possibilities of moving some pieces of content out into dedicated sections or category groups and using relationships to tie everything together.]

1
  • That nailed it and served to show how much more I need to learn! Much appreciated. And yes, coming from EE I have yet to uncover the full depth of craft / twig, and rid myself of my old model of thinking. But as a first project... I'm bound to hit my head somewhere!
    – Seb
    Oct 1 '20 at 15:23

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.