1

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?

1 Answer 1

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 %}
1
  • Thank you, it works to get the conditionally hidden fields, but it doesn't get the conditional rules. How to?
    – Romain P.
    Commented Mar 16, 2023 at 11:07

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.