Skip to content

Instantly share code, notes, and snippets.

@lowedown
Last active March 4, 2016 17:04
Show Gist options
  • Save lowedown/005ccfb6ccb2172d2e6b to your computer and use it in GitHub Desktop.
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.
<?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>
/// <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);
}
}
@lowedown
Copy link
Author

lowedown commented Mar 1, 2016

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment