-
Notifications
You must be signed in to change notification settings - Fork 59
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
Add customFormat method to Field class #152
Conversation
This is an interesting idea, Kaelan. It's worth exploring further. The naming convention could be improved. The name |
The The |
Yes I actually took inspiration from the existing I originally was thinking the callback functionality could be merged into the existing In case you do want to separate
|
Hi Guys - What if this was approached form a more generic angle? Rather than having a method specific to I'll sometimes use add_filter('acf/load_value/name=page_headline', function($value, $post_id) {
return (is_admin() || $value) ? $value : get_the_title($post_id);
}, 10, 2); ... or This could be a |
@nikrowell I could see that being nice for filters. But for actions, it looks like the only field-specific action is The Text::make('Heading')
->filterSettingsOnLoad(function ($field, $fieldObj) {
if ($field['some_setting'] == 'some_value') {
return $fieldObj->required()->helperText('Special instructions...');
}
});
// in filterSettingsOnLoad:
filterSettingsOnLoad($callable): static {
// the filter below would actually get set in `get`, where the final `key` is known
add_filter("acf/load_field/key={key}", function($field) {
$userResult = $callable($field, $this);
if ( ~ is $userResult an Extended/ACF class instance? ~ ) {
return $userResult->get();
}
return $userResult;
});
return $this;
} ^ That might not be a great example, but the point is that some filters may warrant some unique abstractions, in which case having a standalone method is desirable. |
@kaelansmith, you're right, it would require some work adding support on the 14 fields. We could also explore names such as @nikrowell, I agree with this idea as it would make registering filters on fields much cleaner. Keeping the filter close to the field avoids the need to switch contexts. Text::make('Heading')
->maxLength(100)
->filter('acf/load_value/key', function (mixed $value, int|string $id): string {
return (is_admin() || $value) ? $value : get_the_title($id));
})
->required(); The syntax for these methods should be carefully defined. The I also wonder which filters we should support or simply let be. Anything unrelated to the current field would not make sense to support, in my opinion. |
@vinkla I like the simplicity of
I'm not sure why/when a user would ever want to exclude that part? I would think that defining a hook through one of these proposed field methods should always scope the hook to that field -- if I wanted the filter to apply more globally, I would define the filter as usual, separate from the field.
Besides the ones already mentioned ( Sorry this idea has ballooned into a big endeavour, lol. |
Since the package generates the keys automatically, we should provide an implementation that adds the field key from the method rather than requiring the developer to add it themselves.
Let's support everything in the first version. Would you like to create a pull request, @kaelansmith?
I don't think this will be a big endeavor. It should be straightforward adding a |
@vinkla ah, we're on the same page then -- I misunderstood what you meant. Sure, I can likely find time for a PR soon. To clarify, you'd like a single |
Yes, that would be great. But I wonder if it's really necessary to limit it to the current field what do you think? Should we allow all filters from the start? |
Closing this in favour of creating a filter method instead. |
This is a feature idea/request that I figured would be easier to share via a PR. Here's the gist: I would like to make it easy to customize the response format of any given field via a
customFormat
field method (open to better name suggestions); this method would be available to ALL fields, and is therefore defined in the root Field class. It simply leverages ACF'sacf/format_value/key={field_key}
filter hook under-the-hood -- so you pass a callback function intocustomFormat
in the same way you would pass it to that filter hook. When theget
method runs for a field, it checks if a customFormat callback was provided, and sets up the filter for you using the auto-generated (or user-provided) field key.Example:
This is particularly valuable for the types of projects I build, which are headless/decoupled sites where WP is essentially just a REST API that my JS frontends consume; I often need to tweak ACF field formats to expose stuff to the REST API that isn't included by default, or to remove unnecessary nested properties that bloat my API request payload sizes etc. (like my example above which trims out a bunch of bloat that Relationship fields often include). I have so far been using the
acf/format_value
filter hook to adjust field types globally (i.e.add_filter('acf/format_value/type=image' ...);
), but I have recently come across the need to adjust formatting for specific/one-off fields; I can manually set up the filter hook for a given field if I manually provide a key to the field via thekey
method, but I would prefer to continue leveraging the auto-generated field keys and I find thecustomFormat
method much cleaner in general.Thoughts?