Skip to content

Commit 5efc908

Browse files
Add MenuRenderingNotification Example
1 parent e641c96 commit 5efc908

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
}

Umbraco.Docs.Samples.Web/Notifications/UmbracoBuilderNotificationExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ public static IUmbracoBuilder AddDocsSamplesNotifications(this IUmbracoBuilder b
1919
.AddNotificationHandler<ContentUnpublishingNotification, AllUnPublishingCheck>()
2020
.AddNotificationHandler<SendingContentNotification, EditorSendingContentNotificationHandler>()
2121
.AddNotificationHandler<SendingMemberNotification, EditorSendingMemberNotificationHandler>()
22-
.AddNotificationHandler<SendingAllowedChildrenNotification, SendingAllowedChildrenNotificationHandler>();
22+
.AddNotificationHandler<SendingAllowedChildrenNotification, SendingAllowedChildrenNotificationHandler>()
23+
.AddNotificationHandler<MenuRenderingNotification, TreeNotificationHandler>();
24+
2325
return builder;
2426
}
2527
}

0 commit comments

Comments
 (0)