2

The question says it all.

I have:

  • A Matrix field, with
  • A subfield with the type table
  • Two columns within that table of which I want to show the column headers in my template

Is that possible and if yes, how?

15

You don't get table headings with the entry model data (use the dump() function while in dev mode to see yourself). But there's the method getFieldByHandle() that returns a field model containing info to any field settings.

This is how you access the headings of a table field

Craft 2

{% set field = craft.fields.getFieldByHandle('myTableField') %}

{% for column in field.settings.columns %}
    <li>{{ column.heading }}</li>
{% endfor %}

Craft 3

{% set field = craft.app.fields.getFieldByHandle('myTableField') %}

{% for column in field.settings.columns %}
    <li>{{ column.heading }}</li>
{% endfor %}

Table field in Matrix block:

As fields within matrix blocks don't necessarily have unique field handles, you can't use the getFieldByHandle() method to get the field model. Use the unique field ID with the method getFieldById() instead. To look that ID up, open up the source code of the field in the matrix field's field settings. There's a div with the class matrixconfigitem and a data attribute data-id, which is the ID you are looking for.

(PS: use the code in your matrix loop for the block type containing your table "sub" field to output in the right place, but it actually works everywhere!)

| improve this answer | |
  • what would then be my handle? Tried it with the handle value of the subfield of the blocktype, get an error because the field cannot be found {{ dump(field) }} returns NULL. – Paul Aug 26 '14 at 7:12
  • Yep sorry. It's a little different with fields in matrix blocks, @paul. – carlcs Aug 26 '14 at 8:21
  • Thought so, already tried your approach ;) – Paul Aug 26 '14 at 9:32
  • Does the smily mean the approach works for you? If yes, what about accepting / up voting the answer? – carlcs Aug 26 '14 at 9:36
  • No, the smily means that I had already tried the approach you mentioned and it didn't work for me. I would have gladly upvoted your answer if it would have been the solution I was after, of course. – Paul Aug 26 '14 at 9:53

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.