3

For a plugin, I need to check if a certain field exists in a section. How can i manage that? I'm using the settings section from the plugin, looping through all sections by craft()->sections->getAllSections() and in the template with the following code:

{% for section in sections %}
    {% if section['specificfield'] is not defined %}
        do something
    {% endif %}
{% endfor %}

This doesn't seem to be working. Any ideas on how to check correctly?

chris
  • 906
  • 6
  • 10

1 Answers1

3

The solution is to getEntryTypes() from your section and then access the getFieldLayout() to get to the getFields() (thanks again @Khalwat!). Example:

{% for section in sections %}
    {% if section.getEntryTypes()[0].getFieldLayout().getFields()[0].getField().handle == 'specificfield' %}
        do something
    {% endif %}
{% endfor %}

Notice though that I specifically search for the first occurances, in (most) other cases you should foreach the getEntryTypes() and getFields()

chris
  • 906
  • 6
  • 10