#3305 Added optional culture parameter to Children and Descendants extension methods
This commit is contained in:
@@ -864,79 +864,79 @@ namespace Umbraco.Web
|
||||
return content.DescendantsOrSelf(level).OfType<T>();
|
||||
}
|
||||
|
||||
public static IPublishedContent Descendant(this IPublishedContent content)
|
||||
public static IPublishedContent Descendant(this IPublishedContent content, string culture = null)
|
||||
{
|
||||
return content.Children.FirstOrDefault();
|
||||
return content.Children(culture).FirstOrDefault();
|
||||
}
|
||||
|
||||
public static IPublishedContent Descendant(this IPublishedContent content, int level)
|
||||
public static IPublishedContent Descendant(this IPublishedContent content, int level, string culture = null)
|
||||
{
|
||||
return content.EnumerateDescendants(false).FirstOrDefault(x => x.Level == level);
|
||||
return content.EnumerateDescendants(false, culture).FirstOrDefault(x => x.Level == level);
|
||||
}
|
||||
|
||||
public static IPublishedContent Descendant(this IPublishedContent content, string contentTypeAlias)
|
||||
public static IPublishedContent Descendant(this IPublishedContent content, string contentTypeAlias, string culture = null)
|
||||
{
|
||||
return content.EnumerateDescendants(false).FirstOrDefault(x => x.ContentType.Alias == contentTypeAlias);
|
||||
return content.EnumerateDescendants(false, culture).FirstOrDefault(x => x.ContentType.Alias == contentTypeAlias);
|
||||
}
|
||||
|
||||
public static T Descendant<T>(this IPublishedContent content)
|
||||
public static T Descendant<T>(this IPublishedContent content, string culture = null)
|
||||
where T : class, IPublishedContent
|
||||
{
|
||||
return content.EnumerateDescendants(false).FirstOrDefault(x => x is T) as T;
|
||||
return content.EnumerateDescendants(false, culture).FirstOrDefault(x => x is T) as T;
|
||||
}
|
||||
|
||||
public static T Descendant<T>(this IPublishedContent content, int level)
|
||||
public static T Descendant<T>(this IPublishedContent content, int level, string culture = null)
|
||||
where T : class, IPublishedContent
|
||||
{
|
||||
return content.Descendant(level) as T;
|
||||
return content.Descendant(level, culture) as T;
|
||||
}
|
||||
|
||||
public static IPublishedContent DescendantOrSelf(this IPublishedContent content)
|
||||
public static IPublishedContent DescendantOrSelf(this IPublishedContent content, string culture = null)
|
||||
{
|
||||
return content;
|
||||
}
|
||||
|
||||
public static IPublishedContent DescendantOrSelf(this IPublishedContent content, int level)
|
||||
public static IPublishedContent DescendantOrSelf(this IPublishedContent content, int level, string culture = null)
|
||||
{
|
||||
return content.EnumerateDescendants(true).FirstOrDefault(x => x.Level == level);
|
||||
return content.EnumerateDescendants(true, culture).FirstOrDefault(x => x.Level == level);
|
||||
}
|
||||
|
||||
public static IPublishedContent DescendantOrSelf(this IPublishedContent content, string contentTypeAlias)
|
||||
public static IPublishedContent DescendantOrSelf(this IPublishedContent content, string contentTypeAlias, string culture = null)
|
||||
{
|
||||
return content.EnumerateDescendants(true).FirstOrDefault(x => x.ContentType.Alias == contentTypeAlias);
|
||||
return content.EnumerateDescendants(true, culture).FirstOrDefault(x => x.ContentType.Alias == contentTypeAlias);
|
||||
}
|
||||
|
||||
public static T DescendantOrSelf<T>(this IPublishedContent content)
|
||||
public static T DescendantOrSelf<T>(this IPublishedContent content, string culture = null)
|
||||
where T : class, IPublishedContent
|
||||
{
|
||||
return content.EnumerateDescendants(true).FirstOrDefault(x => x is T) as T;
|
||||
return content.EnumerateDescendants(true, culture).FirstOrDefault(x => x is T) as T;
|
||||
}
|
||||
|
||||
public static T DescendantOrSelf<T>(this IPublishedContent content, int level)
|
||||
public static T DescendantOrSelf<T>(this IPublishedContent content, int level, string culture = null)
|
||||
where T : class, IPublishedContent
|
||||
{
|
||||
return content.DescendantOrSelf(level) as T;
|
||||
return content.DescendantOrSelf(level, culture) as T;
|
||||
}
|
||||
|
||||
internal static IEnumerable<IPublishedContent> DescendantsOrSelf(this IPublishedContent content, bool orSelf, Func<IPublishedContent, bool> func)
|
||||
internal static IEnumerable<IPublishedContent> DescendantsOrSelf(this IPublishedContent content, bool orSelf, Func<IPublishedContent, bool> func, string culture = null)
|
||||
{
|
||||
return content.EnumerateDescendants(orSelf).Where(x => func == null || func(x));
|
||||
return content.EnumerateDescendants(orSelf, culture).Where(x => func == null || func(x));
|
||||
}
|
||||
|
||||
internal static IEnumerable<IPublishedContent> EnumerateDescendants(this IPublishedContent content, bool orSelf)
|
||||
internal static IEnumerable<IPublishedContent> EnumerateDescendants(this IPublishedContent content, bool orSelf, string culture = null)
|
||||
{
|
||||
if (content == null) throw new ArgumentNullException(nameof(content));
|
||||
if (orSelf) yield return content;
|
||||
|
||||
foreach (var desc in content.Children.SelectMany(x => x.EnumerateDescendants()))
|
||||
foreach (var desc in content.Children(culture).SelectMany(x => x.EnumerateDescendants()))
|
||||
yield return desc;
|
||||
}
|
||||
|
||||
internal static IEnumerable<IPublishedContent> EnumerateDescendants(this IPublishedContent content)
|
||||
internal static IEnumerable<IPublishedContent> EnumerateDescendants(this IPublishedContent content, string culture = null)
|
||||
{
|
||||
yield return content;
|
||||
|
||||
foreach (var desc in content.Children.SelectMany(x => x.EnumerateDescendants()))
|
||||
foreach (var desc in content.Children(culture).SelectMany(x => x.EnumerateDescendants()))
|
||||
yield return desc;
|
||||
}
|
||||
|
||||
@@ -1022,6 +1022,23 @@ namespace Umbraco.Web
|
||||
|
||||
#region Axes: children
|
||||
|
||||
|
||||
private static IEnumerable<IPublishedContent> WhereHasCulture(this IEnumerable<IPublishedContent> contents, string culture = null)
|
||||
{
|
||||
if (contents == null) throw new ArgumentNullException(nameof(contents));
|
||||
|
||||
var actualCulture = culture ?? GetCurrentCulture();
|
||||
|
||||
return contents.Where(x=>x.HasCulture(actualCulture) ||x.HasCulture(null));
|
||||
}
|
||||
|
||||
private static string GetCurrentCulture()
|
||||
{
|
||||
|
||||
//Review: is this the correct way to get the current culture?
|
||||
return System.Threading.Thread.CurrentThread.CurrentUICulture.Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the children of the content.
|
||||
/// </summary>
|
||||
@@ -1031,10 +1048,10 @@ namespace Umbraco.Web
|
||||
/// <para>Children are sorted by their sortOrder.</para>
|
||||
/// <para>This method exists for consistency, it is the same as calling content.Children as a property.</para>
|
||||
/// </remarks>
|
||||
public static IEnumerable<IPublishedContent> Children(this IPublishedContent content)
|
||||
public static IEnumerable<IPublishedContent> Children(this IPublishedContent content, string culture = null)
|
||||
{
|
||||
if (content == null) throw new ArgumentNullException(nameof(content));
|
||||
return content.Children;
|
||||
return content.Children.WhereHasCulture(culture);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -1046,9 +1063,9 @@ namespace Umbraco.Web
|
||||
/// <remarks>
|
||||
/// <para>Children are sorted by their sortOrder.</para>
|
||||
/// </remarks>
|
||||
public static IEnumerable<IPublishedContent> Children(this IPublishedContent content, Func<IPublishedContent, bool> predicate)
|
||||
public static IEnumerable<IPublishedContent> Children(this IPublishedContent content, Func<IPublishedContent, bool> predicate, string culture = null)
|
||||
{
|
||||
return content.Children().Where(predicate);
|
||||
return content.Children(culture).Where(predicate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -1057,9 +1074,9 @@ namespace Umbraco.Web
|
||||
/// <param name="content">The content.</param>
|
||||
/// <param name="alias">One or more content type alias.</param>
|
||||
/// <returns>The children of the content, of any of the specified types.</returns>
|
||||
public static IEnumerable<IPublishedContent> Children(this IPublishedContent content, params string[] alias)
|
||||
public static IEnumerable<IPublishedContent> Children(this IPublishedContent content, string culture = null, params string[] alias)
|
||||
{
|
||||
return content.Children(x => alias.InvariantContains(x.ContentType.Alias));
|
||||
return content.Children(x => alias.InvariantContains(x.ContentType.Alias), culture);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -1071,15 +1088,15 @@ namespace Umbraco.Web
|
||||
/// <remarks>
|
||||
/// <para>Children are sorted by their sortOrder.</para>
|
||||
/// </remarks>
|
||||
public static IEnumerable<T> Children<T>(this IPublishedContent content)
|
||||
public static IEnumerable<T> Children<T>(this IPublishedContent content, string culture = null)
|
||||
where T : class, IPublishedContent
|
||||
{
|
||||
return content.Children().OfType<T>();
|
||||
return content.Children(culture).OfType<T>();
|
||||
}
|
||||
|
||||
public static IPublishedContent FirstChild(this IPublishedContent content)
|
||||
public static IPublishedContent FirstChild(this IPublishedContent content, string culture = null)
|
||||
{
|
||||
return content.Children().FirstOrDefault();
|
||||
return content.Children(culture).FirstOrDefault();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -1088,26 +1105,26 @@ namespace Umbraco.Web
|
||||
/// <param name="content">The content.</param>
|
||||
/// <param name="alias">The content type alias.</param>
|
||||
/// <returns>The first child of content, of the given content type.</returns>
|
||||
public static IPublishedContent FirstChild(this IPublishedContent content, string alias)
|
||||
public static IPublishedContent FirstChild(this IPublishedContent content, string alias, string culture = null)
|
||||
{
|
||||
return content.Children(alias).FirstOrDefault();
|
||||
return content.Children(culture,alias).FirstOrDefault();
|
||||
}
|
||||
|
||||
public static IPublishedContent FirstChild(this IPublishedContent content, Func<IPublishedContent, bool> predicate)
|
||||
public static IPublishedContent FirstChild(this IPublishedContent content, Func<IPublishedContent, bool> predicate, string culture = null)
|
||||
{
|
||||
return content.Children(predicate).FirstOrDefault();
|
||||
return content.Children(predicate, culture).FirstOrDefault();
|
||||
}
|
||||
|
||||
public static T FirstChild<T>(this IPublishedContent content)
|
||||
public static T FirstChild<T>(this IPublishedContent content, string culture = null)
|
||||
where T : class, IPublishedContent
|
||||
{
|
||||
return content.Children<T>().FirstOrDefault();
|
||||
return content.Children<T>(culture).FirstOrDefault();
|
||||
}
|
||||
|
||||
public static T FirstChild<T>(this IPublishedContent content, Func<T, bool> predicate)
|
||||
public static T FirstChild<T>(this IPublishedContent content, Func<T, bool> predicate, string culture = null)
|
||||
where T : class, IPublishedContent
|
||||
{
|
||||
return content.Children<T>().FirstOrDefault(predicate);
|
||||
return content.Children<T>(culture).FirstOrDefault(predicate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -1117,9 +1134,9 @@ namespace Umbraco.Web
|
||||
/// <param name="services">A service context.</param>
|
||||
/// <param name="contentTypeAliasFilter">An optional content type alias.</param>
|
||||
/// <returns>The children of the content.</returns>
|
||||
public static DataTable ChildrenAsTable(this IPublishedContent content, ServiceContext services, string contentTypeAliasFilter = "")
|
||||
public static DataTable ChildrenAsTable(this IPublishedContent content, ServiceContext services, string contentTypeAliasFilter = "", string culture = null)
|
||||
{
|
||||
return GenerateDataTable(content, services, contentTypeAliasFilter);
|
||||
return GenerateDataTable(content, services, contentTypeAliasFilter, culture);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -1129,13 +1146,13 @@ namespace Umbraco.Web
|
||||
/// <param name="services">A service context.</param>
|
||||
/// <param name="contentTypeAliasFilter">An optional content type alias.</param>
|
||||
/// <returns>The children of the content.</returns>
|
||||
private static DataTable GenerateDataTable(IPublishedContent content, ServiceContext services, string contentTypeAliasFilter = "")
|
||||
private static DataTable GenerateDataTable(IPublishedContent content, ServiceContext services, string contentTypeAliasFilter = "", string culture = null)
|
||||
{
|
||||
var firstNode = contentTypeAliasFilter.IsNullOrWhiteSpace()
|
||||
? content.Children.Any()
|
||||
? content.Children.ElementAt(0)
|
||||
? content.Children(culture).Any()
|
||||
? content.Children(culture).ElementAt(0)
|
||||
: null
|
||||
: content.Children.FirstOrDefault(x => x.ContentType.Alias == contentTypeAliasFilter);
|
||||
: content.Children(culture).FirstOrDefault(x => x.ContentType.Alias == contentTypeAliasFilter);
|
||||
if (firstNode == null)
|
||||
return new DataTable(); //no children found
|
||||
|
||||
|
||||
Reference in New Issue
Block a user