Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
77047c3
Add custom fields experimental setting
cbravobernal Aug 1, 2023
ff12248
Add connections experimental setting, block inspector control and blo…
cbravobernal Aug 1, 2023
514a068
Add "connections" block attribute and get the meta field
michalczaplinski Aug 1, 2023
66a9aef
Merge branch 'experiment/add-custom-connectinons-inspector-control' i…
michalczaplinski Aug 1, 2023
5fbf21c
Get rid of a phpcs warning
michalczaplinski Aug 1, 2023
a493bb1
Update attribute acces and source definition
michalczaplinski Aug 2, 2023
7575b40
Updated the 'connections' property to include 'attributes'
michalczaplinski Aug 2, 2023
f8ee25e
Merge branch 'experiment/add-custom-connectinons-inspector-control' i…
michalczaplinski Aug 2, 2023
39cb931
Add `attributes` to TextControl value
michalczaplinski Aug 2, 2023
fa042ca
Add `attributes` to TextControl value
michalczaplinski Aug 2, 2023
64a5f03
Wrap the PHP code in experimental flag
michalczaplinski Aug 2, 2023
d7fccec
Merge branch 'experiment/add-custom-connectinons-inspector-control' i…
michalczaplinski Aug 2, 2023
0b52159
Update parens
michalczaplinski Aug 2, 2023
2014934
Return early if there is no support for connections
michalczaplinski Aug 2, 2023
52b4a58
Updated block type checks
michalczaplinski Aug 2, 2023
cbec419
Updated experimental blocks logic
michalczaplinski Aug 2, 2023
c29d885
Add a comment in meta.php
michalczaplinski Aug 2, 2023
9f0d851
Update comment
cbravobernal Aug 2, 2023
1c65e74
Clear the paragraph if meta value is cleared
cbravobernal Aug 2, 2023
8638dff
Use placeholders instead of content
cbravobernal Aug 2, 2023
88dd0b5
Update names of HTML processor instances in meta.php
michalczaplinski Aug 3, 2023
81f9c67
Add extra comments
michalczaplinski Aug 3, 2023
60fa061
Merge branch 'experiment/add-custom-connectinons-inspector-control' i…
michalczaplinski Aug 3, 2023
713c6df
Rephrase the placeholder comment
michalczaplinski Aug 3, 2023
e80effe
Check if current block is a paragraph or image
michalczaplinski Aug 3, 2023
7640a60
Abstract the attribute name to use for the connection
michalczaplinski Aug 3, 2023
f0bcb0f
rename `connections` to `__experimentalConnections`
michalczaplinski Aug 7, 2023
6a2785f
Do not export `addAttribute()` or `withInspectorControl()`
michalczaplinski Aug 7, 2023
4408e69
Merge branch 'experiment/add-custom-connectinons-inspector-control' i…
michalczaplinski Aug 7, 2023
5cd3d22
Renamed '__experimentalConnections' to 'connections' in block settings
michalczaplinski Aug 7, 2023
6d00a9b
Renamed '__experimentalConnections' to 'connections' in block settings
michalczaplinski Aug 7, 2023
367ee0c
Remove `connections` attribute from block.json
michalczaplinski Aug 7, 2023
a45e464
Renamed '__experimentalConnections' to 'connections' in block settings
michalczaplinski Aug 7, 2023
84f2a08
Remove `get_updated_html() . ''` in meta.php
michalczaplinski Aug 7, 2023
49d5cb6
Add an allowlist of blocks/attributes
michalczaplinski Aug 7, 2023
414b322
Refactor blocks.php & custom sources
michalczaplinski Aug 7, 2023
92c3cd9
Remove trailing comma?
michalczaplinski Aug 8, 2023
ba713a4
Update naming:
michalczaplinski Aug 9, 2023
a5b9e3b
Merge branch 'trunk' into experiment/add-custom-connectinons-inspecto…
michalczaplinski Aug 9, 2023
a0bd2ca
Merge branch 'experiment/add-custom-connectinons-inspector-control' i…
michalczaplinski Aug 9, 2023
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
Prev Previous commit
Next Next commit
Add "connections" block attribute and get the meta field
  • Loading branch information
