This post ties nicely into the idea thats shown in /automatically-set-the-language-of-the-content-editor
If you have a multi site solution, chances are each site has a finite set of languages available to it. In this example we have one language per site ala:
– sitecore
– Content
— English site – field with value for default language set to be ‘en’
— French site – field with value for default language set to be ‘fr-fr’
Its easy to create invalid language content under each site. This not only bloats the amount of data being stored but can also be quite misleading, its easy to edit content on the wrong language.
The code below demonstrates a Sitecore command which allows these invalid items to be purged from the tree.
It assumes the website root item has a template with a known ID (WebsiteTemplate) and on this item there is a shared droplist field: Default Language which has its source set to /sitecore/system/Languages
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
using System; using System.Linq; using Sitecore.Data; using Sitecore.Data.Items; using Sitecore.Globalization; using Sitecore.Shell.Framework.Commands; namespace ####.SitecoreCustom.Shell.Framework.Commands { /// <summary> /// Find the default site for the current item, /// then scan all descendents and remove versions /// for items other than the current language /// </summary> public class VersionPurgerCommand : Command { private static readonly ID WebsiteTemplate = new ID(new Guid("{B5BBED63-8A9F-41DA-AE3D-B043FF5DD7DF}")); public override void Execute(CommandContext context) { Item rootItem = context.Items.FirstOrDefault(); if (rootItem != null) { string currentSiteLanguage = SiteLanguageItem(context); if (!String.IsNullOrEmpty(currentSiteLanguage)) { RemoveVersions(rootItem, currentSiteLanguage); } } } private static void RemoveVersions(Item rootItem, string validLanguageName) { CleanItem(rootItem, validLanguageName); foreach (Item child in rootItem.Axes.GetDescendants()) { CleanItem(child, validLanguageName); } } private static void CleanItem(Item child, string validLanguageName) { foreach (Language language in child.Languages) { if (!String.Equals(language.Name, validLanguageName, StringComparison.OrdinalIgnoreCase)) { Item childInLanguage = child.Database.GetItem(child.ID, language); if (childInLanguage != null) { childInLanguage.Versions.RemoveAll(false); } } } } public override CommandState QueryState(CommandContext context) { if (SiteLanguageItem(context) == String.Empty) { return CommandState.Hidden; } return base.QueryState(context); } /// <summary> /// Find the current site language for source item /// </summary> private static string SiteLanguageItem(CommandContext context) { Item rootItem = context.Items.FirstOrDefault(); if (rootItem != null) { Item siteRootItem = rootItem.Axes.GetAncestors().FirstOrDefault(a => a.TemplateID == WebsiteTemplate); if (siteRootItem != null && siteRootItem.Versions.Count > 0) { return siteRootItem.Fields["Default Language"].Value; } } return ""; } } } |
This is then patched into the Sitecore commands via:
1 2 3 4 5 6 7 8 9 |
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/"> <sitecore> <commands> <command name="item:purgeversions" type="####.SitecoreCustom.Shell.Framework.Commands.VersionPurgerCommand, ####.SitecoreCustom" /> </commands> </sitecore> </configuration> |
You can then setup a new button in your ribbon by creating a new item in core. In this example, within the versions chunk of the ribbon:
/sitecore/content/Applications/Content Editor/Ribbons/Chunks/Versions/
You need to fill out ‘Click’ to be item:purgeversions and then choose the icon you want.
By overriding the QueryState function the button only shows if:
- You are below a website root item
- You are on the valid language set on the website root item <- this is really cool since it means you can only remove invalid language versions
Taking this forwards, if you had multiple languages per site you’d need to update some of the logic to deal with lists of languages rather than 1 language.