Skip to content

Commit bb5c02a

Browse files
authored
feat: add support for wp-graphql-content-blocks (#430)
1 parent eeaf8da commit bb5c02a

File tree

8 files changed

+211
-2
lines changed

8 files changed

+211
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- fix: Check for Submission Confirmation url before attempting to get the associated post ID.
1212
- fix: Flush static Gravity Forms state between multiple calls to `GFUtils::submit_form()`.
1313
- feat: Add `FieldError.connectedFormField` connection to `FieldError` type.
14+
- feat: Add support for WPGraphQL Content Blocks.
1415
- dev: Remove `vendor` directory from the GitHub repository.
1516
- dev: Use `FormFieldsDataLoader` to resolve fields instead of instantiating a new `Model`.
1617
- chore!: Remove deprecated fields from the schema: `FormsConnectionOrderbyInput.field`, `GfFieldWithDisableQuantitySetting.isQuantityDisabled`. `GfSubmittedEntry.entryId`. `GfForm.button`, `gfForm.entryId`, `gfForm.lastPageButton`.

bin/_lib.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ install_plugins() {
145145
fi
146146
wp plugin activate wp-graphql-upload
147147

148+
# Install WPGraphQL Content Blocks and Activate
149+
if ! $( wp plugin is-installed wp-graphql-content-blocks ); then
150+
wp plugin install https://github.com/wpengine/wp-graphql-content-blocks/releases/latest/download/wp-graphql-content-blocks.zip
151+
fi
152+
wp plugin activate wp-graphql-content-blocks
153+
148154
# Install WPGatsby and Activate
149155
if ! $( wp plugin is-installed wp-gatsby ); then
150156
wp plugin install wp-gatsby

codeception.dist.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,16 @@ modules:
7979
- gravityformsquiz/quiz.php
8080
- gravityformssignature/signature.php
8181
- wp-graphql/wp-graphql.php
82-
- wp-graphql-gravity-forms/wp-graphql-gravity-forms.php
8382
- wp-graphql-upload/wp-graphql-upload.php
83+
- wp-graphql-content-blocks/wp-graphql-content-blocks.php
84+
- wp-graphql-gravity-forms/wp-graphql-gravity-forms.php
8485
activatePlugins:
8586
- gravityforms/gravityforms.php
8687
- gravityformschainedselects/chainedselects.php
8788
- gravityformsquiz/quiz.php
8889
- gravityformssignature/signature.php
8990
- wp-graphql/wp-graphql.php
9091
- wp-graphql-upload/wp-graphql-upload.php
92+
- wp-graphql-content-blocks/wp-graphql-content-blocks.php
93+
- wp-graphql-gravity-forms/wp-graphql-gravity-forms.php
9194
configFile: 'tests/_data/config.php'

docs/querying-forms.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,43 @@ The code comments in the example query below shows how you can fetch and filter
8383
}
8484
}
8585
```
86+
## Get an embedded Form from the Gravity Forms Block.
87+
88+
Gravity Forms can be embedded in a post or page using the [Gravity Forms block](https://docs.gravityforms.com/adding-a-form-using-block/).
89+
90+
When coupled with [WPGraphQL Content Blocks](https://github.com/wpengine/wp-graphql-content-blocks), you can query the embedded form directly from the parsed block content, using the `GravityformsForm.attributes.form` field.
91+
92+
> [!IMPORTANT]
93+
> To query the `GfForm` object from the block content, you must have the `WPGraphQL Content Blocks` plugin version v4.0+ installed and activated.
94+
95+
### Example Query
96+
97+
```graphql
98+
{
99+
post(id: $post_id, idType: DATABASE_ID) {
100+
databaseId
101+
editorBlocks { # Added by WPGraphQL Content Blocks
102+
name
103+
... on GravityformsForm {
104+
attributes {
105+
form { # The GfForm object.
106+
databaseId
107+
formFields {
108+
nodes {
109+
databaseId
110+
type
111+
... on TextField {
112+
label
113+
description
114+
}
115+
}
116+
}
117+
title
118+
# other GraphQL fields.
119+
}
120+
}
121+
}
122+
}
123+
}
124+
}
125+
```

docs/recipes/register-form-to-custom-field.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function my_add_form_to_post() {
3434
$form_id = get_post_meta( $source->ID, 'my_custom_meta_field_form_id', true );
3535

3636
// Return the form.
37-
return $context->get_loader( \WPGraphQL\GF\Data\Loader\FormsLoader::$name )->load_deferred( (int) $form_id );
37+
return \WPGraphQL\GF\Data\Factory::resolve_form( $form_id, $context );
3838
}
3939
]
4040
);

src/Extensions/Extensions.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use WPGraphQL\GF\Extensions\GFQuiz\GFQuiz;
1515
use WPGraphQL\GF\Extensions\GFSignature\GFSignature;
1616
use WPGraphQL\GF\Extensions\WPGatsby\WPGatsby;
17+
use WPGraphQL\GF\Extensions\WPGraphQLContentBlocks\WPGraphQLContentBlocks;
1718
use WPGraphQL\GF\Extensions\WPJamstackDeployments\WPJamstackDeployments;
1819
use WPGraphQL\GF\Interfaces\Hookable;
1920

@@ -29,6 +30,7 @@ public static function register_hooks(): void {
2930
GFQuiz::register_hooks();
3031
GFSignature::register_hooks();
3132
WPGatsby::register_hooks();
33+
WPGraphQLContentBlocks::register_hooks();
3234
WPJamstackDeployments::register_hooks();
3335
}
3436
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* Enables and initializes the Gravity Forms Action Monitor
4+
*
5+
* @package WPGraphQL\GF\Extensions\WPGraphQLContentBlocks
6+
* @since 0.10.0
7+
*/
8+
9+
declare( strict_types = 1 );
10+
11+
namespace WPGraphQL\GF\Extensions\WPGraphQLContentBlocks;
12+
13+
use WPGraphQL\AppContext;
14+
use WPGraphQL\GF\Data\Factory;
15+
use WPGraphQL\GF\Interfaces\Hookable;
16+
use WPGraphQL\GF\Interfaces\Registrable;
17+
use WPGraphQL\GF\Type\WPObject\Form\Form;
18+
19+
/**
20+
* Class - WPGraphQLContentBlocks
21+
*/
22+
class WPGraphQLContentBlocks implements Hookable, Registrable {
23+
/**
24+
* {@inheritDoc}
25+
*/
26+
public static function register_hooks(): void {
27+
if ( ! self::is_plugin_enabled() ) {
28+
return;
29+
}
30+
31+
// Register action monitors.
32+
add_action( 'graphql_register_types', [ self::class, 'register' ] );
33+
}
34+
35+
/**
36+
* Returns whether WPGraphQLContentBlocks is enabled.
37+
*/
38+
public static function is_plugin_enabled(): bool {
39+
return class_exists( 'WPGraphQLContentBlocks' ) && defined( 'WPGRAPHQL_CONTENT_BLOCKS_VERSION' ) && version_compare( WPGRAPHQL_CONTENT_BLOCKS_VERSION, '4.0.0', '>=' );
40+
}
41+
42+
/**
43+
* {@inheritDoc}
44+
*/
45+
public static function register(): void {
46+
register_graphql_field(
47+
'GravityformsFormAttributes', // Generated by wp-graphql-content-blocks.
48+
'form',
49+
[
50+
'type' => Form::$type,
51+
'description' => __( 'The form object associated with the block.', 'wp-graphql-gravity-forms' ),
52+
'resolve' => static function ( $source, array $args, AppContext $context ) {
53+
if ( empty( $source['attrs']['formId'] ) ) {
54+
return null;
55+
}
56+
57+
return Factory::resolve_form( (int) $source['attrs']['formId'], $context );
58+
},
59+
]
60+
);
61+
}
62+
}

tests/wpunit/ContentBlocksTest.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
/**
3+
* Tests a Form block.
4+
*
5+
* @package WPGraphQL\GF\Tests
6+
*/
7+
8+
use Tests\WPGraphQL\GF\TestCase\GFGraphQLTestCase;
9+
10+
/**
11+
* Class - ContentBlocksTest
12+
*/
13+
class ContentBlocksTest extends GFGraphQLTestCase {
14+
private $form_id;
15+
16+
/**
17+
* {@inheritDoc}
18+
*/
19+
public function setUp(): void {
20+
parent::setUp();
21+
22+
$fields = [
23+
$this->factory->field->create(
24+
$this->tester->getPropertyHelper( 'TextField')->values
25+
)
26+
];
27+
28+
$this->form_id = $this->factory->form->create(
29+
array_merge(
30+
$this->tester->getFormDefaultArgs(),
31+
[
32+
'fields' => $fields
33+
]
34+
)
35+
);
36+
37+
$this->clearSchema();
38+
}
39+
40+
/**
41+
* {@inheritDoc}
42+
*/
43+
public function tearDown(): void {
44+
$this->factory->form->delete( $this->form_id );
45+
46+
parent::tearDown();
47+
}
48+
49+
/**
50+
* Test the form field on the GravityformsFormAttributes block.
51+
*/
52+
public function testFormBlock(): void {
53+
$post_id = $this->factory->post->create(
54+
[
55+
'post_content' => sprintf(
56+
'<!-- wp:gravityforms/form {"formId":%d} /-->',
57+
$this->form_id
58+
)
59+
]
60+
);
61+
62+
$query = $this->get_query();
63+
$variables = [
64+
'post_id' => $post_id
65+
];
66+
67+
$actual = $this->graphql( compact( 'query', 'variables' ) );
68+
69+
$this->assertArrayHasKey( 'data', $actual );
70+
71+
$this->assertEquals( $post_id, $actual['data']['post']['databaseId'] );
72+
$this->assertEquals( 'gravityforms/form', $actual['data']['post']['editorBlocks'][0]['name'] );
73+
$this->assertEquals( $this->form_id, $actual['data']['post']['editorBlocks'][0]['attributes']['form']['databaseId'] );
74+
}
75+
76+
private function get_query(): string {
77+
return '
78+
query GetFormBlock($post_id: ID!) {
79+
post(id: $post_id, idType: DATABASE_ID) {
80+
databaseId
81+
editorBlocks {
82+
name
83+
... on GravityformsForm {
84+
attributes {
85+
form {
86+
databaseId
87+
}
88+
}
89+
}
90+
}
91+
}
92+
}
93+
';
94+
}
95+
}

0 commit comments

Comments
 (0)