Skip to content

Instantly share code, notes, and snippets.

@lowedown
Last active November 20, 2017 13:30
Show Gist options
  • Save lowedown/f6942d5bfb807478d9876f24533eec9b to your computer and use it in GitHub Desktop.
Save lowedown/f6942d5bfb807478d9876f24533eec9b to your computer and use it in GitHub Desktop.
Add site root token to lookup field queries
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<settings>
<setting name="MyProject.Multisite.SiteRootTemplateId" value="{161227F1-2BC1-4346-B833-D9DBED0B7104}"/>
<setting name="MyProject.Multisite.SiteRootToken" value="$siteRoot"/>
</settings>
<pipelines>
<getLookupSourceItems>
<processor type="MyProject.Multisite.GetLookupSourceItems.ReplaceSiteRootToken, MyProject"
patch:before="processor[1]" />
</getLookupSourceItems>
</pipelines>
</sitecore>
</configuration>
/// <summary>
/// Will replace a site root token in datasource queries of lookup fields.
/// Example: DataSource=$siteRoot/Home will be replaced to the actual site root relative to the context item i.e. DataSource=/sitecore/content/site_xy/Home
/// </summary>
public class ReplaceSiteRootToken
{
public void Process(GetLookupSourceItemsArgs args)
{
Assert.ArgumentNotNull(args, "args");
if (string.IsNullOrEmpty(args.Source) || args.Item == null)
{
return;
}
var siteRootTemplate = Settings.GetSetting("MyProject.Multisite.SiteRootTemplateId");
var siteRootToken = Settings.GetSetting("MyProject.Multisite.SiteRootToken");
if (string.IsNullOrEmpty(siteRootTemplate) || string.IsNullOrEmpty(siteRootToken))
{
return;
}
var rootItem = args.Item.Axes.GetAncestors().FirstOrDefault(a => a.TemplateID == ID.Parse(siteRootTemplate));
if (rootItem == null)
{
rootItem = args.Item;
}
// Replace the site root token in the query or path
args.Source = args.Source.Replace(siteRootToken, rootItem.Paths.FullPath);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment