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

Block Transforms Don't Honor isDefault Block Variations #28119

Open
mrwweb opened this issue Jan 11, 2021 · 5 comments
Open

Block Transforms Don't Honor isDefault Block Variations #28119

mrwweb opened this issue Jan 11, 2021 · 5 comments
Assignees
Labels
[Feature] Block Variations Block variations, including introducing new variations and variations as a feature [Type] Developer Documentation Documentation for developers [Type] Enhancement A suggestion for improvement.

Comments

@mrwweb
Copy link

mrwweb commented Jan 11, 2021

Describe the bug
Transforming to a block ignores a custom default style variation and uses core options instead (e.g. no image fill setting).

To reproduce

Here's an example of a simple Block Variation with isDefault = true. It makes the Media & Text block use the "Crop Image to Fill Entire Column" option be default.

  1. Create a Block Variation with isDefault set to true. (Example below sets Media & Text block to use "Crop image to fill entire column" option by default)
  2. Insert a block and the variation is used correctly
  3. Insert a block that can be transformed to the block variation (e.g. Image)
  4. Transform block in Step 3 to the block variation made in Step 1 (e.g. Transform Image block to Media & Text block)

Result: The core default block is used.

Expected behavior
The block variation with isDefault should be used when a block is transformed to its type.

Screenshots
Here's my real-world example for the following variation (sets an option and background color, changes inner block to not have Large font size):

wp.blocks.registerBlockVariation(
	'core/media-text',
	{
		isDefault: true,
		attributes: {
			imageFill: true,
			backgroundColor: 'sandstone',
		},
		innerBlocks: [
			[ 'core/paragraph' ],
		],
	}
);

animated GIF showing the steps to reproduce

Editor version (please complete the following information):

  • WordPress version: 5.6 without Gutenberg plugin

Desktop (please complete the following information):

  • OS: Win10
  • Browser Firefox 85

Possibly related to…
#23722 #26962

@talldan talldan added the [Feature] Block Variations Block variations, including introducing new variations and variations as a feature label Jan 12, 2021
@talldan
Copy link
Contributor

talldan commented Jan 12, 2021

I would say this is undocumented behavior, but then the whole of block variations seems to be undocumented 😬
#25451

This would probably be challenging to get working as expected, since each block transform calls createBlock with its own hard-coded set of attributes. Either the transform or createBlock would have to be aware of the default variation and know how to extend it with the attributes from the transform.

@mrwweb
Copy link
Author

mrwweb commented Jan 12, 2021

I agree that block variations documentation is in desperate need of improvement!

If the isDefault attribute won't always in all "default" contexts, then I'd consider renaming it to something like inserterDefault or newBlockDefault.

That said, it also seems like block transforms will ideally be a lot more flexible in the long term, and so maybe this is useful input on one of the many ways it should make it easier to transform blocks in the most user-friendly way.

@gziolo gziolo added [Type] Enhancement A suggestion for improvement. and removed [Type] Enhancement A suggestion for improvement. labels Feb 2, 2021
@gziolo
Copy link
Member

gziolo commented Feb 2, 2021

Block variations are documented in several places:

It would be beneficial to add a tutorial that covers it more in-depth.

If the isDefault attribute won't always in all "default" contexts, then I'd consider renaming it to something like inserterDefault or newBlockDefault.

It all depends on the scope defined. By default it's block and inserter which aren't applied to transforms. It should be better explained. isDefault could have a different meaning for every scope.

That said, it also seems like block transforms will ideally be a lot more flexible in the long term, and so maybe this is useful input on one of the many ways it should make it easier to transform blocks in the most user-friendly way.

There is now also new scope transform added by @ntsekouras that would fit better here. It isn't integrated with block tranforms so far. It has it's own block variation transformation control for some block in the sidebar.

@gziolo gziolo added [Type] Developer Documentation Documentation for developers [Type] Enhancement A suggestion for improvement. labels Feb 2, 2021
@r-interactive
Copy link

Hi,

Just want to know if this bug 'isDefault: true' for Block Variations fixed yet. I was trying this tutorial (https://fullsiteediting.com/lessons/block-variations/) and want my new block variation to be default, but it's not making it the default one.

Thanks!

@mevanloon
Copy link

Just wanted to add that block variations added through the PHP block_type_metadata filter do honor isDefault. The following code will work:

add_filter('block_type_metadata', function($meta) {
  if($meta['name'] == "core/buttons") {
    $meta['variations'] = [
      [
        "title" => "A different button",
        "name" => "buttonzzz",
        "icon" => "with-arrow-left",
        "innerBlocks" => [
          ["core/button", ["text"=>"This button is the new default"]]
        ],
        "attributes" => [],
        "isDefault" => true
      ]
    ];
  }
  return $meta;
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Block Variations Block variations, including introducing new variations and variations as a feature [Type] Developer Documentation Documentation for developers [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

No branches or pull requests

6 participants