0

How to display all fields belonging to current entry in Craft 4? There is an answer to the similar question for Craft 3, but it does not work for Craft 4:

{# 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) %}

    {# print the field handle and the field content  #}
    {{ field.handle }} : {{ entry.getContent()[field.handle] }}

{% endfor %}

I tried to find corresponding methods at the docs, but it seems the versions differ too much.

1 Answer 1

3

With Craft 4, you may need to be a little more specific about what you intend to get. The field layout itself can include field layout elements, which can include UI niceties that are not custom fields that take content. It can also include native fields (like Title) and custom fields you’ve added.

Check out the methods on the FieldLayout model. To approximate your Craft 3 example, you could use getCustomFields() instead of getFields() to list the custom fields for the entry:

{% for field in entry.getFieldLayout().getCustomFields() %}
  {{ field.handle }} : {{ entry[field.handle] }}
{% endfor %}

You could list all of the field handles, as they appear in the field layout tabs, like this:

{% for tab in entry.getFieldLayout().getTabs() %}
  {# output tab.name here if you want #}
  {% for element in tab.getElements() %}
    {{ element.attribute }} : {{ entry[element.attribute] }}
  {% endfor %}
{% endfor %}

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.