Create menu action for specific node type
# help-with-umbraco
b
I want to create a specific menu action, but limit it to a content type
Business
. However I can't seem to find details on this from querystring in notification besides on node id and looking it up in content cache obvious only work if node is published.
Copy code
public class ExportNotificationHandler : INotificationHandler<MenuRenderingNotification>
{
    private readonly IPublishedContentQuery _iPublishedContentQuery;

    public ExportNotificationHandler(IPublishedContentQuery iPublishedContentQuery)
    {
        _iPublishedContentQuery = iPublishedContentQuery;
    }
    
    public void Handle(MenuRenderingNotification notification)
    {
        var test = notification.QueryString["nodeType"];

        int.TryParse(notification.NodeId, out int nodeId);

        if (_iPublishedContentQuery.Content(nodeId) is not Business)
            return;

        if (notification.TreeAlias.Equals(Umbraco.Cms.Core.Constants.Trees.Content))
            //&& notification.QueryString["nodeType"] == Business.ModelTypeAlias)
        {
            var menuItem = new Umbraco.Cms.Core.Models.Trees.MenuItem("export", "Export");
            menuItem.AdditionalData.Add("actionView", "/App_Plugins/MyPlugin/export.html");
            menuItem.Icon = "icon-download-alt";
            notification.Menu.Items.Add(menuItem);
        }
    }
}
I could handle this in Vendr Reviews, because
Store
node is set a that spefic node type: https://github.com/vendrcontrib/vendr-reviews/blob/0a3de6329f2a9f94cf15b626a1be9c80e67b8785/src/Vendr.Contrib.Reviews/Notifications/ReviewsTreeNodesNotification.cs#L58
I tried in
TreeNodesRenderingNotification
to set
NodeType
and additional data, but it doesn't seem these are available in
MenuRenderingNotification
.
Copy code
public class TreeNotificationHandler : INotificationHandler<TreeNodesRenderingNotification>
{
    public void Handle(TreeNodesRenderingNotification notification)
    {
        if (notification.TreeAlias.Equals(Umbraco.Cms.Core.Constants.Trees.Content))
        {
            foreach (var node in notification.Nodes)
            {
                if (node.AdditionalData.TryGetValue("contentType", out object? contentType) && contentType?.ToString() == Business.ModelTypeAlias)
                {
                    node.NodeType = Business.ModelTypeAlias;
                }
            }
        }
    }
}
r
Am I right in saying you can find the node in one of the notifications handlers But it’s the query string value that’s going missing
b
I get the node id in
MenuRenderingNotification
but from that I can mainly decide if the node is in content tree, but not which node type. In
TreeNodesRenderingNotification
the tree nodes are available including the
contentType
. However this doesn't seem to be passed on
MenuRenderingNotification
. https://cdn.discordapp.com/attachments/1274725978215813212/1274832144912351292/image.png?ex=66c3af74&is=66c25df4&hm=35483a1ee1e3289cc751a8a46ce3fa0807ae9d86066ba97a1c181271d2ece8ce&
r
Surely if it’s going into the if statement it’s then a content type of business.. I know it’s long winded but surely you can properties of the node by using a get by id .. and yes I know it’s not “clean” but surely it’s a few milliseconds and gets the job done..
3 Views