Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Julio M. Vivas 61 posts 122 karma points
    Apr 26, 2022 @ 19:07
    Julio M. Vivas
    0

    Custom action on members tree

    Just completing a V8 to V9 migration that is going really well thanks to all the good advise here. However there is one feature I can't find anywhere:

    On V8 I was able to add custom actions on the members section tree using a composition and Umbraco.Web.Trees.TreeControllerBase to get hold of the MenuRendering event where an extra MenuItem could be injected, e.g.

    [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
    public class MemberComposer : IUserComposer
    {
        public void Compose(Composition composition)
        {
            composition.Components().Append<MemberComponent>();
        }
    }
    
    public class MemberComponent : IComponent
    {
        public void Initialize()
        {
            TreeControllerBase.MenuRendering += TreeControllerBase_MenuRendering;
        }
    
        public void Terminate()
        {
            TreeControllerBase.MenuRendering -= TreeControllerBase_MenuRendering;
        }
    
        private void TreeControllerBase_MenuRendering(TreeControllerBase sender, MenuRenderingEventArgs e)
        {
            if (sender.TreeAlias == "member")
            {
                var m = new MenuItem("approveUser", "Aprove User");
                m.AdditionalData.Add("actionView", "/App_Plugins/MemberApproval/MemberApprovalView.html");
                m.Icon = "umb-content";
                e.Menu.Items.Add(m);
            }
        }
    }
    

    On V9, it seems like I have to replace the composition with this

    public void Compose(IUmbracoBuilder builder)
        {
            builder.Services.AddSingleton<IMemberComponent, MemberComponent>();
    
        }
    

    However as the TreeControllerBase is not available anymore, I can't get hold of the tree events in any way.

    I have checked the official docs for trees here but I can't quite see how to inject Umbraco.Cms.Web.BackOffice.Trees.TreeController on the composer:

    https://our.umbraco.com/documentation/Extending/Section-Trees/trees#menurendering

    Any help more than welcome...

  • Bjarne Fyrstenborg 1285 posts 4039 karma points MVP 8x c-trib
    Apr 26, 2022 @ 19:31
  • Julio M. Vivas 61 posts 122 karma points
    Apr 27, 2022 @ 17:32
    Julio M. Vivas
    0

    Hi Bjarne, that's brilliant, thanks a lot.

    I managed to add it as a composer so it works similarly to what we had for V8

    public class MemberComposer : IComposer
    {
        public void Compose(IUmbracoBuilder builder)
        {
            builder.AddNotificationHandler<MenuRenderingNotification, MemberComponent>();
        }
    }
    
    public class MemberComponent : INotificationHandler<MenuRenderingNotification>
    {
        public void Handle(MenuRenderingNotification notification)
        {
            if (notification.TreeAlias.Equals("member")) {
                var m = new MenuItem("approveUser", "Aprove User");
                m.AdditionalData.Add("actionView", "/App_Plugins/MemberApproval/MemberApprovalView.html");
                m.Icon = "umb-content";
                notification.Menu.Items.Insert(5, m);
            }
        }
    }
    

    However, there is something odd going on: I usually manage all the code on a separate [client].Core project so the custom code is not affected when Umbraco is updated. And for some strange reason, on the separate project Umbraco.Cms.Core.Notifications doesn't seem to include MenuRenderingNotification which works perfectly if I add the composer directly on the Umbraco project.

    I have a reference on the separate project to the same version of the package "Umbraco.Cms.Core 9.3.1" but something is not quite right. All the other notifications are there, just not the menu rendering one. Maybe security permissions?

    Anyway, at least it works. Thanks again for the pointers.

  • Julio M. Vivas 61 posts 122 karma points
    Apr 29, 2022 @ 12:57
    Julio M. Vivas
    0

    I noticed I was missing a reference to Umbraco.Cms.Web.BackOffice on my custom code library and now everythig works as expected.

Please Sign in or register to post replies

Write your reply to:

Draft