Block validation: Block validation failed – Custom WordPress Gutenberg Block Fails In Editor when reloaded

huangapple go评论63阅读模式
英文:

Block validation: Block validation failed - Custom WordPress Gutenberg Block Fails In Editor when reloaded

问题

Here's the translated content without the code parts:

我对WordPress和Gutenberg Blocks非常陌生。我已经使用npm Gutenberg生成器创建了一些简单的块。在我的package.json文件中,我已经创建了以下属性:

"attributes": {
"quote": {
"type": "string"
},
"source": {
"type": "string"
}
}

在我的edit.js文件中,我有以下块结构:

export default function Edit(props) {
return (
<div { ...useBlockProps() } className="testimonial">


<RichText
tagName="blockquote"
value={props.attributes.quote}
onChange={(content) => props.setAttributes({ quote: content })}
placeholder='输入引用'
keepPlaceholderOnFocus={true}
/>

<RichText
tagName="figcaption"
value={props.attributes.source}
onChange={(content) => props.setAttributes({source: content })}
placeholder='输入引用/来源'
keepPlaceholderOnFocus={true}
/>

);
}

这是我的save.js文件中的保存函数:

export default function save(props) {
return (


<RichText.Content tagName="blockquote" value={ props.attributes.quote } />

<RichText.Content tagName="figcaption" value={ props.attributes.source } />

);
}

当我将自定义块添加到编辑器时,它运行良好。但是,如果我刷新或重新加载包含我的块的编辑器,我会收到以下错误消息:

块验证:块验证失败 create-block/einzweidinge-testimonial({名称:'create-block/einzweidinge-testimonial',图标:{…},关键字:数组(0), 属性:{…},提供上下文:{…},…})。

通过save函数生成的内容:

从帖子正文中检索的内容:

ffff

fffff

我注意到,如果我添加一个块并没有输入任何数据到RichText组件,那么验证错误就不会发生!我不确定这是否有助于解决问题,但我还在控制台中看到以下警告:

块验证:预期类型为EndTag的令牌({type:'EndTag',标签名:'blockquote'}),而不是Chars({type:'Chars',字符:'ffff' })。我在这里感到困惑,不确定我做错了什么?我已经查看了几个小时!任何帮助或指导都将不胜感激。

更新

在编辑器中检查错误时,我注意到WordPress似乎会添加额外的内容 - 在这种情况下是p标签。

英文:

I am very new to WordPress and I am very new to Gutenberg Blocks. I have created some simple blocks using the npm Gutenburg generator. In my package.json file I have created the following attributes in my package.json file

&quot;attributes&quot;: {
	&quot;quote&quot;: {
	  &quot;type&quot;: &quot;string&quot;
	},
	&quot;source&quot;: {
	  &quot;type&quot;: &quot;string&quot;
	}
  }

In my edit.js file I have the following block stucture

export default function Edit(props) {
	return (
			&lt;div { ...useBlockProps() } className=&quot;testimonial&quot;&gt;
				&lt;div className=&quot;testimonial__wrapper&quot;&gt;
					&lt;div className=&quot;testimonial__header&quot;&gt;
						&lt;i className=&quot;testimonial__quote__left&quot;&gt;&lt;/i&gt;
							&lt;RichText
								tagName=&quot;blockquote&quot;
								value={props.attributes.quote}
								onChange={(content) =&gt; props.setAttributes({ quote: content })}
								placeholder=&#39;Enter Quote&#39;
								keepPlaceholderOnFocus={true}
							/&gt;
						&lt;i className=&quot;testimonial__quote__right&quot;&gt;&lt;/i&gt;
							&lt;RichText
								tagName=&quot;figcaption&quot;
								value={props.attributes.source}
								onChange={(content) =&gt; props.setAttributes({source: content })}
								placeholder=&#39;Enter Quotee / Source&#39;
								keepPlaceholderOnFocus={true}
							/&gt;
					&lt;/div&gt;
				&lt;/div&gt;
			&lt;/div&gt;
	);
}

and this is my save function in the save.js file

export default function save(props) {
	return (
		&lt;div className=&quot;testimonial&quot;&gt;
			&lt;div className=&quot;testimonial__wrapper&quot;&gt;
				&lt;div className=&quot;testimonial__header&quot;&gt;
					&lt;i className=&quot;testimonial__quote__left&quot;&gt;&lt;/i&gt;
					&lt;RichText.Content tagName=&quot;blockquote&quot; value={ props.attributes.quote } /&gt;
					&lt;i className=&quot;testimonial__quote__right&quot;&gt;&lt;/i&gt;
					&lt;RichText.Content tagName=&quot;figcaption&quot; value={ props.attributes.source } /&gt;
				&lt;/div&gt;
			&lt;/div&gt;
		&lt;/div&gt;
	);
}

