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?

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()

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.