Home » Content Editor » Add Buttons to Field Types in Sitecore

Add Buttons to Field Types in Sitecore

Sitecore Technology MVP 2016
Sitecore MVP 2015
Sitecore MVP 2014

Enter your email address to follow this blog and receive notifications of new posts by email.

In a previous post I showed how one could go about removing a button from a field type in Sitecore, and felt a complementary post on adding a new button to a field type was in order.

In this post I will show you how I added a ‘Tomorrow’ button to a new Date field type — I duplicated the existing Date field type (you’ll see this later on in this post) — although I am uncertain how useful such a button would be. In other words, I’m not advocating the Date field type have a ‘Tomorrow’ button. I have created it only as an example.

I first subclassed Sitecore.Shell.Applications.ContentEditor.Date in Sitecore.Kernel.dll so that our new Date control — I’ve named this DateExtended — contains all functionality of the “out of the box” Date control:

using System;

using Sitecore.Diagnostics;
using Sitecore.Web.UI.Sheer;
using Sitecore.Shell.Applications.ContentEditor;

namespace Sitecore.Sandbox.Shell.Applications.ContentEditor
{
    public class DateExtended : Date
    {
        public override void HandleMessage(Message message)
        {
            if (!ShouldHandleMessage(message))
            {
                return;
            }

            if (IsTomorrowClick(message))
            {
                SetDateTomorrow();
                return;
            }

            base.HandleMessage(message);
        }

        private bool ShouldHandleMessage(Message message)
        {
            return IsCurrentControl(message)
                    && !string.IsNullOrWhiteSpace(message.Name);
        }

        private bool IsCurrentControl(Message message)
        {
            return AreEqualIgnoreCase(message["id"], ID);
        }

        private static bool IsTomorrowClick(Message message)
        {
            return AreEqualIgnoreCase(message.Name, "contentdate:tomorrow");
        }

        private void SetDateTomorrow()
        {
            SetValue(GetDateTomorrow());
        }

        protected virtual string GetDateTomorrow()
        {
            return DateUtil.ToIsoDate(System.DateTime.Today.AddDays(1));
        }

        private static bool AreEqualIgnoreCase(string one, string two)
        {
            return string.Equals(one, two, StringComparison.CurrentCultureIgnoreCase);
        }
    }
}

I overrode the HandleMessage() method above to ascertain whether we should handle the message passed to it — only the “contentdate:tomorrow” message should be handled, and this is passed when the ‘Tomorrow’ button is clicked (later on in this post, you will see how this message is associated with the ‘Tomorrow’ button in Sitecore) — and delegate to the base class when another message is passed, and that message is for the current control (this is when message[“id”] is equal to the ID property on the control).

If the ‘Tomorrow’ button was clicked, we derive tomorrow’s date, convert it to the ISO date format, and set it as the value of the field via the SetValue() method which is defined in the Sitecore.Shell.Applications.ContentEditor.Date class.

I then registered our library of controls — a library composed of the one lonely control shown above — with Sitecore via a patch configuration file:

<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <controlSources>
      <source mode="on" namespace="Sitecore.Sandbox.Shell.Applications.ContentEditor" assembly="Sitecore.Sandbox" prefix="content"/>
    </controlSources>
  </sitecore>
</configuration>

I then duplicated the Date field type to a new field type:

new-field-type-core-db

The Control field contains the “content” prefix defined in the patch configuration file above and the name of the DateExtended class — both are joined together by a colon.

Under the new field type I added the new Menu item (button) for ‘Tomorrow’, and associated the “contentdate:tomorrow” message with it:

tomorrow-button-core-db

Let’s try this out!

I created a field on a template using our new field type above:

new-field-date-extended-type

I navigated to an item using the template above, and clicked the ‘Tomorrow’ button:

clicked-tomorrow-button

As you can see the next day’s date was set on the field.

If you have any examples of where you had to add a new button to an existing field type, or ideas for new buttons that might be useful, please share in a comment.

Advertisement

1 Comment

  1. […] Add Buttons to Field Types in Sitecore […]

Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: