Skip to content

Commit b707ccf

Browse files
committed
Fix format date
1 parent 8ea253e commit b707ccf

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

assets/src/js/bindings/sources.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import { __ } from '@wordpress/i18n';
55
import { registerBlockBindingsSource } from '@wordpress/blocks';
66
import { store as coreDataStore } from '@wordpress/core-data';
7+
import { dateI18n } from '@wordpress/date';
78

89
/**
910
* Get the value of a specific field from the ACF fields.
@@ -43,12 +44,19 @@ registerBlockBindingsSource( {
4344
)
4445
: undefined;
4546
const result = {};
46-
4747
Object.entries( bindings ).forEach(
4848
( [ attribute, { args } = {} ] ) => {
4949
const fieldName = args?.key;
50-
50+
const mergedFields = fields?.scf_field_groups
51+
? Object.fromEntries(
52+
fields.scf_field_groups
53+
.flatMap( ( group ) => group.fields || [] )
54+
.map( ( field ) => [ field.name, field ] )
55+
)
56+
: {};
5157
const fieldValue = getFieldValue( fields, fieldName );
58+
const fieldType = mergedFields[ fieldName ]?.type;
59+
5260
if ( typeof fieldValue === 'object' && fieldValue !== null ) {
5361
let value = '';
5462

@@ -59,7 +67,7 @@ registerBlockBindingsSource( {
5967
}
6068

6169
result[ attribute ] = value;
62-
} else if ( typeof fieldValue === 'number' ) {
70+
} else if ( 'number' === typeof fieldValue ) {
6371
if ( attribute === 'content' ) {
6472
result[ attribute ] = fieldValue.toString() || '';
6573
} else {
@@ -69,6 +77,12 @@ registerBlockBindingsSource( {
6977
attribute
7078
);
7179
}
80+
} else if ( 'date_picker' === fieldType && fieldValue ) {
81+
result[ attribute ] =
82+
dateI18n(
83+
mergedFields[ fieldName ]?.display_format,
84+
fieldValue
85+
) || '';
7286
} else {
7387
result[ attribute ] = fieldValue || '';
7488
}

includes/rest-api/class-acf-rest-types-endpoint.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,10 @@ public function get_scf_fields( $post_type_object ) {
7373

7474
foreach ( $fields as $field ) {
7575
$group_fields[] = array(
76-
'label' => $field['label'],
77-
'type' => $field['type'],
78-
'name' => $field['name'],
76+
'label' => $field['label'],
77+
'type' => $field['type'],
78+
'name' => $field['name'],
79+
'display_format' => isset( $field['display_format'] ) ? $field['display_format'] : '',
7980
);
8081
}
8182

0 commit comments

Comments
 (0)