-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Default attributes for block-support properties don't apply on dynamic blocks #50229
Comments
From what I understand, the default are only usually returned when using an instance of the This looks like the code that sets the I don't really know the code well enough to offer suggestions on how to fix it in a backward compatible way. This code has been untouched for years by the looks of it, but @youknowriad last modified it, so might be able to offer suggestions. |
How do you set defaults in block.json? Do you redeclare the style attribute manually? This issue looks like a symptom of the fact that the block parsing in the server is not done "fully". It's just the grammar parse. Unfortunately, there's no solution for that at the moment as introducing the full HTML parsing is too impactful. I'm not really sure why we made sure the default attributes available when using |
My block.json is in the issue description. I'm setting some attributes directly, and for spacing, I set If it helps, for output my render function is simply: function render( $attributes, $content, $block ) {
$wrapper_attributes = get_block_wrapper_attributes();
return sprintf(
'<div %1$s>%2$s</div>',
$wrapper_attributes,
$content
);
} |
I'm encountering the same issue. See a minimal plugin demonstrating the issue here: https://github.com/dutchigor/wordpress-block-supports-render-callback-test |
I just hit this issue today. I would be keen to see some traction. |
This issue was also reported in this ticket: here. I've been able to reproduce it as well. Any clue/idea how can this be fixed? |
The solution fix can be as I explained here #50229 (comment) Make sure to include the default attributes to the "attributes" that are passed to One potential approach as well would be to update |
I am running in to this as well. If a block's
Some additional discussion about default style attributes in |
To reduce the number of open issues, just wondering if this should actually be marked as a duplicate of this very old issue? Also, if this is not going to be fixed soon can we please document this bug, maybe here? This would help prevent hours lost desperately trying to figure out why you can't get something seemingly so simple to work. Not that I did that myself. 🤪 |
I don't think it's the same issue. In fact, I must say that #7342 is an expected behavior because the default value by design is never serialized in the block metadata.
That's the fix that I'm working on right now, and it resolves the issue: diff --git a/src/wp-includes/class-wp-block-supports.php b/src/wp-includes/class-wp-block-supports.php
index c90b5e0c54..71d6b49691 100644
--- a/src/wp-includes/class-wp-block-supports.php
+++ b/src/wp-includes/class-wp-block-supports.php
@@ -104,7 +104,7 @@ class WP_Block_Supports {
}
$block_attributes = array_key_exists( 'attrs', self::$block_to_render ) && is_array( self::$block_to_render['attrs'] )
- ? self::$block_to_render['attrs']
+ ? $block_type->prepare_attributes_for_render( self::$block_to_render['attrs'] )
: array();
$output = array(); I agree with the sentiments shared by @youknowriad that it is suboptimal, but at the same time simple and effective. In the long run, we should rewrite the block parser. @dmsnell did some initial exploration for the lazy parser, that could absorb such operations like adding the default values automatically whenever the attribute is requested for the first time: |
Fix: WordPress/wordpress-develop#7438 |
It should be resolved after WordPress 6.7 gets released in November. Related commit: WordPress/wordpress-develop@7d0e751. |
Thanks @gziolo I appreciate the fix. |
Amazing, thank you @gziolo. This will be very helpful (once WP 6.7 lands). 🎉 |
I'm building a dynamic block, which supports a handful of the built-in style settings (colors, spacing, etc). I can set defaults in
block.json
that apply in the editor, but when I view the front end, it's like nothing is set.get_block_wrapper_attributes
only returns the block class name, none of the color classes are set, and nostyle
attribute.It appears that the default attributes don't exist in
parsed_block
, which is whatapply_block_supports
uses to render the values in PHP.Editor, appears correct:
Front end, no styles:
An example block.json
This sets a default width, colors, and padding, but none of those are applied.
I think this is the same root issue as #50027, but it affects more than just the alignment.
The text was updated successfully, but these errors were encountered: