Skip to content

Block Bindings: Add editor preview to acf-field source. #175

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

Merged
merged 3 commits into from
Jun 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions assets/src/js/bindings/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './sources.js';
83 changes: 83 additions & 0 deletions assets/src/js/bindings/sources.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* WordPress dependencies.
*/
import { __ } from '@wordpress/i18n';
import { registerBlockBindingsSource } from '@wordpress/blocks';
import { store as coreDataStore } from '@wordpress/core-data';

/**
* Get the value of a specific field from the ACF fields.
*
* @param {Object} fields The ACF fields object.
* @param {string} fieldName The name of the field to retrieve.
* @returns {string} The value of the specified field, or undefined if not found.
*/
const getFieldValue = ( fields, fieldName ) => fields?.acf?.[ fieldName ];

const resolveImageAttribute = ( imageObj, attribute ) => {
if ( ! imageObj ) return '';
switch ( attribute ) {
case 'url':
case 'content':
return imageObj.source_url;
case 'alt':
return imageObj.alt_text || '';
case 'title':
return imageObj.title?.rendered || '';
default:
return '';
}
};

registerBlockBindingsSource( {
name: 'acf/field',
label: 'SCF Fields',
getValues( { context, bindings, select } ) {
const { getEditedEntityRecord, getMedia } = select( coreDataStore );
let fields =
context?.postType && context?.postId
? getEditedEntityRecord(
'postType',
context.postType,
context.postId
)
: undefined;
const result = {};

Object.entries( bindings ).forEach(
( [ attribute, { args } = {} ] ) => {
const fieldName = args?.key;

const fieldValue = getFieldValue( fields, fieldName );
if ( typeof fieldValue === 'object' && fieldValue !== null ) {
let value = '';

if ( fieldValue[ attribute ] ) {
value = fieldValue[ attribute ];
} else if ( attribute === 'content' && fieldValue.url ) {
value = fieldValue.url;
}

result[ attribute ] = value;
} else if ( typeof fieldValue === 'number' ) {
if ( attribute === 'content' ) {
result[ attribute ] = fieldValue.toString() || '';
} else {
const imageObj = getMedia( fieldValue );
result[ attribute ] = resolveImageAttribute(
imageObj,
attribute
);
}
} else {
result[ attribute ] = fieldValue || '';
}
}
);

return result;
},
canUserEditValue() {
return false;
},
} );
35 changes: 27 additions & 8 deletions includes/Blocks/Bindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function register_binding_sources() {
array(
'label' => _x( 'SCF Fields', 'The core SCF block binding source name for fields on the current page', 'secure-custom-fields' ),
'get_value_callback' => array( $this, 'get_value' ),
'uses_context' => array( 'postId', 'postType' ),
)
);
}
Expand Down Expand Up @@ -78,15 +79,33 @@ public function get_value( array $source_attrs, \WP_Block $block_instance, strin
}
}

$value = $field['value'];
switch ( $attribute_name ) {
case 'id':
case 'alt':
case 'title':
// The value is in the field of the same name.
$value = $field['value'][ $attribute_name ] ?? '';
break;
case 'url':
// The URL is the field value.
$value = $field['value']['url'] ?? $field['value'] ?? '';
break;
case 'rel':
// Handle checkbox field for rel attribute by joining array values.
if ( is_array( $field['value'] ) ) {
$value = implode( ' ', $field['value'] );
} else {
$value = $field['value'] ?? '';
}
break;
default:
$value = $field['value'];

if ( is_array( $value ) ) {
$value = implode( ', ', $value );
}

// If we're not a scalar we'd throw an error, so return early for safety.
if ( ! is_scalar( $value ) ) {
$value = null;
if ( is_array( $value ) ) {
$value = wp_json_encode( $value );
} elseif ( ! is_scalar( $value ) && null !== $value ) {
$value = '';
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions includes/assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,14 @@ public function register_scripts() {
'version' => $version,
'in_footer' => true,
),
'scf-bindings' => array(
'handle' => 'scf-bindings',
'src' => acf_get_url( sprintf( $js_path_patterns['base'], 'scf-bindings' ) ),
'asset_file' => acf_get_path( sprintf( $asset_path_patterns['base'], 'scf-bindings' ) ),
'version' => $version,
'deps' => array(),
'in_footer' => true,
),
);

// Define style registrations.
Expand Down Expand Up @@ -529,6 +537,7 @@ public function enqueue_scripts() {
// @todo integrate into the above. Previously, they were simply hooked into the hook below.
wp_enqueue_script( 'acf-pro-input' );
wp_enqueue_script( 'acf-pro-ui-options-page' );
wp_enqueue_script( 'scf-bindings' );
wp_enqueue_style( 'acf-pro-input' );

/**
Expand Down
4 changes: 3 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ const commonConfig = {
'js/acf-internal-post-type':
'./assets/src/js/acf-internal-post-type.js',
'js/commands/scf-admin': './assets/src/js/commands/admin-commands.js',
'js/commands/scf-custom-post-types': './assets/src/js/commands/custom-post-type-commands.js',
'js/commands/scf-custom-post-types':
'./assets/src/js/commands/custom-post-type-commands.js',
'js/acf': './assets/src/js/acf.js',
'js/pro/acf-pro-blocks': './assets/src/js/pro/acf-pro-blocks.js',
'js/pro/acf-pro-field-group':
'./assets/src/js/pro/acf-pro-field-group.js',
'js/pro/acf-pro-input': './assets/src/js/pro/acf-pro-input.js',
'js/pro/acf-pro-ui-options-page':
'./assets/src/js/pro/acf-pro-ui-options-page.js',
'js/scf-bindings': './assets/src/js/bindings/index.js',

// CSS files
'css/acf-dark': './assets/src/sass/acf-dark.scss',
Expand Down
Loading