Created
April 23, 2020 11:42
-
-
Save dsreitan/7c6be2df056a0f630f811613c6ae9062 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ServiceConfiguration(typeof(IContentQuery))] | |
public class SiteGetChildrenQuery : GetChildrenQuery | |
{ | |
private readonly LanguageSelectorFactory _languageSelectorFactory; | |
public SiteGetChildrenQuery(IContentQueryHelper queryHelper, IContentRepository contentRepository, LanguageSelectorFactory languageSelectorFactory) | |
: base(queryHelper, contentRepository, languageSelectorFactory) { _languageSelectorFactory = languageSelectorFactory; } | |
public override int Rank => 100; | |
private List<int> WhiteListedContentIds => new List<int> { 1 }; | |
private List<Type> WhiteListedTypes => new List<Type> { typeof(DemoHomePage), typeof(LocationListPage) }; | |
private List<Type> OnlyFilterTheseTypes => new List<Type> { typeof(PageData) }; | |
protected override IEnumerable<IContent> GetContent(ContentQueryParameters parameters) | |
{ | |
var types = parameters.TypeIdentifiers | |
.Select(typeName => TypeResolver.GetType(typeName, false)) | |
.Where(type => type != null); | |
if (OnlyFilterTheseTypes.Any() && !OnlyFilterTheseTypes.Any(x => types.Any(type => type.IsAssignableFrom(x)))) | |
return base.GetContent(parameters); | |
if (!ContentReference.IsNullOrEmpty(parameters.ReferenceId)) | |
return GetChildren(types, parameters.ReferenceId, _languageSelectorFactory.AutoDetect(true)); | |
if (parameters.References?.Any() != true) return Enumerable.Empty<IContent>(); | |
return parameters.References.SelectMany(link => GetChildren(types, link, _languageSelectorFactory.AutoDetect(true))); | |
} | |
private IEnumerable<IContent> GetChildren(IEnumerable<Type> types, ContentReference parentId, LanguageSelector selector) | |
{ | |
if (types?.Any() != true) return Enumerable.Empty<IContent>(); | |
return types.SelectMany(type => GetChildrenByType(type, parentId, selector)) | |
.Distinct() | |
.Where(content => WhiteListedContentIds.Contains(content.ContentLink.ID) || | |
WhiteListedTypes.Any(type => content.GetOriginalType().IsAssignableFrom(type)) || | |
FilterAccess.QueryDistinctAccessEdit(content, AccessLevel.Edit) || | |
FilterAccess.QueryDistinctAccessEdit(content, AccessLevel.Create)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment