Last active
March 4, 2016 17:04
-
-
Save lowedown/005ccfb6ccb2172d2e6b to your computer and use it in GitHub Desktop.
Custom Editor Configuration allowing you to set RichText Editor Profiles per Site instead of using the global "HtmlEditor.DefaultProfile" setting.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8" ?> | |
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> | |
<sitecore> | |
<settings> | |
<!-- Set custom RichText Configuration Type to allow multi site configuration --> | |
<setting name="HtmlEditor.DefaultConfigurationType" value="MyProject.MultiSiteRichTextConfiguration,MyProject"/> | |
<setting name="MultiSiteRichTextConfiguration.DefaultDatabase" value="master"/> | |
<setting name="MultiSiteRichTextConfiguration.CoreDatabase" value="core"/> | |
</settings> | |
</sitecore> | |
</configuration> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Allows setting a default rich text profile per Site. | |
/// </summary> | |
public class MultiSiteRichTextConfiguration : EditorConfiguration | |
{ | |
public MultiSiteRichTextConfiguration(Item profile) : base(GetSiteProfile(profile)) | |
{ | |
} | |
/// <summary> | |
/// Returns the rich text editor profile allowing it to be overridden by site settings | |
/// </summary> | |
/// <param name="profile"></param> | |
/// <returns></returns> | |
private static Item GetSiteProfile(Item profile) | |
{ | |
// Don't override if a profile other than default is used | |
if (!IsDefaultProfile(profile)) | |
{ | |
return profile; | |
} | |
var item = GetItemFromUri(); | |
var site = GetSiteFromItem(item); | |
// Override profile | |
var siteProfile = site.Properties["defaultRichTextProfile"]; | |
if (!string.IsNullOrEmpty(siteProfile)) | |
{ | |
return Factory.GetDatabase(Settings.GetSetting("MultiSiteRichTextConfiguration.CoreDatabase", "core")).GetItem(siteProfile); | |
} | |
return profile; | |
} | |
/// <summary> | |
/// Returns true if the profile is the same as the default profile specified in HtmlEditor.DefaultProfile | |
/// </summary> | |
/// <param name="profile"></param> | |
/// <returns></returns> | |
private static bool IsDefaultProfile(Item profile) | |
{ | |
return Settings.GetSetting("HtmlEditor.DefaultProfile", string.Empty).Equals(profile.Paths.FullPath, StringComparison.CurrentCultureIgnoreCase); | |
} | |
/// <summary> | |
/// Returns the most applicable site for an item | |
/// </summary> | |
/// <param name="item"></param> | |
/// <returns></returns> | |
public static SiteInfo GetSiteFromItem(Item item) | |
{ | |
var siteInfoList = Factory.GetSiteInfoList(); | |
SiteInfo currentSiteinfo = null; | |
var matchLength = 0; | |
foreach (var siteInfo in siteInfoList) | |
{ | |
if (item.Paths.FullPath.StartsWith(siteInfo.RootPath, StringComparison.OrdinalIgnoreCase) && siteInfo.RootPath.Length > matchLength) | |
{ | |
matchLength = siteInfo.RootPath.Length; | |
currentSiteinfo = siteInfo; | |
} | |
} | |
return currentSiteinfo; | |
} | |
/// <summary> | |
/// Returns the context item from the current query string | |
/// </summary> | |
/// <returns></returns> | |
private static Item GetItemFromUri() | |
{ | |
ItemUri itemUri = ItemUri.ParseQueryString(); | |
var database = Factory.GetDatabase(Settings.GetSetting("MultiSiteRichTextConfiguration.DefaultDatabase", string.Empty)); | |
if (database == null) | |
return null; | |
return database.GetItem(itemUri.ItemID); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use the following property on your site setting and point to the RTE Profile you want to set as default:
defaultRichTextProfile="/sitecore/system/Settings/Html Editor Profiles/Rich Text Full"