1

Using Craft 3.

I created the following field:

myMatrix which has Field Type = matrix and is part of the group myGroup

This is composed of one block:

myMatrixBlock with 2 fields:

myMatrixField1

myMatrixField2

I have assigned the myMatrix field to Users using the control panel (user settings >> fields).

How do I

  1. Access the data in myMatrix from my twig template? I'd like to get all the data in this matrix for the current user and loop through it and display data for each myMatrixField1 & Field2. I can't figure out how to retrieve myMatrix field.

  2. How do I create a new record in the myMatrix field? I'd like to create a new entry in the myMatrix field and store data for the current user in each of the fields.

I've been searching at this for several hours now ... I feel this must be VERY easy but I can't seem to figure it out. Also, if you have a link to documentation on functions/variables that are available for each object at the template level I'd be grateful. I can't seem to find that in the docs either.

Thank you!

1

You need to use 'currentUser' to access the field. So;

{% for block in currentUser.myMatrix %}
  {{ block.myMatrixField1 }}
  {{ block.myMatrixField2 }}
{% endfor %}

As for creating a new record in the matrix (I'm guessing you mean from the front end) then you need to create a form that submits the current matrix data alongside your new block data. This isn't super straight forward, but basically you need to create a user profile form that submits the current matrix data alongside the new data.

| improve this answer | |
  • Very helpful. Thank you!! – Erant Oct 19 '19 at 6:00

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.