Created
June 12, 2015 16:11
-
-
Save KKings/19794f34fb6f35f39a9d to your computer and use it in GitHub Desktop.
Sitecore 8 replacement for raw:AddCustomField from Sitecore 7
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
namespace NavArts.Sitecore.Search.ComputedFields | |
{ | |
using System; | |
using System.Linq; | |
using System.Xml; | |
using global::Sitecore.ContentSearch; | |
using global::Sitecore.ContentSearch.ComputedFields; | |
using global::Sitecore.Data; | |
using global::Sitecore.Data.Fields; | |
using global::Sitecore.Diagnostics; | |
/// <summary> | |
/// Adds a Sitecore Field to the Index. | |
/// </summary> | |
public sealed class CustomIndexField : IComputedIndexField | |
{ | |
/// <summary> | |
/// Gets or sets the FieldName from the Index Configuration File | |
/// </summary> | |
public string FieldName { get; set; } | |
/// <summary> | |
/// Gets or sets the ReturnType from the Index Configuration File | |
/// </summary> | |
public string ReturnType { get; set; } | |
/// <summary> | |
/// Gets or sets the SitecoreFieldName | |
/// <para>Controlls what field to pull data out of</para> | |
/// </summary> | |
public string SitecoreFieldName { get; set; } | |
/// <summary> | |
/// Creates a new instance of the CustomIndexField with a null XmlNode | |
/// </summary> | |
public CustomIndexField() : this((XmlNode)null) | |
{ | |
} | |
/// <summary> | |
/// Creates a new instance of the CustomIndexField with the passed in configuration | |
/// </summary> | |
public CustomIndexField(XmlNode configurationNode) | |
{ | |
this.Initialize(configurationNode); | |
} | |
/// <summary> | |
/// Check to see if the Indexed Item is a Standard Values Item | |
/// </summary> | |
/// <param name="indexable">The Indexed Item</param> | |
/// <returns>True if the Item being indexed is a Standard Values Item</returns> | |
public object ComputeFieldValue(IIndexable indexable) | |
{ | |
var item = (SitecoreIndexableItem)indexable; | |
if (item != null | |
&& item.Item != null | |
&& !String.IsNullOrEmpty(this.SitecoreFieldName)) | |
{ | |
var field = FieldTypeManager.GetField(item.Item.Fields[this.SitecoreFieldName]); | |
if (field != null && field.InnerField != null) | |
{ | |
var multilistField = field as MultilistField; | |
if (multilistField != null) | |
{ | |
if (multilistField.TargetIDs.Any()) | |
{ | |
return multilistField.TargetIDs.Select(id => ShortID.Encode(id).ToLower()); | |
} | |
} | |
var lookupField = field as LookupField; | |
if (lookupField != null) | |
{ | |
if (lookupField.TargetItem != null) | |
{ | |
return ShortID.Encode(lookupField.TargetID).ToLower(); | |
} | |
} | |
if (field.InnerField.HasValue) | |
{ | |
return field.InnerField.Value; | |
} | |
} | |
} | |
return null; | |
} | |
/// <summary> | |
/// Sets the <see cref="SitecoreFieldName"/> from the configuration elements | |
/// </summary> | |
/// <param name="configurationNode">Passed in configuration</param> | |
private void Initialize(XmlNode configurationNode) | |
{ | |
if (configurationNode == null) | |
{ | |
Log.Error("<name> configuration error: cannot be found.", this); | |
return; | |
} | |
if (configurationNode.ChildNodes.Count > 0) | |
{ | |
var xmlNode = configurationNode.SelectSingleNode("name"); | |
if (xmlNode == null || String.IsNullOrEmpty(xmlNode.InnerText)) | |
{ | |
Log.Error("<name> configuration error: cannot be empty.", this); | |
return; | |
} | |
this.SitecoreFieldName = xmlNode.InnerText; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment