Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Allow Location Array #146

Open
markmercier opened this issue Nov 8, 2021 · 0 comments
Open

Enhancement: Allow Location Array #146

markmercier opened this issue Nov 8, 2021 · 0 comments

Comments

@markmercier
Copy link

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:

  • if parameter 1 provided to setLocation is an array, pass that array directly as the 'location' parameter; ignore any subsequent ->and or ->or functions
  • else continue as usual (require 3 parameters, build the 'location' array with the ->and and ->or functions)

Thanks for the consideration!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant