|
| 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 creating content editors are presented with a dialog where they 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 | +Sometimes implementors or packages have a requirement to use some additional logic to determine which options are available. |
| 10 | + |
| 11 | +This is possible using content type filters. |
| 12 | + |
| 13 | +{% hint style="info" %} |
| 14 | +The uses 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. One is for filtering the content types allowed at the content root. The other is for the content types allowed below a given parent node. |
| 22 | + |
| 23 | +If you don't want to filter for one or other method, you can just return the provided collection unmodified. |
| 24 | + |
| 25 | +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. |
| 26 | + |
| 27 | +Here we are querying the existing content available at the root. Normally we can create a "Home Page" here, but if one already exists, we remove the option: |
| 28 | + |
| 29 | +```csharp |
| 30 | +internal class OneHomePageOnlyContentTypeFilter : IContentTypeFilter |
| 31 | +{ |
| 32 | + private readonly IContentService _contentService; |
| 33 | + |
| 34 | + public OneHomePageOnlyContentTypeFilter(IContentService contentService) => _contentService = contentService; |
| 35 | + |
| 36 | + public Task<IEnumerable<TItem>> FilterAllowedAtRootAsync<TItem>(IEnumerable<TItem> contentTypes) |
| 37 | + where TItem : IContentTypeComposition |
| 38 | + { |
| 39 | + var docTypeAliasesToExclude = new List<string>(); |
| 40 | + |
| 41 | + const string HomePageDocTypeAlias = "homePage"; |
| 42 | + var docTypeAliasesAtRoot = _contentService.GetRootContent() |
| 43 | + .Select(x => x.ContentType.Alias) |
| 44 | + .Distinct() |
| 45 | + .ToList(); |
| 46 | + if (docTypeAliasesAtRoot.Contains(HomePageDocTypeAlias)) |
| 47 | + { |
| 48 | + docTypeAliasesToExclude.Add(HomePageDocTypeAlias); |
| 49 | + } |
| 50 | + |
| 51 | + return Task.FromResult(contentTypes |
| 52 | + .Where(x => docTypeAliasesToExclude.Contains(x.Alias) is false)); |
| 53 | + } |
| 54 | + |
| 55 | + public Task<IEnumerable<ContentTypeSort>> FilterAllowedChildrenAsync(IEnumerable<ContentTypeSort> contentTypes, Guid parentKey) |
| 56 | + => Task.FromResult(contentTypes); |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +Content type filters are registered as a collection, so it's possible to have more than one either in the solution or an installed package. |
| 61 | + |
| 62 | +You use a composer to register the filters: |
| 63 | + |
| 64 | +```csharp |
| 65 | +public class MyComposer : IComposer |
| 66 | +{ |
| 67 | + public void Compose(IUmbracoBuilder builder) |
| 68 | + { |
| 69 | + builder.ContentTypeFilters() |
| 70 | + .Append<OneHomePageOnlyContentTypeFilter>(); |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
0 commit comments