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

How to set default "align" attribute without using "default" #11388

Closed
elliotcondon opened this issue Nov 2, 2018 · 8 comments
Closed

How to set default "align" attribute without using "default" #11388

elliotcondon opened this issue Nov 2, 2018 · 8 comments
Labels
Needs Technical Feedback Needs testing from a developer perspective. [Type] Help Request Help with setup, implementation, or "How do I?" questions.

Comments

@elliotcondon
Copy link

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

@designsimply designsimply added [Type] Help Request Help with setup, implementation, or "How do I?" questions. Needs Technical Feedback Needs testing from a developer perspective. labels Nov 2, 2018
@noisysocks
Copy link
Member

Hi @elliotcondon!

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".

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 align in render_callback?

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',
) );

Is there an action / filter I can hook into when a block is first initialized? This would allow me to customize the block attributes.

This sounds like a use case for the blocks.registerBlockType filter.

@elliotcondon
Copy link
Author

Hi @noisysocks

Thanks for the reply. This solution does indeed solve the problem, but it also causes another.

By applying a default value within the "render_callback", all dynamic blocks (of this type) are vulnerable to having their "align" setting bulk updated if the developer decides to change this default value setting later on.

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".
The client adds this block to a few pages/posts throughout the site and is given visual confirmation (through the block width and alignment toolbar icon) that the block's alignment is "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.

@noisysocks
Copy link
Member

Yes, that's a problem with using default, even within the official registerBlockType API.

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.

@elliotcondon
Copy link
Author

Hi @noisysocks

Exactly. But in the case of "align", it would be really useful to define a default alignment whilst also saving the selected value.

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?

@noisysocks
Copy link
Member

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 attributeSchema.default is referenced:

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 🙂

@elliotcondon
Copy link
Author

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.

@styledev
Copy link

Bumping this as we too are having this real issue.

@adam-cyln
Copy link

adam-cyln commented Jul 13, 2019

JS file:

[...]
  supports: { align: ["wide", "full"], default: "full" },
[...]
align: {
      type: "string",
      default: "full"
}

PHP file

$align = $attribute['align']? $attribute['align']:'full';

By applying a default value within the "render_callback", all dynamic blocks (of this type) are vulnerable to having their "align" setting bulk updated if the developer decides to change this default value setting later on.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Technical Feedback Needs testing from a developer perspective. [Type] Help Request Help with setup, implementation, or "How do I?" questions.
Projects
None yet
Development

No branches or pull requests

5 participants