1

Ok this is similar to a previously asked question How to display all fields belonging to current entry?

However I'd like to extend this so that it deals with matrix entry types. I have the following but I just need to know how to loop through all the blocks and pull out each blocks entry field handle and content. Any help appreciated.

{# loop through the fields in the entries fieldLayout Model #}
{% for fieldLayoutField in entry.getFieldLayout().getFields() %}

    {# get the field Model from the fieldId #}
    {% set field = craft.fields.getFieldById(fieldLayoutField.fieldId) %}

    {% if field.type == 'Matrix' %}
        {% set matrixHandle = field.handle %}
        {% for field in matrixHandle %}
            ...loop through matrix blocks fields and pull out field handles and content...
        {% endfor %}
    {% else %}
        {# print the field handle and the field content  #}
        {{ field.handle }} : {{ entry.getContent()[field.handle] }}
    {% endif %}

{% endfor %}
5

I find accessing content like this pretty unintuitive, but I think you will need to use the Twig attribute method. Hopefully the below can help keep you moving in the right direction. I've simplified to just accessing the Matrix Field and I've made an assumption that you know the Matrix field handles you will want to access.

{% for fieldLayoutField in entry.getFieldLayout().getFields() %}
  {% set field = craft.fields.getFieldById(fieldLayoutField.fieldId) %}

  {% if field.type == 'Matrix' %}

    {# Dynamically access the Matrix Field on the Entry Model #}
    {% set matrixField = attribute(entry, field.handle) %}
    {% for block in matrixField %}

      {# Output your Matrix Field Handle #}
      {{ field.handle }}

      {# Output your specific fields content #}
      {{ block.fieldHandle }}

    {% endfor %}

  {% endif %}
{% endfor %}

You may also want to take a look at how Craft handles outputting Forms in the CP in the app/templates/_includes folder, starting with forms.html and reviewing the many related files. Doing a task like this cleanly really requires a pretty deep level of abstraction if you don't want to make any assumptions about your variable names.

1

You can use the same syntax on the Matrix block that you're using on the Entry.

{% for fieldLayoutField in entry.getFieldLayout().getFields() %}
  {% set field = craft.fields.getFieldById(fieldLayoutField.fieldId) %}

  {% if field.type == 'Matrix' %}

    {% for block in entry[field.handle] %}
      <tr>

        {# output the name of the matrix field #}
        <td>{{field.name}}</td>

        <td>
          {% for fieldId in block.fieldLayout.fieldIds %}

            {% set field = craft.fields.getFieldById(fieldId) %}

            {# output the name of the nested field and its value #}
            {{field.name}}: {{block[field.handle]}}<br/>

          {% endfor %}
        </td>
      </tr>
    {% endfor %}

  {% endif %}
{% endfor %}

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.