AdvancedCustomFields / acf

Advanced Custom Fields
http://advancedcustomfields.com/
823 stars 168 forks source link

When returning false in acf/load_field or acf/prepare_field multiple warnings occur #901

Closed sebastianthulin closed 3 months ago

sebastianthulin commented 3 months ago

Describe the bug When returning false in acf/load_field or acf/prepare_field multiple warnings occur. This is due to the excptectation of an array, instead of a bool.

This method is the documentated way to remove a field by filter (se how to reproduce).

Error when using "load_field". Warning: Trying to access array offset on value of type bool in ***/wp-content/plugins/advanced-custom-fields-pro/includes/acf-field-functions.php on line 61 Warning: Trying to access array offset on value of type bool in ***/wp-content/plugins/advanced-custom-fields-pro/includes/acf-field-functions.php on line 62 Warning: Trying to access array offset on value of type bool in ***/wp-content/plugins/advanced-custom-fields-pro/includes/acf-field-functions.php on line 62 Warning: Trying to access array offset on value of type bool in ***/wp-content/plugins/advanced-custom-fields-pro/includes/acf-field-functions.php on line 62 Warning: Trying to access array offset on value of type bool in ***/wp-content/plugins/advanced-custom-fields-pro/pro/fields/class-acf-field-clone.php on line 135

Error when using prepare_field Warning: Trying to access array offset on value of type bool in ***/wp-content/themes/municipio/library/Admin/Acf/ContentType/PrepareField.php on line 38 Warning: Trying to access array offset on value of type bool in ***/wp-content/themes/municipio/library/Admin/Acf/ContentType/PrepareField.php on line 39 Warning: Trying to access array offset on value of type bool in ***/wp-content/themes/municipio/library/Admin/Acf/ContentType/PrepareField.php on line 40

To Reproduce Steps to reproduce the behavior: Implement the "prevent field" example according to the documentation. Found here: https://www.advancedcustomfields.com/resources/acf-prepare_field/#preventing-a-field

Expected behavior The field should be hidden, but not throw any warning messages. The field should be removed from rendering. This error occurs when using the fields in a frontend form.

Version Information:

lgladdy commented 3 months ago

Hey there,

Returning false to prevent a field being displayed is only supported in acf/prepare_field as documented, not load_field. Load field is a much lower level filter which happens earlier in the chain and is required to prime our caches etc. It's not possible to return false.

The errors then occurring in prepare_field seems to be something specific to your theme, specifically in library/Acf/ContentType/PrepareField.php - so I'm not sure this is a bug our side to fix?

If returning false from acf/prepare_field ever generates any warnings in ACF's code, please reopen this issue with more info.

Thanks!