StoutLogic / acf-builder

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

Invalid argument supplied for foreach() in .../stoutlogic/acf-builder/src/Transform/RecursiveTransform.php on line 34 (v1.12.0) #160

Closed timlogemann closed 2 years ago

timlogemann commented 2 years ago

I am encountering a thrown error while trying to create a field group with a taxonomy field. I tried printing the $config variable in the transform() function of the RecursiveTransform class and it seems that at some point it tries to pass 0 as $config.

This is the full builder code:

use StoutLogic\AcfBuilder\FieldsBuilder;

$builder = new FieldsBuilder('taxonomy', ['position' => 'side']);

$builder
    ->addTaxonomy('theme', [
        'label' => 'Thema',
        'instructions' => '',
        'required' => 1,
        'conditional_logic' => 0,
        'wrapper' => [
            'width' => '',
            'class' => '',
            'id' => '',
        ],
        'taxonomy' => 'theme',
        'field_type' => 'select',
        'allow_null' => 0,
        'add_term' => 0,
        'save_terms' => 0,
        'load_terms' => 0,
        'return_format' => 'id',
        'multiple' => 0,
    ])
    ->setLocation('post_type', '==', 'excercise');

add_action('acf/init', function () use ($builder) {
    acf_add_local_field_group($builder->build());
});

It really has to do with the taxonomy field, because as soon as I remove the addTaxonomy the error message disappears.

stevep commented 2 years ago

@timlogemann I haven't had a chance to test this code yet, but I have a hunch that removing 'conditional_logic' => 0, will fix the issue. Try it and let me know. I would also recommend removing any additional default config that isn't necessary.

timlogemann commented 2 years ago

Good hunch! that did remove the error. I copied this exact field code from the documentation wiki, where this 'conditional_logic' => 0 was included. Maybe it should be removed from there? If you agree I can make a PR for this

stevep commented 2 years ago

@timlogemann thanks for pointing that out. I removed the conditional_logic key from the examples and added a note that says these are the default attribute values for reference only, and to only include the ones you're changing