Content Type Filters
Describes how to use Content Type Filters to restrict the allowed content options available to editors.
Filtering Allowed Content Types
Implementing a Content Type Filter
Example Use Case
internal class OneHomePageOnlyContentTypeFilter : IContentTypeFilter
{
private readonly IContentService _contentService;
public OneHomePageOnlyContentTypeFilter(IContentService contentService) => _contentService = contentService;
public Task<IEnumerable<TItem>> FilterAllowedAtRootAsync<TItem>(IEnumerable<TItem> contentTypes)
where TItem : IContentTypeComposition
{
var docTypeAliasesToExclude = new List<string>();
const string HomePageDocTypeAlias = "homePage";
var docTypeAliasesAtRoot = _contentService.GetRootContent()
.Select(x => x.ContentType.Alias)
.Distinct()
.ToList();
if (docTypeAliasesAtRoot.Contains(HomePageDocTypeAlias))
{
docTypeAliasesToExclude.Add(HomePageDocTypeAlias);
}
return Task.FromResult(contentTypes
.Where(x => docTypeAliasesToExclude.Contains(x.Alias) is false));
}
public Task<IEnumerable<ContentTypeSort>> FilterAllowedChildrenAsync(IEnumerable<ContentTypeSort> contentTypes, Guid parentKey)
=> Task.FromResult(contentTypes);
}Last updated
Was this helpful?