2

I have a commissions section in where we have a matrix field where we add the user (from craft.users) and the function of the user.

A user can be in more that one commissions.

On the user profile page, we want to show besides some personal information, all the commissions in which he is active.

For a single relation field (user type field), we have no problem, but I can't get it working with this matrix field.

Matrix field info:

Section: commissionmembers
Blocktype: members
Fields: member (fieldtype users)
function (plain text)

This is a working example with a Users field

    {% for user in craft.users.id( craft.request.lastSegment ).limit(1) %}
    {% set commissions = craft.entries.section('commissions').relatedTo(user) %}
        {% for commission in commissions %}
            {{ commission.title }}
        {% endfor %}
    {% endfor %}

Hope you can point me in the right direction how to get it working with the matrix field

0

1 Answer 1

2

You’d need to query for all Matrix Blocks the user is related to using a Matrix Block ElementCriteriaModel. Unfortunatelly this is not possible from the template level by default, you need to either install the plugin Low Blocks, or write some some lines of PHP in a custom plugin.

Here’s how you would have to set the params for the Low Blocks function:

{% set userId = craft.request.lastSegment %}
{% set user = craft.users.id(userId).first() %}

{% if not user %}
    {% exit 404 %}
{% endif %}

{% set matrixField = craft.fields.getFieldByHandle('commissionmembers') %}

{% set blocks = craft.lowblocks.blocks.fieldId(matrixField.id).type('members').relatedTo({
    targetElement: userId,
    field: 'member',
}) %}

{% for block in blocks %}
    {% set commission = block.getOwner() %}

    {{ commission.title }}
    {{ block.function }}
{% endfor %}
1
  • Hey Carl, thank you so much for your help, it's working perfectly. However there was one thing I had to let out, and that is in the relatedTo, field: 'member'. With only the targetElement: userId it works perfectly. Commented Sep 29, 2016 at 21:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.