Content editor warnings are like little flags that pop up at the top of an item in the content editor. Primarily, they are there to prompt you into action or provide information about the item. They're a handy way to chat with your content authors, spotlighting the important bits or giving some more backstory about the item. How you use these warnings can really shape how content authors see and respond to the item, steering them in the right direction and helping them make choices with all the facts.
In this blog, we're going to chat about how to show a content editor warning based on the last datetime an item was published. Here we have tweaked the publish pipeline to grab the publish date time and updated the Published At field in the item. This blog provides instructions on how to retrieve the publish date and time by modifying the PublishItemProcessor
pipeline.
Customizing Content Editor Warnings
Code Walkthrough
using Sitecore.Pipelines.GetContentEditorWarnings;
using Sitecore.Data.Items;
using Sitecore;
using System;
namespace YournameSpace.Foundation.Workflows
{
public class CustomizeContentEditorWarnings
{
public void Process(GetContentEditorWarningsArgs args)
{
if (args.Item == null) return;
string publishedDate = args.Item["__Published At"];
if (string.IsNullOrEmpty(publishedDate)) return;
if (!string.IsNullOrEmpty(publishedDate))
{
string customText = "This item was last published on ";
UpdateContentEditorWarning(publishedDate, args, customText);
}
}
public void UpdateContentEditorWarning(string date, GetContentEditorWarningsArgs args, string customText)
{
if (DateTime.TryParseExact(date, "yyyyMMddTHHmmssZ", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out DateTime dateTime))
{
DateTime serverDate = DateUtil.ToServerTime(dateTime);
string formattedDate = serverDate.ToString("MMMM dd, yyyy 'at' hh:mm tt 'MST'");
var contentEditorWarning = args.Add();
contentEditorWarning.Title = "Attention";
contentEditorWarning.Text = customText + formattedDate;
contentEditorWarning.Icon = " /sitecore/shell/themes/standard/Images/ information.png";
}
}
}
}
Implementation Details
The CustomizeContentEditorWarnings
class contains the Process
method which performs several tasks. First, it checks if args.Item
is null. If an item exists, the method proceeds.
It then retrieves the __Published At
field value from the item. If the publish date field is empty, the method terminates immediately. Otherwise, it adds a warning for the specific publish dates using the UpdateContentEditorWarning
function.
The UpdateContentEditorWarning
function takes a date string as an argument, converting it into a DateTime object. If successful, it adjusts the value to the server's local time for consistency. It then formats the date in a user-friendly way, such as "May 10, 2024 at 03:30 PM MST".
After formatting the date, it creates a new content editor warning. The warning title is "set to include a custom message with the formatted date, offering a clear alert for the user. Lastly, it assigns an icon to the warning for visual appeal. Here I have used the information icon to distinguish it from standard yellow warnings.
Configuration Setup
In the final step, Sitecore is configured using the provided patch file to execute this custom processor whenever it needs to display warnings in the content editor. This ensures that every time an item with a Published At field is loaded in content editor, the process is automatically triggered, guaranteeing consistent and customized warnings.
<pipelines>
<getContentEditorWarnings>
<processor type="YourNameSpace.Foundation.Workflows.CustomizeContentEditorWarnings, YourNameSpace.Foundation.Workflows"/>
</getContentEditorWarnings>
</pipelines>
Harnessing the Power of Custom Content Editor Warnings
Alright, wrapping things up, this blog post is all about tweaking those content editor warnings based on the item data, especially the 'last published date.' By using customizable titles, messages, and icons, we can make these warnings a whole lot clearer and efficient.
Even though we're mostly talking about the 'Published At' field here, keep in mind that you can tie any field to these content editor warnings. This flexibility makes life a lot easier for our editors.
Thanks for reading!