Skip to content

Commit

Permalink
Add MenuRenderingNotification Example
Browse files Browse the repository at this point in the history
  • Loading branch information
erikjanwestendorp committed Jan 3, 2023
1 parent e641c96 commit 5efc908
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
35 changes: 35 additions & 0 deletions Umbraco.Docs.Samples.Web/Notifications/TreeNotificationHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.Security;

namespace Umbraco.Docs.Samples.Web.Notifications;

// https://docs.umbraco.com/umbraco-cms/extending/section-trees/trees#menurenderingnotification
internal class TreeNotificationHandler : INotificationHandler<MenuRenderingNotification>
{
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor;
public TreeNotificationHandler(IBackOfficeSecurityAccessor backOfficeSecurityAccessor)
{
_backOfficeSecurityAccessor = backOfficeSecurityAccessor;
}

public void Handle(MenuRenderingNotification notification)
{
// this example will add a custom menu item for all admin users
// for all content tree nodes
if (notification.TreeAlias.Equals("content") && _backOfficeSecurityAccessor.BackOfficeSecurity?.CurrentUser != null && _backOfficeSecurityAccessor.BackOfficeSecurity.CurrentUser.IsAdmin())
{
// Creates a menu action that will open /umbraco/currentSection/itemAlias.html
var menuItem = new Umbraco.Cms.Core.Models.Trees.MenuItem("itemAlias", "Item name");

// optional, if you don't want to follow the naming conventions, but do want to use a angular view
// you can also use a direct path "../App_Plugins/my/long/url/to/view.html"
menuItem.AdditionalData.Add("actionView", "my/long/url/to/view.html");

// sets the icon to icon-wine-glass
menuItem.Icon = "wine-glass";
// insert at index 5
notification.Menu.Items.Insert(5, menuItem);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public static IUmbracoBuilder AddDocsSamplesNotifications(this IUmbracoBuilder b
.AddNotificationHandler<ContentUnpublishingNotification, AllUnPublishingCheck>()
.AddNotificationHandler<SendingContentNotification, EditorSendingContentNotificationHandler>()
.AddNotificationHandler<SendingMemberNotification, EditorSendingMemberNotificationHandler>()
.AddNotificationHandler<SendingAllowedChildrenNotification, SendingAllowedChildrenNotificationHandler>();
.AddNotificationHandler<SendingAllowedChildrenNotification, SendingAllowedChildrenNotificationHandler>()
.AddNotificationHandler<MenuRenderingNotification, TreeNotificationHandler>();

return builder;
}
}
Expand Down

0 comments on commit 5efc908

Please sign in to comment.