Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Adamsimsy/cd20444034ca3718bf71b54c8153ccf3 to your computer and use it in GitHub Desktop.
Save Adamsimsy/cd20444034ca3718bf71b54c8153ccf3 to your computer and use it in GitHub Desktop.
Sitecore computed facet field
public class RelatedThemesFacet : IComputedIndexField
{
public string FieldName { get; set; }
public string ReturnType { get; set; }
public object ComputeFieldValue(IIndexable indexable)
{
var indexableItem = indexable as SitecoreIndexableItem;
if (indexableItem == null || indexableItem.Item == null)
{
return false;
}
return GetReferringItems(indexableItem, "Theme");
}
private List<Item> GetReferringItems(Item item)
{
if (_referringItems.ContainsKey(item.ID.ToString()))
{
return _referringItems[item.ID.ToString()];
}
var itemLinks = Globals.LinkDatabase.GetItemReferrers(item, false);
var items = new List<Item>();
itemLinks.ForEach(x => items.Add(x.GetSourceItem()));
_referringItems[item.ID.ToString()] = items;
return items;
}
private List<Item> GetReferringItems(Item item, string templateFilter)
{
return FilterListOfItemsByTemplate(GetReferringItems(item), templateFilter);
}
private List<Item> FilterListOfItemsByTemplate(List<Item> items, string template)
{
if (items == null)
{
return new List<Item>();
}
return items.Where(x => x != null && x.TemplateID.ToString() == template).ToList();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment