Fields that inherit the value of their parent items is something I’ve heard asked for before in the past but I’ve always just recommended coding it into the presentation. As an alternative however I’ve created a custom renderField pipeline step that handles it for you.
By including “InheritParent” in the name of your field it will look for values in parent items when none exists in the current item. Installing it is just a matter of including the code in your project and referencing it in the <renderField> just after GetFieldValue:
<renderField>
<processor type=”Sitecore.Pipelines.RenderField.GetFieldValue, Sitecore.Kernel” />
<processor type=”Namespace.FieldValueInherit, Assembly” />
…
using System;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;
using Sitecore.Pipelines.RenderField;
namespace Namespace
{
/// <summary>
/// Sitecore renderField pipeline for inheriting parent field values recursively.
/// </summary>
public class FieldValueInherit
{
/// <summary>
/// Processes the field value inheritance pipeline.
/// </summary>
/// <param name="args">The pipeline arguments.</param>
public void Process(RenderFieldArgs args)
{
//Dont run while in page edit mode.
if (Sitecore.Context.PageMode.IsPageEditor ||
Sitecore.Context.PageMode.IsPageEditorClassic ||
Sitecore.Context.PageMode.IsPageEditorDesigning ||
Sitecore.Context.PageMode.IsPageEditorEditing ||
Sitecore.Context.PageMode.IsPageEditorNavigating)
{
return;
}
//Check field exists.
Field field = args.GetField();
if (field == null)
{
return;
}
//Check field is tagged to inherit parent values.
if (!field.Name.Contains("InheritParent"))
{
return;
}
//If field contains standard values then check parent items for fields that have been set.
if (field.ContainsStandardValue || !field.HasValue)
{
Item item = args.Item;
if (item == null)
{
return;
}
Item[] ancestors = item.Axes.GetAncestors();
Array.Reverse(ancestors);
foreach (Item ancestor in ancestors)
{
if (ancestor.Fields[field.ID] != null && ancestor.Fields[field.ID].HasValue)
{
args.Result.FirstPart = ancestor.Fields[field.ID].Value;
return;
}
}
}
}
}
}