3

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.

2
  • You should formulate it as a question since it's really a Q&A format here.
    – maz
    Commented Sep 20, 2018 at 18:56
  • Ok will do @maz
    – Tyler
    Commented Sep 20, 2018 at 19:09

2 Answers 2

13

Intro

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. enter image description here

The steps we'll go through are as follows:

  1. Create the Rule in the Content Editor Warnings
  2. Create the Rule Condition Item
  3. Create the Custom Condition Code

1 - Create the Rule in the Content Editor Warnings

  1. In the master database navigate to /sitecore/System/Settings/Rules/Content Editor Warnings/Rules. Right click the Rules item and select Insert > Content Editor Warning Rule, and name the new item "ItemHasNoChildren". You should now see the following: enter image description here

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.

  1. In the Rule field of the ItemHasNoChildren item, click "Edit rule" to open the dialog

  2. 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"

  3. 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:

enter image description here

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.

  1. In the "Choose actions" section of the dialog on the right select the "show content editor warning: Title, Text" action

  2. 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:

enter image description here

Click ok.

OPTIONAL: Creating a custom tag

  1. Navigate to /sitecore/System/Settings/Rules/Definitions/Tags.
  2. Right click the "Tags" item and select Insert > Tag and give it the same it "My Custom Rules"

  3. 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

  4. 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

enter image description here

2 - Create the Rule Condition Item

Since the second condition we wanted (item has no children) we need to create an item in Sitecore that will reference our custom rule.

  1. In your master database go to item at /sitecore/System/Settings/Rules/Elements.
  2. Right click the "Elements" item and select Insert > Element Folder, and name the new item "My Custom Rules".
  3. Inside your new item find the "Default" item that is a child of the "Tags" item. In the multilist select the tag you created (mine was called "My Custom Rules") and add it to the selected list. If you did not create a tag, then feel free to select a tag you think is appropriate. Just make sure that that "/sitecore/system/Settings/Rules/Content Editor Warnings/Tags/Default" item also has it selected.

enter image description here

  1. Just like the image above, right click the "My Custom Rules" item, select Insert > Condition, and name the new item "No Children"

  2. 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.

3 - Create the Custom Condition Code

  1. Head over to visual studio and create a new web application solution, I called mine "CodeSamples".
  2. 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.

  1. 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.

  2. 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")

  3. 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:

enter image description here

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.

  1. 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.

  2. 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:

enter image description here

Now our custom condition is a part of the rule we created.

Conclusion

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!)

1
  • any idea how to add some custom warning text (custom action)? the default 'show content editor warning: Title, Text' is not exactly flexible Commented Aug 7, 2019 at 14:43
4

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);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.