WordPress自定义小部件保存多个属性?

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

WordPress Custom Widget Save Multiple Attributes?

问题

I am using the @wordpress/create-block package to build a simple widget. I do not understand how to make the save.js save more than one attribute.

我正在使用 @wordpress/create-block 包来构建一个简单的小部件。我不明白如何在 save.js 中保存多个属性。

I have 2 attributes defined in the block.json: theNotes and theContact.

我在 block.json 中定义了2个属性:theNotestheContact

"attributes": {
	"theNotes": {
		"type": "string",
		"source": "text",
		"selector": "div",
		"default": ""
	},
	"theContact": {
		"type": "string",
		"source": "text",
		"selector": "div",
		"default": ""
	}
}

My edit.js looks like this:

我的 edit.js 如下所示:

export default function Edit( { attributes, setAttributes } ) {
	return (
		<div { ...useBlockProps() }>
			<div>
				<TextControl
					label={ __( 'The Notes', 'editor-notes' ) }
					value={ attributes.theNotes }
					onChange={ ( val ) => setAttributes( { theNotes: val } ) }
				/>
			</div>
			<div>
				<TextControl
					label={ __( 'The Contact', 'editor-notes' ) }
					value={ attributes.theContact }
					onChange={ ( val ) => setAttributes( { theContact: val } ) }
				/>
			</div>
		</div>
	);
}

For the save.js file, I cannot find instructions on how to save both of those attributes using this default scaffolding. I thought something like this would work, but I get a block validation error. It says that both the attributes were saved twice to both attribute values.

对于 save.js 文件,我找不到有关如何使用默认脚手架保存这两个属性的说明。我认为像这样的代码会起作用,但我收到了一个块验证错误。错误显示两个属性都保存了两次到两个属性值。

export default function save( { attributes } ) {
    const blockProps = useBlockProps.save();
    return (
        <div { ...blockProps }>
            <div>{ attributes.theNotes }</div>
            <div>{ attributes.theContact }</div>
        </div>
    );
}

The error says:

错误信息如下:

Content generated by `save` function:

<div class="wp-block-create-block-editor-notes"><div>notesTestcontactTest</div><div>notesTestcontactTest</div></div>

Content retrieved from post body:

<div class="wp-block-create-block-editor-notes"><div>notesTest</div><div>contactTest</div></div>

The Getting Started Guide shows how to save one attribute called "message" like this. Apparently, I do not know what to do when I have multiple attributes to update:

入门指南 展示了如何保存名为 "message" 的一个属性,如下所示。显然,我不知道如何处理多个要更新的属性:

import { useBlockProps } from '@wordpress/block-editor';

export default function save( { attributes } ) {
    const blockProps = useBlockProps.save();
    return <div { ...blockProps }>{ attributes.message }</div>;
}
英文:

I am using the @wordpress/create-block package to build a simple widget. I do not understand how to make the save.js save more than one attribute.

I have 2 attributes defined in the block.json: theNotes and theContact.

&quot;attributes&quot;: {
	&quot;theNotes&quot;: {
		&quot;type&quot;: &quot;string&quot;,
		&quot;source&quot;: &quot;text&quot;,
		&quot;selector&quot;: &quot;div&quot;,
		&quot;default&quot;: &quot;&quot;
	},
	&quot;theContact&quot;: {
		&quot;type&quot;: &quot;string&quot;,
		&quot;source&quot;: &quot;text&quot;,
		&quot;selector&quot;: &quot;div&quot;,
		&quot;default&quot;: &quot;&quot;
	}
}

My edit.js looks like this:

