Skip to content

Commit

Permalink
feat: expose EditorBlock.type field
Browse files Browse the repository at this point in the history
  • Loading branch information
justlevine committed Sep 17, 2024
1 parent 43ad50d commit ef90d29
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-bulldogs-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wpengine/wp-graphql-content-blocks": minor
---

feat: expose `EditorBlock.type` field
12 changes: 5 additions & 7 deletions includes/Type/InterfaceType/EditorBlockInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,13 @@ public static function register_type(): void {
return render_block( $block );
},
],
'type' => [
'type' => 'String',
'description' => __( 'The (GraphQL) type of the block', 'wp-graphql-content-blocks' ),
],
],
'resolveType' => static function ( $block ) {
if ( empty( $block['blockName'] ) ) {
$block['blockName'] = 'core/freeform';
}

$type_name = lcfirst( ucwords( $block['blockName'], '/' ) );

return WPGraphQLHelpers::format_type_name( $type_name );
return WPGraphQLHelpers::get_type_name_for_block( $block['blockName'] ?? null );
},
]
);
Expand Down
8 changes: 1 addition & 7 deletions includes/Type/InterfaceType/PostTypeBlockInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,7 @@ public static function register_type( string $post_type, array $block_names = []
],
],
'resolveType' => static function ( $block ) {
if ( empty( $block['blockName'] ) ) {
$block['blockName'] = 'core/freeform';
}

$type_name = lcfirst( ucwords( $block['blockName'], '/' ) );

return WPGraphQLHelpers::format_type_name( $type_name );
return WPGraphQLHelpers::get_type_name_for_block( $block['blockName'] ?? null );
},
]
);
Expand Down
17 changes: 17 additions & 0 deletions includes/Utilities/WPGraphQLHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,21 @@ public static function format_type_name( string $name ): string {

return ! empty( $type_name ) ? Utils::format_type_name( $type_name ) : $type_name;
}

/**
* Gets the GraphQL type name for a provided block name.
*
* @internal This method will likely be removed in the future in favor of a block Model.
*
* @param ?string $block_name The name of the block.
*/
public static function get_type_name_for_block( ?string $block_name ): string {
if ( empty( $block_name ) ) {
$block_name = 'core/freeform';
}

$type_name = lcfirst( ucwords( $block_name, '/' ) );

return self::format_type_name( $type_name );
}
}
1 change: 1 addition & 0 deletions tests/unit/EditorBlockInterfaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public function test_register_type() {
'clientId',
'parentClientId',
'renderedHtml',
'type',
];
$this->assertArrayHasKey( 'data', $response, json_encode( $response ) );
$actual = array_map(
Expand Down
1 change: 1 addition & 0 deletions tests/unit/PostTypeBlockInterfaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public function test_register_type() {
'clientId',
'parentClientId',
'renderedHtml',
'type',
];
$this->assertArrayHasKey( 'data', $response, json_encode( $response ) );
$actual = array_map(
Expand Down

0 comments on commit ef90d29

Please sign in to comment.