using Umbraco.Cms.Core.Extensions; using Umbraco.Cms.Core.Scoping; using Umbraco.Extensions; namespace Umbraco.Cms.Core.DynamicRoot.QuerySteps; public class NearestAncestorOrSelfDynamicRootQueryStep : IDynamicRootQueryStep { private readonly ICoreScopeProvider _scopeProvider; private readonly IDynamicRootRepository _nodeFilterRepository; public NearestAncestorOrSelfDynamicRootQueryStep(ICoreScopeProvider scopeProvider, IDynamicRootRepository nodeFilterRepository) { _scopeProvider = scopeProvider; _nodeFilterRepository = nodeFilterRepository; } public virtual string SupportedDirectionAlias { get; set; } = "NearestAncestorOrSelf"; public async Task>> ExecuteAsync(ICollection origins, DynamicRootQueryStep filter) { if (filter.Alias != SupportedDirectionAlias) { return Attempt>.Fail(); } if (origins.Count < 1) { return Attempt>.Succeed(Array.Empty()); } using ICoreScope scope = _scopeProvider.CreateCoreScope(autoComplete: true); var result = (await _nodeFilterRepository.NearestAncestorOrSelfAsync(origins, filter))?.ToSingleItemCollection() ?? Array.Empty(); return Attempt>.Succeed(result); } }