export default function Edit( { attributes, setAttributes } ) {
return (
	&lt;div { ...useBlockProps() }&gt;
			&lt;div&gt;
				&lt;TextControl
					label={ __( &#39;The Notes&#39;, &#39;editor-notes&#39; ) }
					value={ attributes.theNotes }
					onChange={ ( val ) =&gt; setAttributes( { theNotes: val } ) }
				/&gt;
			&lt;/div&gt;
			&lt;div&gt;
				&lt;TextControl
					label={ __( &#39;The Contact&#39;, &#39;editor-notes&#39; ) }
					value={ attributes.theContact }
					onChange={ ( val ) =&gt; setAttributes( { theContact: val } ) }
				/&gt;
			&lt;/div&gt;
	&lt;/div&gt;
);}

For the save.js file, I cannot find instructions on how to save both of those attributes using this default scaffolding. I thought something like this would work, but I get a block validation error. It says that both the attributes were saved twice to both attribute values.

export default function save( { attributes } ) {
const blockProps = useBlockProps.save();
return (
    &lt;div { ...blockProps }&gt;
        &lt;div&gt;{ attributes.theNotes }&lt;/div&gt;
        &lt;div&gt;{ attributes.theContact }&lt;/div&gt;
    &lt;/div&gt;
);  }

The error says:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

Content generated by `save` function:

&lt;div class=&quot;wp-block-create-block-editor-notes&quot;&gt;&lt;div&gt;notesTestcontactTest&lt;/div&gt;&lt;div&gt;notesTestcontactTest&lt;/div&gt;&lt;/div&gt;

Content retrieved from post body:

&lt;div class=&quot;wp-block-create-block-editor-notes&quot;&gt;&lt;div&gt;notesTest&lt;/div&gt;&lt;div&gt;contactTest&lt;/div&gt;&lt;/div&gt;

<!-- end snippet -->

The Getting Started Guide shows how to save one attribute called "message" like this. Apparently, I do not know what to do when I have multiple attributes to update:

import { useBlockProps } from &#39;@wordpress/block-editor&#39;;

export default function save( { attributes } ) {
const blockProps = useBlockProps.save();
return &lt;div { ...blockProps }&gt;{ attributes.message }&lt;/div&gt;;}

答案1

得分: 1

错误指示了save()函数的问题;然而,这有点误导。block.json中的属性选择器[1]实际上是如何定义的,为"div",用于both theNotestheContact,导致了savepost body之间的内容不匹配。

由于定义的属性选择器不是唯一元素(在保存的内容中出现了3次<div>...</div>),所以从帖子内容中提取数据的方法会匹配多次,因此两个属性都保存了<div><div>的文本内容。这导致错误消息中出现了"notesTestcontactTest"文本。

block.json(当前)

"attributes": {
    "theNotes": {
        "type": "string",
        "source": "text",  // the value saved for theNotes is the text
        "selector": "div", // within this matching selector
        "default": ""
    },
    "theContact": {
        "type": "string",
        "source": "text",  // the value save for theContact is the text
        "selector": "div", // with the same matching selector as theNotes
        "default": ""
    }
}

通过为两个属性的div选择器添加唯一的类名,将使值正确保存和更新,例如:

block.json(已更新)

"attributes": {
    "theNotes": {
        "type": "string",
        "source": "text", 
        "selector": "div.the-notes", // a div with the className "the-notes"
        "default": ""
    },
    "theContact": {
        "type": "string",
        "source": "text", 
        "selector": "div.the-contact", // a div with the className "the-contact"
        "default": ""
    }
}

与使用TextControl编辑块级元素的文本内容相比,RichText 组件更适合,例如<div>

edit.js

import { useBlockProps, RichText } from '@wordpress/block-editor';

export default function Edit( { attributes, setAttributes } ) {
    return (
        <div { ...useBlockProps() }>
            <RichText
                tagName="div" // Output as a <div>, no extra div needed
                value={ attributes.theNotes } 
                onChange={ ( content ) => setAttributes( { theNotes: content } ) }
                placeholder="Enter Notes.." // Optional
            />
            
            <RichText
                tagName="div" // Output as a <div>, no extra div needed
                value={ attributes.theContact } 
                onChange={ ( content ) => setAttributes( { theContact: content } ) }
                placeholder="Enter Contact.." // Optional
            />
        </div>
    )
}

然后,需要更新save()函数,以确保属性选择器找到正确的匹配项以提取props.attributes的值。可以在save中使用RichText.Content来渲染标签的标记和内容:

save.js

import { useBlockProps, RichText } from '@wordpress/block-editor';

export default function save( { attributes } ) {
    const blockProps = useBlockProps.save();
    return (
        <div { ...blockProps }>
            <RichText.Content 
                tagName="div" 
                className="the-notes"    // Used for the selector
                value={ attributes.theNotes } 
            />
            <RichText.Content 
                tagName="div" 
                className="the-contact"  // Used for the selector
                value={ attributes.theContact } 
            />
        </div>
    ) 
}

RichTexttagName也可以是另一个块级元素,如"h2"或"p",只要属性选择器匹配即可。在重新测试这些更改时,从编辑器中删除先前的块,保存,刷新/清除缓存,然后再次插入您的块。要确认属性正确保存,可以在编辑器中使用代码视图查看将要保存的标记,应该类似于:

<!-- wp:create-block/editor-notes -->
<div class="wp-block-create-block-editor-notes"><div class="the-notes">Notes Test</div><div class="the-contact">Contact Test</div></div>
<!-- /wp:create-block/editor-notes -->
英文:

The error indicates an issue with the save() function; however this is a little misleading. The content mismatch between save and post body is actually caused by how the attribute selector in block.json is defined as "div" for both theNotes and theContact.

Given the attributes selector defined is not a unique element (&lt;div&gt;...&lt;/div&gt; appears 3 times in the saved content), the method used to extract data from the post content gets multiple matches so both attributes save the text content of &lt;div&gt; and &lt;div&gt; again. This results in the "notesTestcontactTest" text seen in the error message.

block.json (current)

&quot;attributes&quot;: {
    &quot;theNotes&quot;: {
        &quot;type&quot;: &quot;string&quot;,
        &quot;source&quot;: &quot;text&quot;,  // the value saved for theNotes is the text
        &quot;selector&quot;: &quot;div&quot;, // within this matching selector
        &quot;default&quot;: &quot;&quot;
    },
    &quot;theContact&quot;: {
        &quot;type&quot;: &quot;string&quot;,
        &quot;source&quot;: &quot;text&quot;,  // the value save for theContact is the text
        &quot;selector&quot;: &quot;div&quot;, // with the same matching selector as theNotes
        &quot;default&quot;: &quot;&quot;
    }
}

By adding a unique class name to div selector of both attributes, the values will save and update correctly, eg:

block.json (updated)

&quot;attributes&quot;: {
    &quot;theNotes&quot;: {
        &quot;type&quot;: &quot;string&quot;,
        &quot;source&quot;: &quot;text&quot;, 
        &quot;selector&quot;: &quot;div.the-notes&quot;, // a div with the className &quot;the-notes&quot;
        &quot;default&quot;: &quot;&quot;
    },
    &quot;theContact&quot;: {
        &quot;type&quot;: &quot;string&quot;,
        &quot;source&quot;: &quot;text&quot;, 
        &quot;selector&quot;: &quot;div.the-contact&quot;, // a div with the className &quot;the-contact&quot;
        &quot;default&quot;: &quot;&quot;
    }
}

The RichText component would be more suitable than using TextControl for editing content to be saved as text of block-level elements like &lt;div&gt;, eg:

edit.js

import { useBlockProps, RichText } from &#39;@wordpress/block-editor&#39;;

export default function Edit( { attributes, setAttributes } ) {
	return (
		&lt;div { ...useBlockProps() }&gt;
			&lt;RichText
				tagName=&quot;div&quot; // Output as a &lt;div&gt;, no extra div needed
				value={ attributes.theNotes } 
				onChange={ ( content ) =&gt; setAttributes( { theNotes: content } ) }
				placeholder=&quot;Enter Notes..&quot; // Optional
			/&gt;
			
			&lt;RichText
				tagName=&quot;div&quot; // Output as a &lt;div&gt;, no extra div needed
				value={ attributes.theContact } 
				onChange={ ( content ) =&gt; setAttributes( { theContact: content } ) }
				placeholder=&quot;Enter Contact..&quot; // Optional
			/&gt;
		&lt;/div&gt;
	)
}

The save() function would then also need updating to ensure the attributes selector finds the right match to extract the props.attributes values. RichText.Content can be used in save to render the tags markup and content:

save.js

import { useBlockProps, RichText } from &#39;@wordpress/block-editor&#39;;

export default function save( { attributes } ) {
    const blockProps = useBlockProps.save();
    return (
        &lt;div { ...blockProps }&gt;
            &lt;RichText.Content 
                tagName=&quot;div&quot; 
                className=&quot;the-notes&quot;    // Used for the selector
                value={ attributes.theNotes } 
            /&gt;
            &lt;RichText.Content 
                tagName=&quot;div&quot; 
                className=&quot;the-contact&quot;  // Used for the selector
                value={ attributes.theContact } 
            /&gt;
        &lt;/div&gt;
    ) 
}

The RichText tagName could also be another block-level element like "h2" or "p" as long as the attributes selector matches. When retesting these changes to your block, remove the previous block from the Editor, save, refresh/clear the cache then insert your block again. To confirm the attributes save corrently, in the Editor use the Code view to see the markup that will be saved, it should be something like:

&lt;!-- wp:create-block/editor-notes --&gt;
&lt;div class=&quot;wp-block-create-block-editor-notes&quot;&gt;&lt;div class=&quot;the-notes&quot;&gt;Notes Test&lt;/div&gt;&lt;div class=&quot;the-contact&quot;&gt;Contact Test&lt;/div&gt;&lt;/div&gt;
&lt;!-- /wp:create-block/editor-notes --&gt;

huangapple
  • 本文由 发表于 2023年2月10日 06:40:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75405190.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定