Skip to content

Instantly share code, notes, and snippets.

@jammykam
Last active March 10, 2018 00:02
Show Gist options
  • Save jammykam/1ffd3c56fcb2ec58b0720751c223696c to your computer and use it in GitHub Desktop.
Save jammykam/1ffd3c56fcb2ec58b0720751c223696c to your computer and use it in GitHub Desktop.
Extension to post by @rpeplau to resolve RTE tokens in Experience Editor mode for site specific purposes http://blog.peplau.com.br/en_US/site-specific-richtext-editor-profiles-custom-source-in-native-richtext-fields/
using Sitecore;
using Sitecore.Data.Fields;
using Sitecore.Diagnostics;
using Sitecore.ExperienceEditor.Utils;
using Sitecore.Globalization;
using Sitecore.Shell.Applications.ContentEditor.RichTextEditor;
using Sitecore.Web.UI.Sheer;
namespace Konabos.RteSources.Commands
{
/// This resolves the profile when the Rich Text Editor is opened in EE mode
public class EditHtml : Sitecore.Shell.Applications.WebEdit.Commands.EditHtml
{
protected void Run(ClientPipelineArgs args)
{
Assert.ArgumentNotNull((object)args, nameof(args));
if (args.IsPostBack)
{
base.Run(args);
}
else
{
string parameter1 = args.Parameters["itemid"];
string parameter2 = args.Parameters["language"];
string parameter3 = args.Parameters["version"];
string parameter4 = args.Parameters["fieldid"];
string parameter5 = args.Parameters["controlid"];
string parameter6 = args.Parameters["value"];
Field field = Client.GetItemNotNull(parameter1).Fields[parameter4];
Assert.IsNotNull((object)field, "field");
SheerResponse.ShowModalDialog(new ModalDialogOptions(new RichTextEditorUrl()
{
Conversion = RichTextEditorUrl.HtmlConversion.DoNotConvert,
Disabled = false,
FieldID = parameter4,
IsPageEdit = true,
ID = string.Empty,
ItemID = parameter1,
Language = parameter2,
Mode = string.Empty,
ShowInFrameBasedDialog = true,
Source = SourceHelper.ResolveSource(field.Source, parameter4), /* this is the only change */
Url = "/sitecore/shell/Controls/Rich Text Editor/EditorPage.aspx",
Value = parameter6,
Version = parameter3
}.GetUrl().ToString())
{
Width = "1200",
Height = "700",
Response = true,
Header = Translate.TextByLanguage("Rich Text Editor", Language.Parse(WebUtility.ClientLanguage))
});
args.WaitForPostBack();
}
}
}
}
using Sitecore.Pipelines.GetLookupSourceItems;
namespace Konabos.RteSources.Pipelines
{
public class ResolveRteLookupSource
{
public void Process(GetLookupSourceItemsArgs args)
{
if (!args.Source.Equals("site:rteprofile"))
return;
// Resolve the token using whatever logic you need. We'll hardcode for this example.
args.Source = "/sitecore/system/Settings/Html Editor Profiles/Rich Text Full";
args.AbortPipeline();
}
}
}
<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
<sitecore>
<pipelines>
<getChromeData>
<processor type="Konabos.RteSources.Pipelines.RteTokenSourceChrome, Konabos.RteSources"
patch:before="*[contains(@type, 'GetRenderingChromeData')]" />
</getChromeData>
<getLookupSourceItems>
<processor patch:before="*[1]" type="Konabos.RteSources.Pipelines.ResolveRteLookupSource, Konabos.RteSources" />
</getLookupSourceItems>
</pipelines>
<commands>
<command name="webedit:edithtml" set:type="Konabos.RteSources.Commands.EditHtml, Konabos.RteSources" />
</commands>
</sitecore>
</configuration>
using System.Collections.Generic;
using Sitecore.Data.Fields;
using Sitecore.Pipelines.GetChromeData;
namespace Konabos.RteSources.Pipelines
{
/// This resolves the RTE profile for the field webedit toolbar in EE mode
public class RteTokenSourceChrome : GetChromeDataProcessor
{
public override void Process(GetChromeDataArgs args)
{
if (!args.ChromeType.Equals("field"))
return;
Field field = args.CustomData["field"] as Field;
if (field != null && field.Type != "Rich Text" && !field.Source.StartsWith("site:"))
return;
string profilePath = SourceHelper.ResolveSource(field.Source, args.CommandContext.Items[0]);
if (profilePath == field.Source)
return;
profilePath += "/WebEdit Buttons";
this.AddButtonsToChromeData((IEnumerable<WebEditButton>)base.GetButtons(profilePath), args);
}
}
}
using Sitecore.Data.Items;
using Sitecore.Pipelines.GetLookupSourceItems;
namespace Konabos.RteSources
{
public class SourceHelper
{
public static string ResolveSource(string fieldSource, string itemId)
{
return ResolveSource(fieldSource, Sitecore.Context.ContentDatabase.GetItem(itemId));
}
public static string ResolveSource(string fieldSource, Item item)
{
if (!fieldSource.StartsWith("site:"))
return fieldSource;
var args = new GetLookupSourceItemsArgs { Source = fieldSource, Item = item };
Sitecore.Pipelines.CorePipeline.Run("getLookupSourceItems", args);
return args.Source;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment