To add on @Chris Auer answer, you may find below the code snippet showing where you should perform the change.
You need to override the OnLoad
method of the GalleryVersionsForm
class from the namespace Sitecore.Shell.Applications.ContentManager.Galleries.Versions
in the assembly Sitecore.Client.dll
.
public class GalleryVersionsExtensionForm : GalleryVersionsForm
{
protected override void OnLoad(EventArgs e)
{
Assert.ArgumentNotNull((object)e, nameof(e));
base.OnLoad(e);
if (Context.ClientPage.IsEvent)
{
//APPLY IMPLEMENTATION HERE
//Code Snippet to retrieve selected version
var formParameters = Context.ClientPage.Request.Form["__PARAMETERS"];
var selectedVersion = formParameters.Split(',')[2].Replace(")", string.Empty).Split('=')[1];
return;
}
Item currentItem = GalleryVersionsForm.GetCurrentItem();
if (currentItem != null)
{
if (currentItem.IsFallback)
{
HtmlGenericControl htmlGenericControl = new HtmlGenericControl("div");
htmlGenericControl.InnerText = Translate.Text("No version exists in the current language. You see a fallback version from '{0}' language.", (object)currentItem.OriginalLanguage);
htmlGenericControl.Attributes["class"] = "versionNumSelected";
Context.ClientPage.AddControl((System.Web.UI.Control)this.Versions, (System.Web.UI.Control)htmlGenericControl);
}
else
{
Item[] versions = currentItem.Versions.GetVersions();
for (int index = versions.Length - 1; index >= 0; --index)
{
Item obj = versions[index];
XmlControl control = ControlFactory.GetControl("Gallery.Versions.Option") as XmlControl;
Assert.IsNotNull((object)control, typeof(XmlControl), "Xml Control \"{0}\" not found", "Gallery.Versions.Option");
Context.ClientPage.AddControl((System.Web.UI.Control)this.Versions, (System.Web.UI.Control)control);
CultureInfo culture = Context.User.Profile.Culture;
string str1 = obj.Statistics.Updated == DateTime.MinValue ? Translate.Text("[Not set]") : DateUtil.FormatShortDateTime(DateUtil.ToServerTime(obj.Statistics.Updated), culture);
string str2 = obj.Statistics.UpdatedBy.Length == 0 ? "-" : obj.Statistics.UpdatedBy;
string str3 = obj.Version.ToString() + ".";
string str4 = obj.Version.Number != currentItem.Version.Number ? "<div class=\"versionNum\">" + str3 + "</div>" : "<div class=\"versionNumSelected\">" + str3 + "</div>";
control["Number"] = (object)str4;
control["Header"] = (object)Translate.Text("Modified <b>{0}</b> by <b>{1}</b>.", (object)str1, (object)str2);
control["Click"] = (object)string.Format("item:load(id={0},language={1},version={2})", (object)currentItem.ID, (object)currentItem.Language, (object)obj.Version.Number);
}
}
}
Item obj1 = Sitecore.Client.CoreDatabase.GetItem("/sitecore/content/Applications/Content Editor/Menues/Versions");
if (obj1 == null || !obj1.HasChildren)
return;
string queryString = WebUtil.GetQueryString("id");
this.Options.AddFromDataSource(obj1, queryString, new CommandContext(currentItem));
}
}
It is in the IF STATEMENT of the IsEvent that you need to add your implementation. The statement is true when the user clicks on a version.
Note that you may also add your custom implementation, without overriding the whole class, after the base method call. Please see below.
public class GalleryVersionsExtensionForm : GalleryVersionsForm
{
protected override void OnLoad(EventArgs e)
{
Assert.ArgumentNotNull((object)e, nameof(e));
base.OnLoad(e);
if (Context.ClientPage.IsEvent)
{
//APPLY IMPLEMENTATION HERE
var formParameters = Context.ClientPage.Request.Form["__PARAMETERS"];
var selectedVersion = formParameters.Split(',')[2].Replace(")", string.Empty).Split('=')[1];
}
}
}
Once you have your code, you need to change the codebeside of the the xml
for the version gallery UI.
From
<CodeBeside Type="Sitecore.Shell.Applications.ContentManager.Galleries.Versions.GalleryVersionsForm,Sitecore.Client"/>
To
<CodeBeside Type="YourNamespace,YourAssemblyName"/>
IMPORTANT
When changing the version gallery UI, it will apply on both the ribbon and the on item version list. Please see screenshot below where the red squares highlight the affected sections
UPDATE 1
The above does not work on the Experience Editor. From a brief analysis, you will need to update/override the JavaScript file SelectVersionGallery.js
to add your custom implementation to the event listener selectversionoption:click
. The file can be found at the path sitecore\shell\client\Sitecore\Speak\Ribbon\Galleries\Versions\SelectVersionGallery.js
UPDATE 2
To retrieve the selected version, the only way is to read the __PARAMETERS
. The returned value is
"item:load(id={110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9},language=en,version=1)"
So, you'll need to perform some replace and split in order to have the version number
var formParameters = Context.ClientPage.Request.Form["__PARAMETERS"];
var selectedVersion = formParameters.Split(',')[2].Replace(")", string.Empty).Split('=')[1];
GetCurrentItem method implementation
private static Item GetCurrentItem()
{
string queryString1 = WebUtil.GetQueryString("db");
string queryString2 = WebUtil.GetQueryString("id");
Language index1 = Language.Parse(WebUtil.GetQueryString("la"));
Sitecore.Data.Version index2 = Sitecore.Data.Version.Parse(WebUtil.GetQueryString("vs"));
Database database = Factory.GetDatabase(queryString1);
Assert.IsNotNull((object)database, queryString1);
return database.Items[queryString2, index1, index2];
}