Skip to content

Instantly share code, notes, and snippets.

@dsreitan
Created April 23, 2020 11:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsreitan/7c6be2df056a0f630f811613c6ae9062 to your computer and use it in GitHub Desktop.
Save dsreitan/7c6be2df056a0f630f811613c6ae9062 to your computer and use it in GitHub Desktop.
[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