From 02764e0cccbe1c6f5daa4a4c348c6448fcdb534c Mon Sep 17 00:00:00 2001 From: Stephan Date: Tue, 5 Mar 2019 13:02:59 +0100 Subject: [PATCH] Implement IPublishedContent Siblings --- src/Umbraco.Web/PublishedContentExtensions.cs | 51 ++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/src/Umbraco.Web/PublishedContentExtensions.cs b/src/Umbraco.Web/PublishedContentExtensions.cs index f880076c50..dbd5008b74 100644 --- a/src/Umbraco.Web/PublishedContentExtensions.cs +++ b/src/Umbraco.Web/PublishedContentExtensions.cs @@ -10,6 +10,7 @@ using Umbraco.Core.Models; using Umbraco.Core.Services; using Umbraco.Examine; using Umbraco.Web.Composing; +using Umbraco.Web.PublishedCache; namespace Umbraco.Web { @@ -21,6 +22,7 @@ namespace Umbraco.Web // see notes in PublishedElementExtensions // private static IPublishedValueFallback PublishedValueFallback => Current.PublishedValueFallback; + private static IPublishedSnapshot PublishedSnapshot => Current.PublishedSnapshot; #region Urls @@ -235,7 +237,8 @@ namespace Umbraco.Web /// /// The content items. /// The specific culture to filter for. If null is used the current culture is used. (Default is null). - internal static IEnumerable WhereIsInvariantOrHasCulture(this IEnumerable contents, string culture = null) + internal static IEnumerable WhereIsInvariantOrHasCulture(this IEnumerable contents, string culture = null) + where T : class, IPublishedContent { if (contents == null) throw new ArgumentNullException(nameof(contents)); @@ -1117,6 +1120,52 @@ namespace Umbraco.Web #endregion + #region Axes: siblings + + /// + /// Gets the siblings of the content. + /// + /// The content. + /// The specific culture to filter for. If null is used the current culture is used. (Default is null) + /// The siblings of the content. + public static IEnumerable Siblings(this IPublishedContent content, string culture = null) + { + return content.Parent != null + ? content.Parent.Children(culture) + : PublishedSnapshot.Content.GetAtRoot().WhereIsInvariantOrHasCulture(culture); + } + + /// + /// Gets the siblings of the content, of a given content type. + /// + /// The content. + /// The specific culture to filter for. If null is used the current culture is used. (Default is null) + /// The content type alias. + /// The siblings of the content, of the given content type. + public static IEnumerable SiblingsOfType(this IPublishedContent content, string contentTypeAlias, string culture = null) + { + return content.Parent != null + ? content.Parent.ChildrenOfType(contentTypeAlias, culture) + : PublishedSnapshot.Content.GetAtRoot().OfTypes(contentTypeAlias).WhereIsInvariantOrHasCulture(culture); + } + + /// + /// Gets the siblings of the content, of a given content type. + /// + /// The content type. + /// The content. + /// The specific culture to filter for. If null is used the current culture is used. (Default is null) + /// The siblings of the content, of the given content type. + public static IEnumerable Siblings(this IPublishedContent content, string culture = null) + where T : class, IPublishedContent + { + return content.Parent != null + ? content.Parent.Children(culture) + : PublishedSnapshot.Content.GetAtRoot().OfType().WhereIsInvariantOrHasCulture(culture); + } + + #endregion + #region Axes: custom ///