Skip to content
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

feat: expose EditorBlock.type field #285

Merged
merged 1 commit into from
Sep 18, 2024
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
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
Loading