|
| 1 | +--- |
| 2 | +description: Describes how to use Content Type Filters to restrict the allowed content options available to editors. |
| 3 | +--- |
| 4 | + |
| 5 | +# Filtering Allowed Content Types |
| 6 | + |
| 7 | +When content editors add new content they are presented with a dialog where they must select the type of content they want to create. The options available are defined when setting up the Document, Media, and Member types in the **Settings** section. |
| 8 | + |
| 9 | +Implementors and package creators can add additional logic to determine which options are available to the editors. |
| 10 | + |
| 11 | +This is possible using Content Type Filters. |
| 12 | + |
| 13 | +{% hint style="info" %} |
| 14 | +The use cases supported here are similar to those where the `SendingAllowedChildrenNotification` would be used in Umbraco 13 or earlier. |
| 15 | +{% endhint %} |
| 16 | + |
| 17 | +## Implementing a Content Type Filter |
| 18 | + |
| 19 | +To create a Content Type Filter you use a class that implements the `IContentTypeFilter` interface (found in the `Umbraco.Cms.Core.Services.Filters` namespace). |
| 20 | + |
| 21 | +There are two methods you can implement: |
| 22 | + |
| 23 | +* One for filtering the content types allowed at the content root |
| 24 | +* One for the content types allowed below a given parent node. |
| 25 | + |
| 26 | +If you don't want to filter using one of the two approaches, you can return the provided collection unmodified. |
| 27 | + |
| 28 | +### Example Use Case |
| 29 | + |
| 30 | +The following example shows a typical use case. Often websites will have a "Home Page" Document Type which is created at the root. Normally, only one of these is required. You can enforce that using the following Content Type Filter. |
| 31 | + |
| 32 | +The code below is querying the existing content available at the root. Normally you can create a "Home Page" here, but if one already exists that option is removed: |
| 33 | + |
| 34 | +```csharp |
| 35 | +internal class OneHomePageOnlyContentTypeFilter : IContentTypeFilter |
| 36 | +{ |
| 37 | + private readonly IContentService _contentService; |
| 38 | + |
| 39 | + public OneHomePageOnlyContentTypeFilter(IContentService contentService) => _contentService = contentService; |
| 40 | + |
| 41 | + public Task<IEnumerable<TItem>> FilterAllowedAtRootAsync<TItem>(IEnumerable<TItem> contentTypes) |
| 42 | + where TItem : IContentTypeComposition |
| 43 | + { |
| 44 | + var docTypeAliasesToExclude = new List<string>(); |
| 45 | + |
| 46 | + const string HomePageDocTypeAlias = "homePage"; |
| 47 | + var docTypeAliasesAtRoot = _contentService.GetRootContent() |
| 48 | + .Select(x => x.ContentType.Alias) |
| 49 | + .Distinct() |
| 50 | + .ToList(); |
| 51 | + if (docTypeAliasesAtRoot.Contains(HomePageDocTypeAlias)) |
| 52 | + { |
| 53 | + docTypeAliasesToExclude.Add(HomePageDocTypeAlias); |
| 54 | + } |
| 55 | + |
| 56 | + return Task.FromResult(contentTypes |
| 57 | + .Where(x => docTypeAliasesToExclude.Contains(x.Alias) is false)); |
| 58 | + } |
| 59 | + |
| 60 | + public Task<IEnumerable<ContentTypeSort>> FilterAllowedChildrenAsync(IEnumerable<ContentTypeSort> contentTypes, Guid parentKey) |
| 61 | + => Task.FromResult(contentTypes); |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +Content Type Filters are registered as a collection, making it possible to have more than one in the solution or an installed package. |
| 66 | + |
| 67 | +The filters need to be registered in a composer: |
| 68 | + |
| 69 | +```csharp |
| 70 | +public class MyComposer : IComposer |
| 71 | +{ |
| 72 | + public void Compose(IUmbracoBuilder builder) |
| 73 | + { |
| 74 | + builder.ContentTypeFilters() |
| 75 | + .Append<OneHomePageOnlyContentTypeFilter>(); |
| 76 | + } |
| 77 | +} |
| 78 | +``` |
0 commit comments