14th Dec '23
/
0 comments

Modifying ACF Field Value Before It Is Output

Consider the scenario where a Page has a “Page Fields” field group created with ACF Pro like this:

Field name: scientific_coordinators (type Repeater)
|__ Sub field 1: name (type Text)
|__ Sub field 2: institution (type Text)

Sample Page being edited:

The objective is to output the name and institution separated by a comma for each row like this in a Bricks query loop with its query type set to the above Repeater when this Page is being viewed on the front end:

  • Paul Miles, Rutgers
  • Ezekiel Deleon, Nalanda

In the Bricks editor we could set the text of a Basic Text element like this:

{acf_scientific_coordinators_name}, {acf_scientific_coordinators_institution}

and it would work fine as long as the second sub field is not empty.

If the second field value is empty for one of the rows, the output would look like this:

  • Paul Miles,
  • Ezekiel Deleon, Nalanda

Not ideal.

We need a way to check if the second institution field has any value and only then output it with a preceding comma and space. Otherwise, nothing should be output.

This can be done using this code:

<?php

add_filter( 'acf/format_value/name=institution', function ( $value, $post_id, $field ) {
	return $value ? ', ' . $value : $value;	
}, 10, 3 );

Notice how we don’t prefix the field name with the group name. This means each field name must be unique for it to be targetted via a filter hook like the above. And so, the better practice would be naming the ACF sub fields scientific_coordinators_name instead of just name and scientific_coordinators_institution instead of just institution.

Anyway, with the above code in place we modify the Basic Text element’s text to:

{acf_scientific_coordinators_name}{acf_scientific_coordinators_institution}

and the output on the Page would be:

  • Paul Miles
  • Ezekiel Deleon, Nalanda

Reference

https://www.advancedcustomfields.com/resources/acf-format_value/

Get access to all 630 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

ACF Repeater Sub Field Values as Tabs in Bricks

ACF Repeater Sub Field Values as Tabs in Bricks

Bricks' query loop supports ACF Repeaters and Meta Box Groups out of the box besides posts, users and terms. This tutorial shows how we can…
Categories:
Tags:
Pro
Google Maps JavaScript API in Bricks

Google Maps JavaScript API in Bricks

This Pro tutorial provides the steps to show post-specific maps with custom marker content in Bricks using Google's Maps JavaScript API and Advanced Custom Fields.…
Categories:
How to create filters with IsotopeJS in Bricks (Part 1)

How to create filters with IsotopeJS in Bricks (Part 1)

This tutorial series will explore the IsotopeJS library's features inside the Bricks ecosystem.
Categories:
Pro
Limit Bricks Users Query to Current Post’s User-type ACF Field Value

Limit Bricks Users Query to Current Post’s User-type ACF Field Value

Consider this scenario with ACF: "John Briley" user: "Gandhi" movie: The requirement is to show John Briley as the author when Gandhi movie item is…
Categories:
Pro
How to Inject a Different Type of Post in Bricks Query Loop

How to Inject a Different Type of Post in Bricks Query Loop

Updated on 26 Aug 2024 A user asks: Hello, I'd like to inject a CPT post card inside of a Bricks query loop of a different CPT…
Pro
Conditionally Excluding Elements from Bricks Editor

Conditionally Excluding Elements from Bricks Editor

In the past, we shared How to Exclude Elements from Bricks Editor. This Pro tutorial shows how specific elements can be excluded selectively based on…