I want to create a custom block that has nested blocks inside it. Here is a simplified blueprint:
name: My Custom Block
fields:
headline:
type: writer
marks:
- bold
content_blocks:
type: blocks
fieldsets:
- heading
- text
- video
Now I want to create an editable preview for the panel. My problem is, that I don’t know how to properly use the <k-blocks> element. I want it to function exactly like the root blocks inside a page. Here is what I have so far:
But this has only one nested (custom) block type. In my case there will be different block types possible, some default and some custom ones. Also the example from that article isn’t editable inside the preview.
Is there a way to have nested blocks be editable? And is it possible to do that without having to rebuild the default previews?
This works for me as a very basic outline, using the example blocks from above (without the updating part, that is. Maybe it helps as a starting point:
Just wanted to point that I had a challenge with the exact same use case described by @RobertC and that this answer allowed me to do exactly what I wanted to do and was stuck trying to implement.
I don’t think it is documented as clearly as it is here anywhere else though. I have not seen the part with the following in the different documentation pages and examples provided. In the “custom block library” shared with the docs there is no example of a custom block containing nested blocks that are standard blocks defined by the fieldsets and not custom blocks themselves. (not sure if I’m crystal clear here). If there actually is, I missed it.
For future reference here is an example block I was working on when stumbled on the issue:
boxedfeature.yml :
# A block with 2 parts: one side is content (different fields possible, default and custom ones) with a background color, and the other side is an image
name: Boxed Feature
fields:
contents:
type: blocks
fieldsets:
- heading
- text
- list
- button # a custom block
illustration:
type: files
multiple: false
background:
type: color
width: 1/2
mode: options
options:
"#625c51": "Dark background"
"#E5E2DA": "Light background"
index.js for that block:
panel.plugin("hb4/boxedfeature", {
blocks: {
boxedfeature: {
computed: {
image() {
return this.content.illustration[0] || {};
}
},
template: `
<div class="boxed-feature" :style="'background-color:' + this.content.background">
<div class="boxed-feature__grid">
<div class="boxed-feature__content">
<!-- here goes the k-block element with a for loop described by @texnixe -->
<div v-for="item in content.contents">
<k-block :type="item.type" :content="item.content"></k-block>
</div>
</div>
<figure>
<img class="boxed-feature__img" :src="image.url" alt="Feature Image">
</figure>
</div>
</div>
`,
},
},
});
Coming back to this project, obviously my code lacks any way of saving the content in the index.js.
Any suggestion on how to make this work so that the individual blocks contents inside this custom block are saved when edited?
I tried several things but the content is never saved it seems. Any help?