How would I create a custom Content Editor Warning bar?
I want it to appear whenever the item is based on a specific template and does not have any children.
How would I create a custom Content Editor Warning bar?
I want it to appear whenever the item is based on a specific template and does not have any children.
I'll be answering my own question here, since I know how to do this, but I wanted to share.
Sitecore has an out of the box solution for creating custom notifications in the content editor. Sitecore also has a variety of rules that can be used to describe when this notification bar should appear, but if a condition for a rule you want to use does not exist there is a way to create one yourself.
I am going to step through an example where a warning bar appears when an item based a specific template has no children.
The steps we'll go through are as follows:
We said that there were two conditions that we wanted. The first being when the item is of a specified template, and the second is when the item has no children. Our first condition exists in Sitecore out of the box, but the second we'll create custom ourselves.
In the Rule field of the ItemHasNoChildren item, click "Edit rule" to open the dialog
In the choose conditions section on the left of the dialog, scroll down to "Item Information" and you select the condition that says "where the item template is specific template"
In the Edit Rule section of the dialog at the bottom, you should see "Rule 1" with the condition you selected below it. Select the underlined words "specific template" and from the dialog that pops up select the template you want to base your rule off of.
In my case, I created a new template called "SampleTemplate". You can choose one of your that already exist if you like, but don't choose the Standard Template, otherwise this rule will run on every item. At this point the "Create rule" dialog should look something like this:
As mentioned before we will create the second condition ourselves, so it isn't available in the dialog yet, we'll come back to it later. For now we will set the actions we want this rule to do.
In the "Choose actions" section of the dialog on the right select the "show content editor warning: Title, Text" action
The action should not appear in the "Edit rule" section of the dialog below the condition we added, click on the underlined words "Title" and "Text" and give them some meaningful information, this is what text will be displayed on the warning bar
The dialog should look something like this now:
Click ok.
Right click the "Tags" item and select Insert > Tag and give it the same it "My Custom Rules"
Navigate back to "Content Editor Warnings" Rules Context Folder (/sitecore/System/Settings/Rules/Content Editor Warnings) and then go to the "Default" item that is a child of its "Tags" item
The "Tags" field in the "Taxonomy" section shows a multilist of the tags that the Content Editor Warnings subscribe to, find the tag you created and add it the selected list
Since the second condition we wanted (item has no children) we need to create an item in Sitecore that will reference our custom rule.
Just like the image above, right click the "My Custom Rules" item, select Insert > Condition, and name the new item "No Children"
In the No Children item write "where the item has no children" in the "Text" field of the "Data" section
Next we want this item to reference our code, but we'll come back to that once we've written the code.
Create a new class, I called mine "WhenItemHasNoChildren" and use the following code:
using Sitecore.Diagnostics; //For Assert class
using Sitecore.Data.Items; //For Item class
using Sitecore.Rules; //For RuleContext class
using Sitecore.Rules.Conditions; //For WhenCondition class
namespace CodeSamples
{
public class WhenItemHasNoChildren<T> : WhenCondition<T> where T : RuleContext
{
protected override bool Execute(T ruleContext)
{
Assert.ArgumentNotNull(ruleContext, "ruleContext");
Item item = ruleContext.Item;
return (item == null) ? false : !item.HasChildren;
}
}
}
I'll leave it to you to figure out what's going on there.
Once you've done that, build you code, and publish or copy (I won't judge) your CodeSamples.dll to the bin folder of your Sitecore instance's webroot.
Go back to Sitecore and find your "No Children" condition again (mine is at "/sitecore/System/Settings/Rules/Definitions/Elements/My Custom Rules/No Children")
In the "Type" field under the "Script" section enter: your_namespace.your_classname,your_project. For me that looks like: CodeSamples.WhenItemHasNoChildren,CodeSamples
Your item should look something like this:
Now that you have successfully linked your condition item to the custom code for your condition, let's go back to the Content Editor Warnings and add the condition to the rule we created.
Navigate to /sitecore/System/Settings/Rules/Content Editor Warnings/Rules/ItemHasNoChildren and click "Edit rule" in the Rule field once again to open the dialog.
In the "Choose conditions" section of the dialog, you should now see "where the item has no children" under "My Custom Rules". Select it and now your "Create rule" dialog should look like this:
Now our custom condition is a part of the rule we created.
Now create some items based on the "SampleTemplate" or whatever template you used, and give some items children and don't for others you should see your new warning bar! (I had more pictures showing it in action, but I don't have enough reputation to post them in here. Good luck!)
Besides the option of using rules mentioned by Tyler (which is great), one can also use the getContentEditorWarnings
pipeline (this is also how the rules are being executed).
public class HasNoChildren
{
public void Process(GetContentEditorWarningsArgs args)
{
if (args.Item == null) return;
if (!args.Item.HasChildren) return;
var contentEditorWarning = args.Add();
contentEditorWarning.Title = "Item has no children";
contentEditorWarning.Text = "Item has no children. It is required [...]";
}
}
Patch it in using a config file:
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
<sitecore>
<pipelines>
<getContentEditorWarnings>
<processor type="YourSolution.HasNoChildren, YourSolution" />
</getContentEditorWarnings>
</pipelines>
</sitecore>
</configuration>
Here's the properties/methods on the ContentEditorWarning
class which you can use to customize your warning message:
public class ContentEditorWarning
{
public string Key { get; set; }
public string Title { get; set; }
public string Text { get; set; }
public string Icon { get; set; }
public bool HideFields { get; set; }
public bool IsExclusive { get; set; }
public bool IsFullscreen { get; set; }
public List<Pair<string, string>> Options { get; }
public void AddOption(string header);
public void AddOption(string header, string command);
}