StoutLogic / acf-builder

An Advanced Custom Field Configuration Builder
GNU General Public License v2.0
794 stars 61 forks source link

Add option to disable conditional field check. #63

Open christianmagill opened 6 years ago

christianmagill commented 6 years ago

I have a few ACF front end forms that draw from multiple field groups.

I'd like to be able to do conditional logic between the different field groups. However, I get stuck with an error that ACF Builder throws when the field does not exist in the group.

It would be really awesome if there could be an additional parameter on conditional() that would ignore this check.

If I alter the array to add my conditionals they work as expected across field groups.

Thanks!

stevep commented 6 years ago

The issue is the way ACF builder creates unique field keys. Instead of being randomly generated like how ACF proper does it, ACF builder name spaces field keys by their field group. This means when referring to a field by name in a conditional, it can never reference a field outside its field group.

Recently on the dev branch, I added a setCustomKey method to the FieldBuilder to manage field key uniqueness manually. This is a result from issue #61. Try setting a custom key for the field you want to reference in your cross field group conditional, then in that condition instead of referencing the field by name, reference it by your custom key.

$builderA = FieldsBuilder('group_a');
$builderA
    ->addTrueFalse('enable_group_b_item')
        ->setCustomKey('my_unique_field_key');

$builderB = FieldsBuilder('group_b');
$builderB
    ->addText('group_b_text')
        ->conditional('my_unique_field_key', '==', '1');

In this case you could set the custom key to be enable_group_b_item as well, and you could still use it in the conditional method call. But be aware if you use $builderA in multiple places using addFields you'll have 2 fields with the same key and things won't work as expected unless you manually give the copy a new key.

That said I haven't tested this, but change the "stoutlogic/acf-builder" version to dev-develop in your project's composer.json and see if it works. You might have to set minimum-stability to dev as well.

christianmagill commented 6 years ago

Thanks, I'll give it a try.