Last active
June 21, 2016 19:56
-
-
Save soen/9329234b9cac20a5765ff41010d7bc91 to your computer and use it in GitHub Desktop.
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
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