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
How to set default "align" attribute without using "default" #11388
Comments
Hi @elliotcondon!
Yes, this is currently a limitation that won't be fixed until something like #2751 lands. Could you work around it by hardcoding the default value of function my_plugin_render_callback( $attributes, $content ) {
if ( isset( $attributes[ 'align' ] ) ) {
$align = $attributes['align'];
} else {
$align = 'center'; // Hardcoded default value
}
...
}
register_block_type( 'my-plugin/my-plugin', array(
'render_callback' => 'my_plugin_render_callback',
) );
This sounds like a use case for the |
Hi @noisysocks Thanks for the reply. This solution does indeed solve the problem, but it also causes another. By applying a This is because the blocks don't have any "align" attributes saved within the HTML comments, and will default to the "new default value". For example. A developer may setup the block type to have a default align of "wide". A month later, the developer thinks it would be better for the block to default to "full" and applies this change to the block type settings assuming it will only affect newly added blocks. To both the developer and client's surprise, all the previously created blocks change from "wide" to "full" breaking much of the desired layout. Hope this helps explain the issue. |
Yes, that's a problem with using If it's an attribute with a default value that you think is likely to change in the future, then I'd recommend not specifying a default value at all. The value of the attribute will then always be stored in the HTML or JSON. |
Hi @noisysocks Exactly. But in the case of "align", it would be really useful to define a Perhaps this is an issue that would only effect "Dynamic Blocks". If that's the case, is there any chance we could bypass the "attribute value comparison filtering prior to serialization" for dynamic blocks? |
There's no API for skipping this check. You could probably take advantage of how we're using strict equality to do this check and return a new string object every time that align: {
type: 'string',
get default() {
return new String( 'center' );
},
}, I really don't recommend this though! My advice is to not fight Gutenberg, set a sensible default value, and to not worry too much about the default needing to change in the future. This is the approach that core blocks have been taking. Closing this out for now as I think the question has been answered. Feel free to post another or to ping me on Slack if there's anything else you need help with 🙂 |
That's a shame. There is a real world need for allowing the default value to be saved within a dynamic block's data. I hope my "align" example above can inspire someone to replicate the same problem and investigate a solution. |
Bumping this as we too are having this real issue. |
JS file:
PHP file
I use a dynamic bloc precisely for this reason. In the sense that I want to have these modifications. But you must have a reason to think that. |
Hi all,
I'm looking for a way to set a default "align" setting for a dynamic block.
I understand that the attribute "align" can be given a "default" property, however, this prevents that value from being included in the "HTML comment serialization process".
This is an issue for dynamic blocks which is described here: #7342
Using the "editor.BlockListBlock" filter could be a possibility, but seems overkill for performing such a simple task.
Is there an action / filter I can hook into when a block is first initialized? This would allow me to customize the block attributes. I've tried everything I can think of from within the "fieldType" object which seems to run "too late" in the initialization process - resulting in the alignment toolbar and wrapper attributes ignoring the "default align" until a "change" has occurred (selecting the block).
Thanks
Elliot
The text was updated successfully, but these errors were encountered: