Dynamic root query step
s

skttl

9 months ago
Has anyone in here worked with creating custom dynamic root node query steps (https://docs.umbraco.com/umbraco-cms/13.latest/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/multinode-treepicker#adding-a-custom-query-step)? I'm trying to do a query step, to find a global settings node in the root of my content tree. I think I've come to the conclusion to do it like below, but curious if others has done it in other ways.
cs
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.DynamicRoot.QuerySteps;
using Umbraco.Cms.Core.Extensions;
using Umbraco.Cms.Core.Services;

namespace Website.Core.DynamicRoot;

public class GlobalDataNodeDynamicRootQueryStep : IDynamicRootQueryStep
{
    private readonly IContentService _contentService;

    public GlobalDataNodeDynamicRootQueryStep(IContentService contentService)
    {
        _contentService = contentService;
    }

    protected virtual string SupportedDirectionAlias { get; set; } = "GlobalDataNode";

    public async Task<Attempt<ICollection<Guid>>> ExecuteAsync(ICollection<Guid> origins, DynamicRootQueryStep filter)
    {
        await Task.Run(() => { });

        if (filter.Alias != SupportedDirectionAlias)
        {
            return Attempt<ICollection<Guid>>.Fail();
        }

        if (origins.Count < 1)
        {
            return Attempt<ICollection<Guid>>.Succeed(Array.Empty<Guid>());
        }

        var globalDataNode = _contentService.GetRootContent().FirstOrDefault(x => x.ContentType.Alias == ContentModels.GlobalDataNode.ModelTypeAlias);

        if (globalDataNode == null)
        {
            return Attempt<ICollection<Guid>>.Succeed(Array.Empty<Guid>());
        }

        return Attempt<ICollection<Guid>>.Succeed(globalDataNode.Key.ToSingleItemCollection());
    }
}