AdvancedCustomFields / AdvancedCustomFields/acf
[Feedback] Adding a new results filter for AJAX choices
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 945
- Forks
- 197
- PR merge metrics
- No merged PRs in 30d
Description
Hi all,
I'm looking for some feedback on a new filter. Please be sure to post your comments bellow and 'thumbs up' other comments you like!
Overview
I'm looking to add a new filter to customize the 'AJAX results' for choice based fields. The aim of this filter is to allow the select field (and others) to be used with 'Remote Data Sources' or simply to be customized with PHP.
The fields currently using AJAX to load choices are: Select, Relationship, Post Object, Page Link, Taxonomy and User. These fields use Select2 to handle the AJAX functionality which expects a JSON response like:
array(
'results' => array(
'red' => 'Red',
'green' => 'Green',
),
'more' => true
);
Existing Filters
ACF contains some existing filters used in AJAX queries, and it is important to keep a similar naming convention. Existing filters look like:
'acf/fields/post_object/query' - modify the get_posts() query $args
'acf/fields/post_object/result' - modify each result $title
New filter name
Here are some suggested names for the filter:
'acf/fields/select/results' - implies modifying the array of results
'acf/fields/select/response' - implies modifying the AJAX response
* note that the above is in context of the select field type. This would change for each different field type such as post_object, relationship, etc.
What do you think of this filter name? Please let me know in the comments below.
New filter paramters
This filter will only be as useful as the parameters it is given. The obvious ones are:
- $results - (array) the data to be modified by this filter (format to be discussed)
- $field - (array) the field array containing all attributes & settings
- $post_id - (int|string) the current post ID being edited
- $paged - (int) the current page being queried (used for paginating results)
- $search - (string) the search term if entered
What do you think of these parameters? Please let me know in the comments below.
New filter functionality
This filter allows us to modify the $results, but what does $results look like?
As shown previously, Select2 expects and array containing:
- results: This is the array of returned 'key => value' pairs
- more: This boolean value tells Select2 if there is 'more data' to load (allows pagination loading via scroll)
I'm happy to keep things consistent between the filtered PHP $response to the returned JSON, however, it is possible for us to use a different 'data format'.
Here is an example showing 2 different ways the $response could look. This is only an example returning 2 results at a time (more is true):
1. Select2 friendly.
- Allows you to edit both 'results' and 'more' independently, and provides a future proof way of adding more data to the returned response.
- Makes sense for the naming to be 'response'
array(
'results' => array(
'red' => 'Red',
'green' => 'Green',
),
'more' => true
);
2. Simple
- Allows you to edit both 'results' and 'more' together in the same array
- May cause issues if an actual choice has a value of '_more' (unlikely)
- Makes sense for the naming to be 'results'
array(
'red' => 'Red',
'green' => 'Green',
'_more' => true
);
Thanks
Thanks in advance for your comments, thoughts, advice and votes. Please keep your comments as short and precise as possible. I'm only looking for general feedback at the moment, we can have more in depth discussions afterwards. 😄👍
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Review the existing AJAX filters named in the issue for post_object and the other choice-based fields, then compare the proposed results and response formats with Select2's expected JSON. Done would require a settled filter name, parameter set, and response shape; this issue currently asks for feedback rather than identifying an implementation entry point or test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- api, backend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 22/100