StoutLogic / acf-builder

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

Using a loop to create conditions for a field #140

Closed marcelo2605 closed 3 years ago

marcelo2605 commented 3 years ago

I'm not sure if it's possible, but I'm trying to figure out how to set some conditions from an array of values.

->addTrueFalse('filter_taxonomy', [
        'label' => 'Filter by taxonomy?',
        'wrapper' => [
            'width' => '33%',
        ],
        'ui' => 1,
    ])
        ->conditional('load_content', '==', '1')

So basically I have a first condition and need to add some other "and" conditions where the values comes from a function.

stevep commented 3 years ago

You could save the field, or conditional to a variable and then add more conditionals to that. Basically since every function call in ACF Builder's fluent api return $this, you can grab a handle at any point.

So something like this should work.


$builder = new FieldsBuilder('banner');

$builder
    ->addText('Title');

$filterTaxonomyField = $builder->addTrueFalse('filter_taxonomy', [
        'label' => 'Filter by taxonomy?',
        'wrapper' => [
            'width' => '33%',
        ],
        'ui' => 1,
    ]);

foreach($condition in $conditions) {
    $filterTaxonomyField
        ->conditional($condition, '==', '1');
}

return $builder->build();
marcelo2605 commented 3 years ago

Thanks @stevep!