From 7b6bf780ce28edc560e8a9f5f28f55bf45fac57c Mon Sep 17 00:00:00 2001 From: nikolajlauridsen Date: Wed, 6 Oct 2021 14:30:26 +0200 Subject: [PATCH] Add GetAncestorIds extension method --- .../Extensions/ContentExtensions.cs | 14 ++++++++--- .../Services/Implement/ContentService.cs | 24 ++++++++++--------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/Umbraco.Core/Extensions/ContentExtensions.cs b/src/Umbraco.Core/Extensions/ContentExtensions.cs index 8385de5e70..daca62926a 100644 --- a/src/Umbraco.Core/Extensions/ContentExtensions.cs +++ b/src/Umbraco.Core/Extensions/ContentExtensions.cs @@ -3,15 +3,15 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.IO; using System.Linq; using System.Xml.Linq; +using Umbraco.Cms.Core; using Umbraco.Cms.Core.IO; using Umbraco.Cms.Core.Models; using Umbraco.Cms.Core.Models.Membership; -using Umbraco.Cms.Core.Models.PublishedContent; using Umbraco.Cms.Core.PropertyEditors; -using Umbraco.Cms.Core.Serialization; using Umbraco.Cms.Core.Services; using Umbraco.Cms.Core.Strings; @@ -165,7 +165,15 @@ namespace Umbraco.Extensions return ContentStatus.Unpublished; } - + /// + /// Gets a collection containing the ids of all ancestors. + /// + /// to retrieve ancestors for + /// An Enumerable list of integer ids + public static IEnumerable GetAncestorIds(this IContent content) => + content.Path.Split(Constants.CharArrays.Comma) + .Where(x => x != Constants.System.RootString && x != content.Id.ToString(CultureInfo.InvariantCulture)).Select(s => + int.Parse(s, CultureInfo.InvariantCulture)); #endregion diff --git a/src/Umbraco.Infrastructure/Services/Implement/ContentService.cs b/src/Umbraco.Infrastructure/Services/Implement/ContentService.cs index 15490bcd04..21c365dd8e 100644 --- a/src/Umbraco.Infrastructure/Services/Implement/ContentService.cs +++ b/src/Umbraco.Infrastructure/Services/Implement/ContentService.cs @@ -531,18 +531,20 @@ namespace Umbraco.Cms.Core.Services.Implement public IEnumerable GetAncestors(IContent content) { //null check otherwise we get exceptions - if (content.Path.IsNullOrWhiteSpace()) return Enumerable.Empty(); - - var rootId = Cms.Core.Constants.System.RootString; - var ids = content.Path.Split(Constants.CharArrays.Comma) - .Where(x => x != rootId && x != content.Id.ToString(CultureInfo.InvariantCulture)).Select(s => - int.Parse(s, CultureInfo.InvariantCulture)).ToArray(); - if (ids.Any() == false) - return new List(); - - using (var scope = ScopeProvider.CreateScope(autoComplete: true)) + if (content.Path.IsNullOrWhiteSpace()) { - scope.ReadLock(Cms.Core.Constants.Locks.ContentTree); + return Enumerable.Empty(); + } + + var ids = content.GetAncestorIds().ToArray(); + if (ids.Any() == false) + { + return new List(); + } + + using (IScope scope = ScopeProvider.CreateScope(autoComplete: true)) + { + scope.ReadLock(Constants.Locks.ContentTree); return _documentRepository.GetMany(ids); } }