AdvancedCustomFields / AdvancedCustomFields/acf

Feature Request: acf/after_format_value filter

Open
#450 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
945
Forks
197
PR merge metrics
No merged PRs in 30d

Description

Hey @elliotcondon,

recently I found myself in need of formatting ACF values based on a condition which could change during the runtime and found out that this can't be achieved by using the standard acf/format_value filter due to the internal caching mechanism. The formatted value is cached the first time the function is called and if the conditon changes later on it has no effect.

I made it work by using the acf/pre_format_value filter to bypass the default logic inside the acf_format_value() function but this introduces potential problems down the line when you update the plugin.

That's why I'm proposing a new acf/after_format_value filter. (Or maybe acf/post_format_value but that might be confusing.)

It would look something like this:

function acf_format_value( $value, $post_id, $field ) {
	
    // Allow filter to short-circuit load_value logic.
    $check = apply_filters( "acf/pre_format_value", null, $value, $post_id, $field );
    if( $check !== null ) {
        return $check;
    }
    
    // Get field name.
    $field_name = $field['name'];
    
    // Check store.
    $store = acf_get_store( 'values' );
    if( $store->has( "$post_id:$field_name:formatted" ) ) {
        $formatted_value = $store->get( "$post_id:$field_name:formatted" );
        $formatted_value = apply_filters( "acf/after_format_value", $formatted_value, $post_id, $field );
        return $formatted_value;
    }
	
    /**
    * Filters the $value for use in a template function.
    *
    * @date	28/09/13
    * @since	5.0.0
    *
    * @param	mixed $value The value to preview.
    * @param	string $post_id The post ID for this value.
    * @param	array $field The field array.
    */
    $value = apply_filters( "acf/format_value", $value, $post_id, $field );
	
    // Update store.
    $store->set( "$post_id:$field_name:formatted", $value );
    
    // New filter
    $value = apply_filters( "acf/after_format_value", $value, $post_id, $field );

    // Return value.
    return $value;
}

Just for context this is my use case:
I'm using the Latte templating engine and I need to format the values differently if the get_field function is called from the template .latte file.

I'm aware this is an edge case and there might be more elegant solution to my problem so feel free to ignore this if you think it's a stupid idea;)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating acf_format_value() and reviewing its acf/pre_format_value, acf/format_value, and values-store paths. Check how a new acf/after_format_value filter would behave for cached and newly formatted values, then verify that both paths apply it consistently and add or update the relevant tests if the repository provides them.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.