When I add my custom block into the editor it works great. Should I update and then view my page that includes the block it works! However should I refresh or reload the editor that contains my block I get the following error:

Block validation: Block validation failed for `create-block/einzweidinge-testimonial` ({name: &#39;create-block/einzweidinge-testimonial&#39;, icon: {…}, keywords: Array(0), attributes: {…}, providesContext: {…},&#160;…}).

Content generated by `save` function:

&lt;div class=&quot;testimonial&quot;&gt;&lt;div class=&quot;testimonial__wrapper&quot;&gt;&lt;div class=&quot;testimonial__header&quot;&gt;&lt;i class=&quot;testimonial__quote__left&quot;&gt;&lt;/i&gt;&lt;blockquote&gt;&lt;/blockquote&gt;&lt;i class=&quot;testimonial__quote__right&quot;&gt;&lt;/i&gt;&lt;figcaption&gt;&lt;/figcaption&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

Content retrieved from post body:

&lt;div class=&quot;testimonial&quot;&gt;&lt;div class=&quot;testimonial__wrapper&quot;&gt;&lt;div class=&quot;testimonial__header&quot;&gt;&lt;i class=&quot;testimonial__quote__left&quot;&gt;&lt;/i&gt;&lt;blockquote&gt;ffff&lt;/blockquote&gt;&lt;i class=&quot;testimonial__quote__right&quot;&gt;&lt;/i&gt;&lt;figcaption&gt;fffff&lt;/figcaption&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

I have noticed that should I add a block and not input any data to the RichText components the validation error is not raised! I am not sure if this helps solve the issue but I also see the following warning in the console.

blocks.min.js?ver=69022aed79bfd45b3b1d:10 Block validation: Expected token of type `EndTag` ({type: &#39;EndTag&#39;, tagName: &#39;blockquote&#39;}), instead saw `Chars` ({type: &#39;Chars&#39;, chars: &#39;ffff&#39;}).

I am stumped here and unsure what I am doing wrong? I've been looking into this for a couple of hours! Any help or guidence would be appreciated.

** UPDATE **

When inspecting the error in the editor I have noticed that word press seems to add extra content - in this case p tags

Block validation: Block validation failed – Custom WordPress Gutenberg Block Fails In Editor when reloaded

答案1

得分: 1

我终于解决了问题,问题在于我在package.json文件中定义了我的 "attributes" 对象,而不是block.json文件中!

英文:

I finally worked out the issue, the problem was that I was defining my "attributes" object in the package.json file and not the block.json file!

答案2

得分: 0

问题在于您的属性定义方式;在使用RichText时,需要sourceselector以正确存储和呈现属性。

block.json

{
    ...
    "attributes": {
        "quote": {
            "type": "string",
            "source": "html",
            "selector": "blockquote"
        },
        "source": {
            "type": "string",
            "source": "html",
            "selector": "figcaption"
        }
    }
}

在您的RichText组件中,tagName(例如blockquote)可以用作属性值的选择器。您的save()edit()函数都已正确设置RichText。不过,请注意,RichText上的keepPlaceholderOnFocus属性会在控制台中引发警告,因为其默认值为true,而该属性已从RichText中移除。一旦您更新了属性,您的块应该会如预期地验证并工作。

英文:

The issue is how your attributes are defined; a source and selector is required when using RichText to properly store and render the attributes.

block.json

{
    ...
    &quot;attributes&quot;: {
        &quot;quote&quot;: {
            &quot;type&quot;: &quot;string&quot;,
            &quot;source&quot;: &quot;html&quot;,
            &quot;selector&quot;: &quot;blockquote&quot;
        },
        &quot;source&quot;: {
            &quot;type&quot;: &quot;string&quot;,
            &quot;source&quot;: &quot;html&quot;,
            &quot;selector&quot;: &quot;figcaption&quot;
        }
    }
}

In your RichText component, the tagName (eg. blockquote) can be used as the selector for the attributes value. Both your save() and edit() functions are setup with RichText correctly. One note though, the property keepPlaceholderOnFocus on RichText will throw a warning in the console as its default is true and the property has since been removed from RichText. Once you have updated the attributes, your block should validate and work as expected.