When creating new block directly from ContentArea, the block is automatically published and added to ContentArea. This behavior is not configurable. In this article I will show how to add new block as a draft.
We need to prepare a new EditorDescriptor for ContentArea type. It will inherit from built-in ContentAreaEditorDescriptor. The editor will use custom widgets for All Properties mode and for On Page edit overlay. So we need to set ClientEditingClass and OverlayConfiguration.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[EditorDescriptorRegistration(TargetType = typeof(ContentArea), UIHint = CustomContentArea)] public class CustomContentAreaEditorDescriptor: ContentAreaEditorDescriptor { public const string CustomContentArea = "CustomContentArea"; public CustomContentAreaEditorDescriptor() { ClientEditingClass = "alloy/editors/CustomContentAreaEditor"; } public override void ModifyMetadata(ExtendedMetadata metadata, IEnumerable<Attribute> attributes) { base.ModifyMetadata(metadata, attributes); metadata.OverlayConfiguration["customType"] = "alloy/editors/CustomContentArea"; } } |
Both overlay and editing widget use CreateContentFromSelector command when adding new block. Command has autoPublish property used to check if the content should be automatically published or created as a draft. The command is created every time when “Create a new block” link is clicked, so we have to override original “execute” method, copy the method body and set “autopublish” to false. For widget and for overlay code is almost the same.
For editing widget:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
define([ "dojo/_base/declare", "dojo/_base/lang", "epi-cms/contentediting/editors/ContentAreaEditor", "epi-cms/widget/command/CreateContentFromSelector" ], function ( declare, lang, ContentAreaEditor, CreateContentFromSelector ) { return declare([ContentAreaEditor], { executeAction: function (actionName) { var autoPublish = false; /* ------------------------------------------------- */ /* copied from client resources */ /* ------------------------------------------------- */ if (actionName === "createnewblock") { this._preventOnBlur = true; // since we're going to create a block, we need to hide all validation tooltips because onBlur is prevented here this.validate(false); var command = new CreateContentFromSelector({ creatingTypeIdentifier: "episerver.core.blockdata", createAsLocalAsset: true, autoPublish: autoPublish, allowedTypes: this.allowedTypes, restrictedTypes: this.restrictedTypes }); command.set("model", { save: lang.hitch(this, function (block) { this._preventOnBlur = false; var value = lang.clone(this.get("value"), true) || []; value.push(block); this.set("value", value); // In order to be able to add a block when creating it from a floating editor // we need to set the editing parameter on the editors parent wrapper to true // since it has been set to false while being suspended when switching to // the secondaryView. this.parent.set("editing", true); this.onChange(value); // Now call onBlur since it's been prevented using the _preventOnBlur flag. this.onBlur(); }), cancel: lang.hitch(this, function () { this._preventOnBlur = false; this.onBlur(); }) }); command.execute(); } /* ------------------------------------------------- */ } }); }); |
For overlay:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
define([ "dojo/_base/declare", "dojo/_base/lang", "epi-cms/widget/overlay/ContentArea", "epi-cms/widget/command/CreateContentFromSelector" ], function ( declare, lang, ContentArea, CreateContentFromSelector ) { return declare([ContentArea], { executeAction: function (actionName) { var autoPublish = false; /* ------------------------------------------------- */ /* copied from client resources */ /* ------------------------------------------------- */ if (actionName === "createnewblock") { var command = new CreateContentFromSelector({ creatingTypeIdentifier: "episerver.core.blockdata", createAsLocalAsset: true, treatAsSecondaryView: true, autoPublish: autoPublish, allowedTypes: this.allowedTypes, restrictedTypes: this.restrictedTypes }); command.set("model", { save: lang.hitch(this, function (block) { var value = lang.clone(this.model.get("value"), true) || []; value.push(block); this.onValueChange({ propertyName: this._source.propertyName, value: value }); }) }); command.execute(); } else { this.inherited(arguments); } /* ------------------------------------------------- */ } }); }); |
After running the site, when creating content using “Create new block”, new blocks are added to ContentArea as drafts.