Limiting BlockEditor blocks? Possible?
Is it possible to limit block editor blocks somehow? i.e. I have a dynamic column block (i.e. a seciton you can add multiple 'columns' in it up to 4.
I want to limit it so that a max of 4
ColumnContentBlock
items can be used within the element.6 Replies
Looks like to achieve what I want, I have to move my custom block into a inline repeater which isn't ideal for reusability..
Hi @Braunson We're planning to add the max property to the block editor field, but I'm curious what you mean by reusability. The code defining fields can be used in multiple blocks, repeaters or even the form itself. It's standard OOP PHP, so you can refactor to avoid code duplication
In order to limit the block editor block, i instead have to use a repeater. a repeater doesn't accept a class so i have to redefine the custom sub-block item fields in the repeater everywhere..
not a huge issue for my current use case but in the future if i want to re-use that custom sub-block elsewhere, i have to re-define the fields
I think I see your point but making sure, are you saying that because your repeater fields are defined in a blade file? You can still use the form builder in a blade file, so you should still be able to DRY. If you are using an inline repeater, I'm not sure I understand your point. You can create a simple class or function that returns a form fields array or collection, and you can unpack or merge that in multiple blocks or repeaters. Does that make sense?
With the block editor, I can pass in a Block class
ColumnContentBlock
where my fields are defined. I can then use that block class ColumnContentBlock
elsewhere where needed however since BlockEditor doesn't support max, the solution to limit the # of block ColumnContentBlock
that can be used is to use the InlineRepeater
field. The downside with that is it does not accept a block ColumnContentBlock
like BlockEditor
does, instead I have to pull my field definitions out of the ColumnContentBlock
class and into the InlineRepeater
definition inside my .... ParentContentBlockThatContainsBlockEditorAsAField
I get that. What I'm saying is that the fields definition you are talking about does not have to be duplicated. Both
ColumnContentBlock
and the inline repeater can "import" the same fields. I'll put together an example once I'm back at a computer because it seems like you don't understand what I'm trying to say