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

Transform block allows transforming into blacklisted/non-whitelisted blocks #9047

Closed
tomasruud opened this issue Aug 16, 2018 · 9 comments
Closed
Labels
[Feature] Block Transforms Block transforms from one block to another Needs Technical Feedback Needs testing from a developer perspective. [Type] Help Request Help with setup, implementation, or "How do I?" questions.

Comments

@tomasruud
Copy link

We have a whitelist of default blocks that that are allowed in the editor. For instance we have whitelisted the core/image block, but the gallery block is not in this list. However, we are still able to insert a image block, and transform it into a gallery. Once we reload the page in the editor, the block will error out, because the gallery block is not available.

To Reproduce
Steps to reproduce the behavior:

  1. Whitelist only the image block
  2. Insert image block into editor and transform it into a gallery
  3. Save and reload the page in the editor
  4. The block shows an error because the new block is not available

Expected behavior
I would expect blocks that can be transformed to not show an option of transforming into a blacklisted/non-available block.

@designsimply
Copy link
Member

Adding related documentation links for reference:

I found this in a past discussion—it was in a conversation about parent blocks, however, I thought it might be relevant here as well :

Allowed blocks is not a strong contract, it's just filtering out blocks from the initial set.

Source: #6753 (comment)

In addition to whitelisting the image block, could you try blacklisting the gallery block to see if that solves the problem in this case?

@designsimply designsimply added [Type] Help Request Help with setup, implementation, or "How do I?" questions. [Feature] Block Transforms Block transforms from one block to another [Status] Needs More Info Follow-up required in order to be actionable. labels Aug 16, 2018
@tomasruud
Copy link
Author

I have done all the whitelisting from php, but when I try to add core/image to a blacklist using js, I get an error saying Block "core/gallery" is not registered.. I would assume this is because the block is not registered in the editor due to the php whitelist?

@designsimply designsimply added Needs Technical Feedback Needs testing from a developer perspective. and removed [Status] Needs More Info Follow-up required in order to be actionable. labels Aug 17, 2018
@youknowriad
Copy link
Contributor

How are you exactly "whitelisting" the blocks?

@tomasruud
Copy link
Author

I'm using this filter

add_filter('allowed_block_types', [$this, 'whitelistedBlocks'], 10, 2);

The whitelist looks something like this

public function whitelistedBlocks($defaultTypes, $post)
{
	$basic = [
		'core/paragraph',
		'core/heading',
		'core/image',
	];
	
	$custom = [
		'nice/anchor',
		'nice/article',
		'nice/article-spotlight',
		'nice/base-map',
		'nice/base-stats',
		'nice/contact',
		'nice/contact-email',
		'nice/contact-flexi',
		'nice/hero-image',
		'nice/hero-color',
		'nice/horizontal-rule',
	];
	
	switch ($post->post_type) {
		case 'news':
			return $basic;
		default:
			return $custom;
	}
}

@youknowriad
Copy link
Contributor

According to the documentation allowed_block_types is used to hide blocks from the inserter and not remove them entirely. You can probably try to unregister them instead. https://wordpress.org/gutenberg/handbook/extensibility/extending-blocks/#using-a-whitelist

@dottxado
Copy link

Just tried to unregister all the core blocks but whitelisting the list block: inserted it and trying to transform it, the editor gives an error like "The editor has encountered an unexpected error", and i have this in the console TypeError: Cannot read property 'name' of undefined

@youknowriad
Copy link
Contributor

I believe this happens because you can't unregister all core blocks, some are mandatory: freeform, paragraph, html probably.

@dottxado
Copy link

Ok, you're right: i've repeated the test, unregistering only the subhead block, and it won't show up in the transformations inserter.
So, to be precise, we have to whitelist the blocks both via JS and via PHP: via JS to completely unregister the block, via PHP to allow only certain block into certain post type. Am i right?

@youknowriad
Copy link
Contributor

You can just use a javascript script to unregister the block types you want. And load a separate script per post type to unregister different blocks for different post types.

Closing the issue for now, let me know if you need more help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Block Transforms Block transforms from one block to another 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

4 participants