admin管理员组文章数量:1312895
My goal is for a dynamic Gutenberg Block to work. To find where I go wrong, I reverted it to a static block to check that the attributes save properly, and it works just fine. I also commented out the php side of things so that won't affect anything.
Second step in my testing: I turn the block to dynamic by returning null from the save function. My goal is only to see if the editor stores the attributes, I don't care about the front end yet. Result: It doesn't and I have no idea why not.
The documentation says: "For many dynamic blocks, the save callback function should be returned as null, which tells the editor to save only the block attributes to the database." So it should save them? I don't get it.
Here's the static code that's working perfectly:
registerBlockType( 'slp/signup', {
title: __( 'Sign Up' ),
icon: 'email',
category: 'layout',
keywords: [
__( 'sign up' ),
__( 'form' )
],
attributes: {
title: {
type: 'array',
source: 'children',
selector: 'h2',
},
},
edit( props ) {
function setTitle( content ) {
props.setAttributes( { title: content } );
}
return (
<RichText
tagName="h2"
value={ props.attributes.title }
formattingControls={ [ 'bold', 'italic' ] }
onChange={ setTitle }
placeholder={ __( 'Heading...' ) }
/>
);
},
save( props ) {
return <h2>{props.attributes.title}</h2>;
}
} );
Here's the only thing I change to try to turn it dynamic:
save( props ) {
return null;
}
At this point no attributes are stored, and when I update my editor, everything is empty. (And when I do try to output it with php, it returns an empty array.)
My goal is for a dynamic Gutenberg Block to work. To find where I go wrong, I reverted it to a static block to check that the attributes save properly, and it works just fine. I also commented out the php side of things so that won't affect anything.
Second step in my testing: I turn the block to dynamic by returning null from the save function. My goal is only to see if the editor stores the attributes, I don't care about the front end yet. Result: It doesn't and I have no idea why not.
The documentation says: "For many dynamic blocks, the save callback function should be returned as null, which tells the editor to save only the block attributes to the database." So it should save them? I don't get it.
Here's the static code that's working perfectly:
registerBlockType( 'slp/signup', {
title: __( 'Sign Up' ),
icon: 'email',
category: 'layout',
keywords: [
__( 'sign up' ),
__( 'form' )
],
attributes: {
title: {
type: 'array',
source: 'children',
selector: 'h2',
},
},
edit( props ) {
function setTitle( content ) {
props.setAttributes( { title: content } );
}
return (
<RichText
tagName="h2"
value={ props.attributes.title }
formattingControls={ [ 'bold', 'italic' ] }
onChange={ setTitle }
placeholder={ __( 'Heading...' ) }
/>
);
},
save( props ) {
return <h2>{props.attributes.title}</h2>;
}
} );
Here's the only thing I change to try to turn it dynamic:
save( props ) {
return null;
}
At this point no attributes are stored, and when I update my editor, everything is empty. (And when I do try to output it with php, it returns an empty array.)
Share Improve this question asked Dec 18, 2020 at 7:49 Benjamin Antoni AndersenBenjamin Antoni Andersen 635 bronze badges1 Answer
Reset to default 3You can't use attributes that source data from the HTML markup of your block when using Dynamic Blocks. The attribute selector looks at the Save function's HTML for the attribute, not the Edit function.
You can simply use a type: 'string' instead. Hope this helps!
本文标签: Gutenberg Dynamic Block not Storing Attributes
版权声明:本文标题:Gutenberg Dynamic Block not Storing Attributes 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741904202a2404045.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论