Skip to content

Commit 2ed5e0c

Browse files
authored
Merge pull request #181 from wp-graphql/feat/161-update-datetime-field-descriptions
feat: update docs Date fields to link to the RFC3339 spec
2 parents 5d500c0 + 4938913 commit 2ed5e0c

File tree

6 files changed

+46
-11
lines changed

6 files changed

+46
-11
lines changed

src/FieldConfig.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ protected function is_supported_field_type(): bool {
135135
*/
136136
public function get_field_description(): string {
137137

138+
$graphql_field_type = $this->get_graphql_field_type();
139+
$field_type_config = ( $graphql_field_type instanceof AcfGraphQLFieldType ) ? $graphql_field_type->get_config() : [];
140+
138141
// Use the explicit graphql_description, if set
139142
if ( ! empty( $this->acf_field['graphql_description'] ) ) {
140143
$description = $this->acf_field['graphql_description'];
@@ -153,6 +156,14 @@ public function get_field_description(): string {
153156
);
154157
}
155158

159+
if ( isset( $field_type_config['graphql_description_after'] ) ) {
160+
if ( is_callable( $field_type_config['graphql_description_after'] ) ) {
161+
$description .= ' ' . call_user_func( $field_type_config['graphql_description_after'], $this );
162+
} else {
163+
$description .= ' ' . $field_type_config['graphql_description_after'];
164+
}
165+
}
166+
156167
return $description;
157168
}
158169

src/FieldType/DatePicker.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,16 @@ public static function register_field_type(): void {
1212
register_graphql_acf_field_type(
1313
'date_picker',
1414
[
15-
'graphql_type' => 'String',
16-
'resolve' => static function ( $root, $args, $context, $info, $field_type, FieldConfig $field_config ) {
15+
'graphql_type' => 'String',
16+
// Apply a description to be appended to the field description.
17+
// @todo: consider removing when CustomScalar types are supported along with the @specifiedBy directive
18+
'graphql_description_after' => static function ( FieldConfig $field_config ) {
19+
$field_type = $field_config->get_acf_field()['type'] ?? null;
20+
21+
// translators: The $s is the name of the acf field type that is returning a date string according to the RFC3339 spec.
22+
return '(' . sprintf( __( 'ACF Fields of the %s type return a date string according to the RFC3339 spec: https://datatracker.ietf.org/doc/html/rfc3339.', 'wpgraphql-acf' ), $field_type ) . ')';
23+
},
24+
'resolve' => static function ( $root, $args, $context, $info, $field_type, FieldConfig $field_config ) {
1725
$value = $field_config->resolve_field( $root, $args, $context, $info );
1826

1927
if ( empty( $value ) ) {

src/FieldType/DateTimePicker.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@ public static function register_field_type(): void {
1313
register_graphql_acf_field_type(
1414
'date_time_picker',
1515
[
16-
'graphql_type' => 'String',
17-
'resolve' => static function ( $root, $args, $context, $info, $field_type, FieldConfig $field_config ) {
16+
'graphql_type' => 'String',
17+
// Apply a description to be appended to the field description.
18+
// @todo: consider removing when CustomScalar types are supported along with the @specifiedBy directive
19+
'graphql_description_after' => static function ( FieldConfig $field_config ) {
20+
$field_type = $field_config->get_acf_field()['type'] ?? null;
21+
22+
// translators: The $s is the name of the acf field type that is returning a date string according to the RFC3339 spec.
23+
return '(' . sprintf( __( 'ACF Fields of the "%s" type return a date string according to the RFC3339 spec: https://datatracker.ietf.org/doc/html/rfc3339.', 'wpgraphql-acf' ), $field_type ) . ')';
24+
},
25+
'resolve' => static function ( $root, $args, $context, $info, $field_type, FieldConfig $field_config ) {
1826
$value = $field_config->resolve_field( $root, $args, $context, $info );
1927

2028
if ( empty( $value ) ) {

src/FieldType/FlexibleContent.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ static function ( $field ) use ( $layout ) {
7373
)
7474
);
7575

76-
$layout['sub_fields'] = array_merge( $sub_fields, $layout['sub_fields'] );
76+
$layout_sub_fields = ! empty( $layout['sub_fields'] ) && is_array( $layout['sub_fields'] ) ? $layout['sub_fields'] : [];
77+
78+
$layout['sub_fields'] = array_merge( $sub_fields, $layout_sub_fields );
7779

7880
$layouts[] = $layout;
7981
}

src/ThirdParty/AcfExtended/AcfExtended.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,12 +339,12 @@ public function register_initial_types(): void {
339339
'startDate' => [
340340
// @todo: DATETIME Scalar
341341
'type' => 'String',
342-
'description' => __( 'The start date of a date range returned as an RFC 3339 time string', 'wpgraphql-acf' ),
342+
'description' => __( 'The start date of a date range returned as an RFC 3339 time string (https://datatracker.ietf.org/doc/html/rfc3339)', 'wpgraphql-acf' ),
343343
],
344344
'endDate' => [
345345
// @todo: DATETIME Scalar
346346
'type' => 'String',
347-
'description' => __( 'The start date of a date range RFC 3339 time string', 'wpgraphql-acf' ),
347+
'description' => __( 'The start date of a date range RFC 3339 time string (https://datatracker.ietf.org/doc/html/rfc3339)', 'wpgraphql-acf' ),
348348
],
349349
],
350350
]

tests/_support/WPUnit/AcfFieldTestCase.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -672,11 +672,14 @@ public function testFieldDescriptionUsesInstructionsIfGraphqlDescriptionNotProvi
672672
// the instructions should be used for the description
673673
// if "graphql_description" is not provided
674674
$this->expectedNode( '__type.fields', [
675-
'name' => $this->get_formatted_field_name(),
676-
'description' => $instructions
675+
$this->expectedField( 'name', $this->get_formatted_field_name() ),
676+
$this->expectedField( 'description', self::NOT_NULL ),
677677
]),
678+
678679
] );
679680

681+
$this->assertStringContainsString( $instructions, $actual['data']['__type']['fields'][0]['description'] );
682+
680683
// remove the local field
681684
acf_remove_local_field( $field_key );
682685

@@ -720,11 +723,14 @@ public function testFieldDescriptionUsesGraphqlDescriptionIfProvided(): void {
720723
// the instructions should be used for the description
721724
// if "graphql_description" is not provided
722725
$this->expectedNode( '__type.fields', [
723-
'name' => $this->get_formatted_field_name(),
724-
'description' => $graphql_description
726+
$this->expectedField( 'name', $this->get_formatted_field_name() ),
727+
$this->expectedField( 'description', self::NOT_NULL ),
725728
]),
726729
] );
727730

731+
$this->assertStringContainsString( $graphql_description, $actual['data']['__type']['fields'][0]['description'] );
732+
733+
728734
// remove the local field
729735
acf_remove_local_field( $field_key );
730736

0 commit comments

Comments
 (0)