StoutLogic / acf-builder

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

Enhancement: Allow Location Array #146

Open markmercier opened 2 years ago

markmercier commented 2 years ago

There are certain limitations on what ACF locations can be accomplished programmatically using the ->setLocation() function along with the ->and() and ->or() additions. Being able to pass a complete array to ->setLocation() would allow significantly more flexibility.

For example, I'm currently trying to build 'Bulk Add [post_type]' admin pages for each of my custom post types, and I have an ACF fieldset I want on each of these pages. Originally I tried:

/* manual for demonstration, but would be generated programmatically */
$post_types = [
    'team',
    'testimonial',
];

foreach($post_types as $post_type) {
    $bulk_add->setLocation('options_page', '==', 'bulk-add-' . $post_type);
}

This doesn't work, as each iteration through the array overwrites the location, so the field only appears on the final array item. I also can't do something like:

$bulk_add->setLocation('options_page', '==', 'bulk-add-post');
foreach($post_types as $post_type) {
    $bulk_add->or('options_page', '==', 'bulk-add-' . $post_type);
}

as I get a no such function: or error, since we're out of the setLocation context.

A solution that would be very extensible would be to allow passing an entire 'location' array (exactly as ACF expects it) to the setLocation function, in lieu of the 3 parameters it expects. That way, I could do something like this:

$post_types = [
    'testimonial',
    'team',
];

/* set blank array for population */
$location_array = array();

/* loop through the post types to add to array */
foreach($post_types as $post_type) {
    /* each of these is a new 'and' argument in ACF */
    array_push($location_array,
        array(
            array(
                'param' => 'options_page',
                'operator' => '==',
                'value' => 'bulk-add-' . $post_type,
            )
        )
    );
}

/* (this locations array now looks like:) */
$location_array_demo = array(
    array(
        array(
            'param' => 'options_page',
            'operator' => '==',
            'value' => 'bulk-add-testimonial',
        ),
        array(
            'param' => 'options_page',
            'operator' => '==',
            'value' => 'bulk-add-team',
        )
    )
);

/* set the location of the field set to the location array */
$bulk_add->setLocation($location_array);

I'd think implementing this would be a matter of:

Thanks for the consideration!