|
| 1 | +--- |
| 2 | +description: Describes how to use provide flags in management API responses for use in presenting additional details to consumers. |
| 3 | +--- |
| 4 | + |
| 5 | +# Flag Providers |
| 6 | + |
| 7 | +The Umbraco management APIs for trees, collections, and items include a `flags` collection for each item. These indicate additional information for the item that can be presented to users. |
| 8 | + |
| 9 | +The Umbraco backoffice uses this to provide additional overlay icons on tree, collection, and item presentations. This core behavior and extension point is described in the article on [backoffice signs](../customizing/back-office-signs.md). |
| 10 | + |
| 11 | +## Core Flag Providers |
| 12 | + |
| 13 | +Umbraco ships with four flag providers that will provide indications of the following document or media states: |
| 14 | + |
| 15 | +- **Is Protected** - indicates the document is not available for public access. |
| 16 | +- **Has Pending Changes** - indicates that the document is published but has changes in draft waiting for publication |
| 17 | +- **Has Schedule** - indicates that the document is scheduled for publishing in the future |
| 18 | +- **Has Collection** - indicates that the document or media is based on a content type that is configured for display as a collection |
| 19 | + |
| 20 | +For example, an item that is scheduled for publication would contain the following in the tree, collection or item API response: |
| 21 | + |
| 22 | +```json |
| 23 | + "flags": [ |
| 24 | + { |
| 25 | + "alias": "Umb.ScheduledForPublish" |
| 26 | + } |
| 27 | + ], |
| 28 | +``` |
| 29 | + |
| 30 | +## Providing a Custom Flag Provider |
| 31 | + |
| 32 | +If your package or project needs to present additional information for a tree, collection or item value, a custom flag provider can be created. This will be coupled with a presentation extension to determine how the information is interpreted for display as a [backoffice sign](../customizing/back-office-signs.md). |
| 33 | + |
| 34 | +To create a flag provider, implement the `IFlagProvider` interface. There are two methods to implement: |
| 35 | + |
| 36 | +- `CanProvideFlags<TItem>` - returns `bool` indicating whether this provider can provide flags for the tree, collection or item view model. |
| 37 | +- `PopulateFlagsAsync<TItem>(IEnumerable<TItem> itemViewModels)` - populates the `Flags` property for the provided collection of view models. |
| 38 | + |
| 39 | +An illustrative implementation is as follows: |
| 40 | + |
| 41 | +```csharp |
| 42 | +using Umbraco.Cms.Api.Management.ViewModels; |
| 43 | +using Umbraco.Cms.Core; |
| 44 | + |
| 45 | +internal class MyDocumentFlagProvider : IFlagProvider |
| 46 | +{ |
| 47 | + private const string Alias = Constants.Conventions.Flags.Prefix + "MyDocumentFlag"; |
| 48 | + |
| 49 | + // Indicate that this flag provider only provides flags for documents. |
| 50 | + public bool CanProvideFlags<TItem>() |
| 51 | + where TItem : IHasFlags => |
| 52 | + typeof(TItem) == typeof(DocumentTreeItemResponseModel) || |
| 53 | + typeof(TItem) == typeof(DocumentCollectionResponseModel) || |
| 54 | + typeof(TItem) == typeof(DocumentItemResponseModel); |
| 55 | + |
| 56 | + public Task PopulateFlagsAsync<TItem>(IEnumerable<TItem> itemViewModels) |
| 57 | + where TItem : IHasFlags |
| 58 | + { |
| 59 | + foreach (TItem item in itemViewModels) |
| 60 | + { |
| 61 | + if (ShouldAddFlag(item)) |
| 62 | + { |
| 63 | + item.AddFlag(Alias); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return Task.CompletedTask; |
| 68 | + } |
| 69 | + |
| 70 | + private bool ShouldAddFlag(TItem item) => return true; // Provide custom logic here. |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +The flag provider needs to be registered with Umbraco in a composer or application startup with: |
| 75 | + |
| 76 | +```csharp |
| 77 | + builder.FlagProviders() |
| 78 | + .Append<MyDocumentFlagProvider>(); |
| 79 | +``` |
| 80 | + |
| 81 | +For some flags, there may be sufficient information on the view models to map whether a flag should be created. |
| 82 | + |
| 83 | +For an example of this, please see the core flag provider `IsProtectedFlagProvider` whose [source code can be found here](https://github.com/umbraco/Umbraco-CMS/blob/main/src/Umbraco.Cms.Api.Management/Services/Flags/IsProtectedFlagProvider.cs). |
| 84 | + |
| 85 | +More complex flags will require additional information, using the identifiers of the view models to retrieve the necessary data. It's important to avoid "N+1" issues. The aim should be to retrieve all the data needed to populate the flags for the whole collection in one step. |
| 86 | + |
| 87 | +In core, the flag provider `HasScheduleFlagProvider` shows a good example of this. The [source code can be found here](https://github.com/umbraco/Umbraco-CMS/blob/main/src/Umbraco.Cms.Api.Management/Services/Flags/HasScheduleFlagProvider.cs). |
0 commit comments