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?