Skip to content

Instantly share code, notes, and snippets.

@soen
Last active June 21, 2016 19:56
Show Gist options
  • Save soen/9329234b9cac20a5765ff41010d7bc91 to your computer and use it in GitHub Desktop.
Save soen/9329234b9cac20a5765ff41010d7bc91 to your computer and use it in GitHub Desktop.
using System.Linq;
using Sitecore.Configuration;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Pipelines.ExpandInitialFieldValue;
namespace CustomExpandTokenProcessors.Pipelines
{
public class NextSortOrderReplacer : ExpandInitialFieldValueProcessor
{
private const string Token = "$nextsortorder";
public override void Process(ExpandInitialFieldValueArgs args)
{
Assert.ArgumentNotNull(args, "args");
if (args.SourceField.Value.Contains(Token))
{
if (args.TargetItem != null && args.TargetItem.Parent != null)
{
int nextSortOrder = GetNextSortOrder(args);
args.Result = args.Result.Replace(Token, nextSortOrder.ToString());
}
else
{
args.Result = args.Result.Replace(Token, Settings.DefaultSortOrder.ToString());
}
}
}
private static int GetNextSortOrder(ExpandInitialFieldValueArgs args)
{
int nextSortOrder = Settings.DefaultSortOrder;
Item parent = args.TargetItem.Parent;
if (parent.HasChildren)
{
Item itemWithHighestSortOrder = parent.Children.OrderByDescending(x => x.Appearance.Sortorder).First();
nextSortOrder = nextSortOrder + itemWithHighestSortOrder.Appearance.Sortorder;
}
return nextSortOrder;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment