Created
May 7, 2016 18:12
-
-
Save Adamsimsy/cd20444034ca3718bf71b54c8153ccf3 to your computer and use it in GitHub Desktop.
Sitecore computed facet field
This file contains 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
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