<p>I don't really understand your current code or the intention behind it, but if the use case is to determine if a custom field is conditionally hidden or not for a particular entry, you can pull all the visible fields from the entry's field layout like this:</p>
<pre><code>{% set visibleFields = entry.fieldLayout.getVisibleCustomFields(entry)|column('handle') %}

...

{% if 'someLightswitchField' in visibleFields %}
    {# Display sub fields #}
{% endif %}
</code></pre>
<p>If you prefer or need to get an array of all hidden fields, there's no method for that, but it can be done by first getting all the custom fields, and then filtering out the visible ones:</p>
<pre><code>{% set visibleFields = entry.fieldLayout.getVisibleCustomFields(entry) %}
{% set hiddenFields = entry.fieldLayout.getCustomFields()|without(visibleFields)|column('handle') %}

{% if 'someLightswitchField' not in hiddenFields %}
    {# Display sub fields #}
{% endif %}
</code></pre>

CC BY-SA 4.0
2023-03-16T11:07:21.393Z
Thank you, it works to get the conditionally hidden fields, but it doesn't get the conditional rules. How to?
Romain P.
CC BY-SA 4.0
2023-03-15T11:52:11.763Z
2023-03-15T11:52:11.763Z
Mats Mikkel Rummelhoff
<p>I've just updated from Craft 3 to Craft 4. I was previously using the <a href="https://github.com/mmikkel/Reasons-Craft3" rel="nofollow noreferrer">Reasons plugin</a> for CP but also front-end purpose. On front-end, I was printing field layouts and show/hide the fields based on the same Reasons conditions found in the CP.</p>
<p>Unfortunately, this plugin has been stopped for Craft 4 in profit of the native conditional fields. However, I don't find out the equivalent of what I could achieve using Reasons.</p>
<p>This is what I was doing in Craft 3 + Reasons:</p>
<pre><code>{% set conditionals = [] %}
% for key, value in craft.app.getPlugins().getPlugin('reasons').reasons.getData().conditionals  %}
    {%
        if 'section' in key
        and (key | split(':') | last) | number_format == craft.app.sections.getSectionByHandle('companies').id ?? -1
    %}
        {% set conditionals = value %}
    {% endif %}
{% endfor %}

{% set rules = [] %}
{% set syncedFields = [] %}
{% set syncedHandles = [] %}
{% set hiddenFields = [] %}
{% for id, condition in conditionals %}
    {% set syncedFields = syncedFields | merge([condition.0.0.0.fieldId ?? null]) | filter | unique %}
    {% set handle = craft.app.fields.getFieldById(condition.0.0.0.fieldId ?? null).handle ?? null %}
    {% set syncedHandles = syncedHandles | merge([&quot;{handle}: '{value}'&quot; | t({
        handle: handle,
        value: company[handle].value ?? company[handle] ?? null
    })]) | filter | unique %}
    {% set hiddenFields = hiddenFields | merge([id ?? null]) | filter %}
    {% set condition = {
        sync: condition.0.0.0.fieldId ?? null,
        field: id ?? null,
        rule: '{compare} {value}' | t({
            compare: condition.0.0.0.compare,
            value: condition.0.0.0.value
        })
    } %}
{% endfor %}
</code></pre>
<p>In my case, the intended context here is quite simple: it's checking a Lightswitch field from another field. If the main field is switched on, subfields will be shown.</p>
<p>What would be the equivalent in Craft 4?</p>

<templating><conditional><entries-field><craft4><field-layout-designer>
Update from Craft 3 + Reasons to Craft 4: how to get fields' conditions from template?
# Update from Craft 3 + Reasons to Craft 4: how to get fields' conditions from template?

(Asked by: username_0 on 2023-03-15)

I've just updated from Craft 3 to Craft 4. I was previously using the Reasons plugin for CP but also front-end purpose. On front-end, I was printing field layouts and show/hide the fields based on the same Reasons conditions found in the CP.
Unfortunately, this plugin has been stopped for Craft 4 in profit of the native conditional fields. However, I don't find out the equivalent of what I could achieve using Reasons.
This is what I was doing in Craft 3 + Reasons:
{% set conditionals = [] %}
% for key, value in craft.app.getPlugins().getPlugin('reasons').reasons.getData().conditionals  %}
    {%
        if 'section' in key
        and (key | split(':') | last) | number_format == craft.app.sections.getSectionByHandle('companies').id ?? -1
    %}
        {% set conditionals = value %}
    {% endif %}
{% endfor %}

{% set rules = [] %}
{% set syncedFields = [] %}
{% set syncedHandles = [] %}
{% set hiddenFields = [] %}
{% for id, condition in conditionals %}
    {% set syncedFields = syncedFields | merge([condition.0.0.0.fieldId ?? null]) | filter | unique %}
    {% set handle = craft.app.fields.getFieldById(condition.0.0.0.fieldId ?? null).handle ?? null %}
    {% set syncedHandles = syncedHandles | merge(["{handle}: '{value}'" | t({
        handle: handle,
        value: company[handle].value ?? company[handle] ?? null
    })]) | filter | unique %}
    {% set hiddenFields = hiddenFields | merge([id ?? null]) | filter %}
    {% set condition = {
        sync: condition.0.0.0.fieldId ?? null,
        field: id ?? null,
        rule: '{compare} {value}' | t({
            compare: condition.0.0.0.compare,
            value: condition.0.0.0.value
        })
    } %}
{% endfor %}

In my case, the intended context here is quite simple: it's checking a Lightswitch field from another field. If the main field is switched on, subfields will be shown.
What would be the equivalent in Craft 4?

**Tags:** templating, conditional, entries-field, craft4, field-layout-designer


---

## 1 answers

### Answer by username_1 on 2023-03-15. Score: 1

I don't really understand your current code or the intention behind it, but if the use case is to determine if a custom field is conditionally hidden or not for a particular entry, you can pull all the visible fields from the entry's field layout like this:
{% set visibleFields = entry.fieldLayout.getVisibleCustomFields(entry)|column('handle') %}

...

{% if 'someLightswitchField' in visibleFields %}
    {# Display sub fields #}
{% endif %}

If you prefer or need to get an array of all hidden fields, there's no method for that, but it can be done by first getting all the custom fields, and then filtering out the visible ones:
{% set visibleFields = entry.fieldLayout.getVisibleCustomFields(entry) %}
{% set hiddenFields = entry.fieldLayout.getCustomFields()|without(visibleFields)|column('handle') %}

{% if 'someLightswitchField' not in hiddenFields %}
    {# Display sub fields #}
{% endif %}

> **Comments:**
> **username_0**: Thank you, it works to get the conditionally hidden fields, but it doesn't get the conditional rules. How to?  

---

