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

Custom sorting/filtering of the Filter Dropdown Options? #378

Open
Muffinman opened this issue May 18, 2023 · 1 comment
Open

Custom sorting/filtering of the Filter Dropdown Options? #378

Muffinman opened this issue May 18, 2023 · 1 comment

Comments

@Muffinman
Copy link

Hi,

We have a client requirement where one dropdown needs to filter the items available in the second. They also need the options very specifically sorting based on an order they enter themselves in the admin. These fields are all ACF fields.

e.g. Selecting an option in Competition here will need to filter the options available in Section

Screenshot 2023-05-18 at 13 11 22

Looking at the filters available for ACP, this almost seems like it's possible with acp/filtering/dropdown_args.

add_filter('acp/filtering/dropdown_args', function (array $data, \ACA\ACF\Column $column) {
    $activeColumns = [];

    if (!empty($_GET['acp_filter']) && !empty($_GET['layout'])) {
        foreach ($_GET['acp_filter'] as $columnName => $value) {
            $activeColumns[$columnName] = [
                'column' => ac_get_column($columnName, $_GET['layout']),
                'value' => $value,
            ];
        }
    }

    // Restrict by active competition
    if ($column->get_meta_key() === 'section') {
        foreach ($activeColumns as $activeColumn) {
            if ($activeColumn['column']->get_meta_key() === 'competition') {
                $data['options'] = array_filter($data['options'], function ($item) use ($activeColumn) {
                    return get_post_meta($item, 'competition', true) === $activeColumn['value'];
                }, ARRAY_FILTER_USE_KEY);
            }
        }
    }

    return $data;
});

However the first thing that happens on page load is that an ajax request to acp_update_filtering_cache which overwrites the custom data and ordering we just applied.

Is there some way of doing what we need, without resorting to developing custom Dropdown field types?

@Muffinman
Copy link
Author

I've been looking into this again, and I think it would allow developers much more flexibility if one or both of the following could be added:

  1. A new filter on each Model::get_fitlering_data() to allow options to be updated by developers
class FormattedRelation extends ACP\Filtering\Model\Meta {

	public function get_filtering_data() {
		$values = $this->get_meta_values();
		$options = [];

		foreach ( $values as $value ) {
			$options[ $value ] = $this->column->get_formatted_value( $value );
		}

		// Allow options data to be changed with filter
		$options = apply_filters( 'acp/filtering/filtering_options', $options, $this->column );

		return [
			'empty_option' => true,
			'options'      => $options,
		];
	}
}
  1. Update assets/filtering/table.js to send through active filtering params to acp_update_filtering_cache.

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