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

Feature Request: Disallow a ACF block to be nested #469

Closed
CreativeDive opened this issue Mar 5, 2021 · 5 comments
Closed

Feature Request: Disallow a ACF block to be nested #469

CreativeDive opened this issue Mar 5, 2021 · 5 comments

Comments

@CreativeDive
Copy link
Collaborator

CreativeDive commented Mar 5, 2021

Hey @elliotcondon

it's possible to set an option to acf_register_block_type() to disallow a ACF block can be nested?

In some cases this can be very helpful to set restriction for the user. Because the user is very free to add all ACF blocks as an inner block.

I my case I have builded a header block with ACF, which is always placed on the top outside of the content. But there are other cases, if disallowing to be nested can be very helpful.

Are there any ways I can do this using the Block API?

Something like that?

acf_register_block_type(
	array(
		'name' => 'acf-block',
		'title' => __('ACF Block', 'block'),
		'supports' => array(
			...
			'nested' => false, // We can't use this block as inner block
			...
		),
		'render_template' => my_dir( 'inc/acf-blocks/acf_template/acf_template.php' ),
	)
);

And also helpful can be to avoid the nesting of an ACF block to specific wrapper blocks, like this:

acf_register_block_type(
	array(
		'name' => 'acf-block',
		'title' => __('ACF Block', 'block'),
		'supports' => array(
			...
			'disallow_nesting' => array('core/columns', 'core/cover'), // We can't use this block as inner block for specific block types
			...
		),
		'render_template' => my_dir( 'inc/acf-blocks/acf_template/acf_template.php' ),
	)
);

or

acf_register_block_type(
	array(
		'name' => 'acf-block',
		'title' => __('ACF Block', 'block'),
		'supports' => array(
			...
			'allow_nesting' => array('core/columns', 'core/cover'), // We can use this block as inner block only for specific block types
			...
		),
		'render_template' => my_dir( 'inc/acf-blocks/acf_template/acf_template.php' ),
	)
);

I think the last way, should be the best way corresponding to the following filter:

add_filter( 'allowed_block_types', 'my_function', 10, 2 );

@elliotcondon
Copy link
Contributor

Hi @CreativeDive

Good question. After a quick look online, this may be possible by using the parent property: https://developer.wordpress.org/block-editor/developers/block-api/block-registration/#parent-optional

If the above doesn't work out, please have a read over this ticket which poses a similar question to the Gutenberg team - there may be some useful solutions there.

@CreativeDive
Copy link
Collaborator Author

@elliotcondon Thank you for response.

This parent property is a good solution to allow a block to be nested only in a specific parent block.

This can be also very helpful in the other way.

Is there currently any way to add this parent property to acf_register_block_type()?

Really good thinking on your linked ticket WordPress/gutenberg#7845.

It seems this will support in the future. Will ACF stick with it to support this use case as soon as it becomes available?

@CreativeDive
Copy link
Collaborator Author

@elliotcondon the parent property really works, very nice. Thank you :-)

acf_register_block_type(
	array(
		'name' => 'my-block',
		'title' => __('My Block', 'wphave'),
		...
		'parent' => array('core/cover'),
		...
	)
);

But it seems there is no possibility to set a block for the "root" document only and to allow only specific block as inner blocks at the moment.

@Log1x
Copy link

Log1x commented Mar 6, 2021

But it seems there is no possibility to set a block for the "root" document only and to allow only specific block as inner blocks at the moment.

<InnerBlocks allowedBlocks="<?php echo json_encode(['acf/my-block']); ?>" />

@CreativeDive
Copy link
Collaborator Author

CreativeDive commented Mar 6, 2021

@Log1x Thank you, yes here we can set the allowed blocks for a specific inner block. Thats really great.

But things like to allow a block only on the root first level is missing at the moment. But it seems this will come to Gutenberg.

My main question here, is how we can restrict a specific block in the main way on acf_register_block_type(), like this:

acf_register_block_type(
	array(
		'name' => 'acf-block',
		'title' => __('ACF Block', 'block'),
		'supports' => array(
			...
			'nested' => false, // We can't use this block as inner block
			...
		),
		'render_template' => my_dir( 'inc/acf-blocks/acf_template/acf_template.php' ),
	)
);

So we could define, this block is strictly not nestable.

I don't know if something like that comes to Gutenberg in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants