November Happy Hour will be moved to Thursday December 5th.

Custom Admin Menu Item with Multiple Routes

Vote:
 

I am working on a custom admin tool which uses mutliple controllers and views. 

I have created a Menu Provider class which sucessfully displays the top-level item in the Settings sidebar. Clicking it displays the entry point route as configured in the provider. However, I have other routes within the the admin tool that I want to use too. When I navigate to one of these other routes, I want to keep the sidebar navigation visible with my menu item displayed as active.

 
This code for the privider:

[MenuProvider]
public class ExampleToolMenuProvider : IMenuProvider
{
    public IEnumerable<MenuItem> GetMenuItems()
    {
        var example = new UrlMenuItem("Example", MenuPaths.Global + "/cms/admin/example/",
            "/example/index/")
        {
            IsAvailable = _ => true,
            SortIndex = 61,
            AuthorizationPolicy = CmsPolicyNames.CmsAdmin

        };

        return new List<MenuItem>(1)
        {
            example
        };
    }
}

Here's what it looks like when I select it from the menu:


My goal is that when clicking 'Two', I would be taken to the new route but mainting the 'Example' option active in the sidebar. However, the sidebar does not load, it looks like so...



Here's the controller with a MenuItem attribute on the index Action

[Authorize(Roles = Roles.CmsAdmins)]
[Route("/example/two/")]
public class TwoController : Controller
{
    [Route("[action]")]
    [HttpGet]
    [MenuItem(MenuPaths.Global + "/cms/admin/example/", Url = "/example/two/index/")]
    public IActionResult Index()
    {       
        return View("~/Index.cshtml");
    }
}

Am I doing anything wrong with the declaration of the MenuItems? I have followed the docs here - https://docs.developers.optimizely.com/content-management-system/docs/adding-and-configuring-menu-items - but did not have any luck.

Note, there is a comment on this blog post about the new menu system which is similar to my issue - https://world.optimizely.com/blogs/matthew-slim/dates/2023/6/new-menu-system-released/ 

Erik's workaround here was to 'hack' the  Html.CreatePlatformNavigationMenu() like so:

    var platformNavigation = Html.CreatePlatformNavigationMenu() as HtmlString;
    var platformNavigationContent = platformNavigation.Value.Replace("data-epi-product-id=\"\"", "data-epi-product-id=\"global_cms\"");
    platformNavigation = new HtmlString(platformNavigationContent);

This almost works, it shows 'a navigation'. However, for me, this just shows the root cms navigation sidebar, it does not show the admin sidebar with my menu item as active. 

 Does anyone have any experience with this? 

Thanks in advance

#326283
Edited, Jul 31, 2024 7:18
Vote:
 

Hi Daniel

I believe menu items are only for the top and left menu. You can render your tab menu in the main area on your own.

And as long as the action URLs begin with the same URL as the menu item, the menu item should be shown as selected.

#326292
Jul 31, 2024 9:48
Vote:
 

Hi Stefan

Thanks for the reply.

Your second statement helped, i.e. 'And as long as the action URLs begin with the same URL as the menu item, the menu item should be shown as selected.'

I had bee using the path '/example/index/' in my UrlMenuItem, the 'index' node was preventing my other actions from matching. I have changed the routing to remove the need to pass the 'index', so it's now just '/example/' and it works... mostly. 

With that updated, I still need to 'hack' the Html.CreatePlatformNavigationMenu() to get it to display the menu on the sub-routes (as mentioned earlier)... but now, at least it is showing the admin menu when I visit one of the 'sub-routes', whereas before it was just showing the root cms menu. My only problem now is that the 'active' state on the menu item is only there when I am on the top-level route/url. 

So for the most part, this is working, but if anyone knows a better way to do this without the 'hack' or how I can further hack it to show the current menu item as active/selected when on a sub-route, please let me know.

#326298
Edited, Jul 31, 2024 11:00
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.