michalczaplinski committed Aug 1, 2023
commit 514a0683f185347d4d9f95b1cc7a4cfb9e23de6d
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ Start with the basic building block of all narrative. ([Source](https://github.c
- **Name:** core/paragraph
- **Category:** text
- **Supports:** __unstablePasteTextInline, anchor, color (background, gradients, link, text), spacing (margin, padding), typography (fontSize, lineHeight), ~~className~~
- **Attributes:** align, content, direction, dropCap, placeholder
- **Attributes:** align, connections, content, direction, dropCap, placeholder

## Pattern placeholder

Expand Down
95 changes: 95 additions & 0 deletions lib/experimental/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,98 @@ function gutenberg_register_metadata_attribute( $args ) {
return $args;
}
add_filter( 'register_block_type_args', 'gutenberg_register_metadata_attribute' );




// TODO: Wrap this with a check for the experimental Custom Sources API.

/**
* Renders the block meta attributes.
*
* @param string $block_content Block Content.
* @param array $block Block attributes.
* @param WP_Block $block_instance The block instance.
*/
function gutenberg_render_custom_sources( $block_content, $block, $block_instance ) {
$meta_custom_source = require __DIR__ . '/custom-sources/meta.php';
$block_type = $block_instance->block_type;

// Not sure if we need it, it was in Riad's PR.
if ( null === $block_type ) {
return $block_content;
}

// Whitelist of the block types that support custom sources
// Currently, we only allow the Paragraph and Image blocks to use custom sources.
if ( ! in_array( $block_type->name, array( 'core/paragraph', 'core/image' ), true ) ) {
return $block_content;
}

// Get all the attributes that have a connection.
$connected_attributes = _wp_array_get( $block_type->attributes, array( 'connections', 'attributes' ), false );
if ( ! $connected_attributes ) {
return $block_content;
}

foreach ( $connected_attributes as $attribute_name => $attribute_value ) {
// If the source value is not meta, skip it because we only support meta
// sources for now.
if ( 'meta' !== $attribute_value['source'] ) {
continue;
}

// If the attribute does not have a source, skip it.
if ( ! isset( $block_type->attributes[ $attribute_name ]['source'] ) ) {
continue;
}

// If the attribute does not specify the name of the custom field, skip it.
if ( ! isset( $attribute_value['value'] ) ) {
continue;
}

$block_content = $meta_custom_source['apply_source'](
$block_content,
$block_instance,
$attribute_value['value'],
$block_type->attributes[ $attribute_name ]
);
}

return $block_content;
}

add_filter( 'render_block', 'gutenberg_render_custom_sources', 10, 3 );




// ----- This is just for testing, remove later -----

/**
* Registers a custom meta
*/
function init_test_summary_meta_field() {
register_meta(
'post',
'test_custom_field',
array(
'show_in_rest' => true,
'single' => true,
'type' => 'string',
'default' => 'hello this is a custom field test',
)
);
register_meta(
'post',
'second_test_custom_field',
array(
'show_in_rest' => true,
'single' => true,
'type' => 'string',
'default' => 'second custom field test',
)
);
}
add_action( 'init', 'init_test_summary_meta_field' );
33 changes: 33 additions & 0 deletions lib/experimental/custom-sources/meta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Meta Custom Source
*
* @package gutenberg
*/

return array(
'name' => 'meta',
'apply_source' => function ( $block_content, $block_instance, $meta_field, $attribute_config ) {
$meta_value = get_post_meta( $block_instance->context['postId'], $meta_field, true );
$p = new WP_HTML_Tag_Processor( $block_content );
$found = $p->next_tag(
array(
// TODO: build the query from CSS selector.
'tag_name' => $attribute_config['selector'],
)
);
if ( ! $found ) {
return $block_content;
}
$tag_name = $p->get_tag();
$markup = "<$tag_name>$meta_value</$tag_name>";
$p2 = new WP_HTML_Tag_Processor( $markup );
$p2->next_tag();
$names = $p->get_attribute_names_with_prefix( '' );
foreach ( $names as $name ) {
$p2->set_attribute( $name, $p->get_attribute( $name ) );
}

return $p2 . '';
},
);
9 changes: 9 additions & 0 deletions packages/block-library/src/paragraph/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"description": "Start with the basic building block of all narrative.",
"keywords": [ "text" ],
"textdomain": "default",
"usesContext": [ "postId" ],
Copy link
Contributor

@youknowriad youknowriad Aug 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder whose responsibility is it to do that. In other words, the block is not using the "postId" explicitly, so why add the "useContext" here. The "connection framework" should inject/filter this somehow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean that in the context of Core blocks or custom blocks or both?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

both

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK! For now, I think that for core blocks this is fine. But you're right that for custom blocks we'll have to come up with a proper mechanism here. Are you all right with that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think it's part of the definition of a "custom source" to say which change it needs to make to block registration. I think we should use it for both core and third-party blocks but yeah it's fine to start without it.

Copy link
Member

@gziolo gziolo Aug 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now, you could update usesContext and pass postId in PHP when processing gutenberg_render_block_connections. This way, it won't get unnecessarily wired for all Paragraph block instances through block.json definition.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though this is the first implementation and custom blocks are not supported yet, I believe it'd be better to start creating mechanisms that would work for both if possible. Mainly because we already know we will want to support that in the future.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coming in a bit late here.

The current work on partial syncing (#54233) is using the store actions/selectors for attributes to inject/update values, but it might be difficult to pull this off if block context is being used for custom fields, as I believe it uses react context in JS.

While they're still proofs of concepts, the current work for custom fields/partial syncing does need to remain compatible, so it might be good to explore ways to read/write custom field values in the editor soon though so that we have a clear idea of the problems that need to be solved.

"attributes": {
"align": {
"type": "string"
Expand All @@ -28,6 +29,14 @@
"direction": {
"type": "string",
"enum": [ "ltr", "rtl" ]
},
"connections": {
"type": "object",
"attributes": {
"content": {
"source": "meta"
}
}
}
},
"supports": {
Expand Down