Develop Secure Code
getCurrentSelectedBlock
replaceBlock
replaceBlockReplaces the selected component on the canvas of a marketing content item. Updates persist when the user saves their changes in the content builder. Use this function when building a CMS extension for Marketing Cloud Next that interacts with components on the canvas.
1import { LightningElement, wire } from "lwc";
2import { replaceBlock } from "experience/blockBuilderApi";
3
4class Example extends LightningElement {
5 blockText;
6 block;
7
8 @wire(getCurrentSelectedBlock, {})
9 onCurrentSelectedBlock({ data }) {
10 if (data) {
11 // extract the text attribute
12 this.blockText = data.attributes.text;
13
14 // convert the block JSON to a JS object
15 this.block = JSON.parse(JSON.stringify(data));
16 }
17 }
18
19 // manipulate the text, or other block attributes
20
21 updateBlock() {
22 const id = this.block.id;
23 this.block.attributes.text = this.blockText;
24
25 // replaceBlock requires the block and the nodeId in an object
26 replaceBlock(this.block, {
27 nodeId: id,
28 }).then((response) => {
29 // while the call returns a promise with a payload,
30 // it is not an indication of the success of the
31 // replace operation, since it is asynchronous
32 });
33 }
34}| Parameter Name | Type | Description | Required? |
|---|---|---|---|
selectedBlock | Object | The new component that replaces the selected component on the canvas. | ![]() |
nodeId | String | The ID of the selected component (or node) to be replaced. This field must be included in an object as the second parameter. | ![]() |
replaceBlock action has been initiated. The replacement operation is asynchronous in the content builder.error—A validation error indicating that the nodeId was invalid or not provided, or that the block is invalid.Release Preview