|
| 1 | +using Umbraco.Cms.Core.Events; |
| 2 | +using Umbraco.Cms.Core.Notifications; |
| 3 | +using Umbraco.Cms.Core.Security; |
| 4 | + |
| 5 | +namespace Umbraco.Docs.Samples.Web.Notifications; |
| 6 | + |
| 7 | +// https://docs.umbraco.com/umbraco-cms/extending/section-trees/trees#menurenderingnotification |
| 8 | +internal class TreeNotificationHandler : INotificationHandler<MenuRenderingNotification> |
| 9 | +{ |
| 10 | + private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor; |
| 11 | + public TreeNotificationHandler(IBackOfficeSecurityAccessor backOfficeSecurityAccessor) |
| 12 | + { |
| 13 | + _backOfficeSecurityAccessor = backOfficeSecurityAccessor; |
| 14 | + } |
| 15 | + |
| 16 | + public void Handle(MenuRenderingNotification notification) |
| 17 | + { |
| 18 | + // this example will add a custom menu item for all admin users |
| 19 | + // for all content tree nodes |
| 20 | + if (notification.TreeAlias.Equals("content") && _backOfficeSecurityAccessor.BackOfficeSecurity?.CurrentUser != null && _backOfficeSecurityAccessor.BackOfficeSecurity.CurrentUser.IsAdmin()) |
| 21 | + { |
| 22 | + // Creates a menu action that will open /umbraco/currentSection/itemAlias.html |
| 23 | + var menuItem = new Umbraco.Cms.Core.Models.Trees.MenuItem("itemAlias", "Item name"); |
| 24 | + |
| 25 | + // optional, if you don't want to follow the naming conventions, but do want to use a angular view |
| 26 | + // you can also use a direct path "../App_Plugins/my/long/url/to/view.html" |
| 27 | + menuItem.AdditionalData.Add("actionView", "my/long/url/to/view.html"); |
| 28 | + |
| 29 | + // sets the icon to icon-wine-glass |
| 30 | + menuItem.Icon = "wine-glass"; |
| 31 | + // insert at index 5 |
| 32 | + notification.Menu.Items.Insert(5, menuItem); |
| 33 | + } |
| 34 | + } |
| 35 | +} |
0 commit comments