wordpress/ACF/Timber – how to insert a custom field value?

I have a repeater, inside the repeater, flexible content.
In the flexible content, a select list.

Based on the select list value, I’d like return something else, in my case an array of post objects/post IDs.

I’ve spent some time trying to do this via these ACF hooks

prepare_field

load_value

format_value

i.e.

function cb_acf_load_value($value, $post_id, $field) {

switch ( $field['_name'] ):

    case 'static_blocks'; //sub-field name in flexible content 'section' 

        error_log('i am here');
        return $value;

        $new_value = $field_controller->prepare_static_blocks( $value );
        return $new_value;  
     break;
endswitch;
...
}
add_filter( 'acf/load_value', 'cb_acf_load_value', 10, 3 );

however, even if I reduce my ambitions, and just try and return a simple string,
the field value in twig is empty. I’ve tried same approach with ACF field ID, and seems the same.

If I were not using timber/twig, I would do this directly in the template, but the whole point of twig is try (as best as possible) to avoid that kind of pattern.
i.e. data should all be prepared before i hit the ‘view’

I could do still do something hackish with a custom twig simpleFunction, or even setting my custom list of post objects in the context, and then in twig

{% if content.acf_fc_layout == 'static_blocks' %}
  {{ custom_var }}
{% endif %} 

but it’s really a bad idea, as it means, regardless of whether that field has been added to the post in admin, i’ll be running this extra query every time, which is bad.

Any help or direction much appreciated!

Read more here: wordpress/ACF/Timber – how to insert a custom field value?

Leave a Reply

Your email address will not be published. Required fields are marked *