getting menu items

Topics: Customizing Orchard, Writing modules
Apr 7, 2011 at 8:37 AM

Hello, it's me again

Last time to get menu items i did this

 

 protected override DriverResult Display(FatMenuItem part, string displayType, dynamic shapeHelper)
        {
            menuitems = _menuService.Get().ToList();
            List<FatMenuItem> fatmenu = new List<FatMenuItem>();
            foreach (var menuitem in menuitems.Where(o => !o.MenuPosition.Contains("."))) 
            {
                if (menuitem.Is<MenuItemPart>())
                {
                    fatmenu.Add(AddFatFooterItem(menuitem, new FatMenuItem() { Text = menuitem.MenuText, Url = menuitem.As<MenuItemPart>().Url }));
                }
            }
return ...
}

 public FatMenuItem AddFatFooterItem(MenuPart parent, FatMenuItem fatMenuItemParent)
        {
            if (menuitems.Where(o => o.MenuPosition.StartsWith(parent.MenuPosition + ".")).Any())
            {
                int dotcount = parent.MenuPosition.Count(o => o == '.');
                foreach (MenuPart child in menuitems.Where(o => o.MenuPosition.StartsWith(parent.MenuPosition + ".") && o.MenuPosition.Count(oo => oo == '.') == parent.MenuPosition.Count(ooo => ooo == '.') + 1))
                    if (child.Is<MenuItemPart>())
                    {
                        fatMenuItemParent.ChildList.Add(AddFatFooterItem(child, new FatMenuItem() { Text = parent.MenuText, Url = parent.As<MenuItemPart>().Url }));
                    }
            }
            else 
            {
                fatMenuItemParent.Text = parent.MenuText;
                fatMenuItemParent.Url = parent.As<MenuItemPart>().Url;
            }
            return fatMenuItemParent;
        }

now if (child.Is<MenuItemPart>())

for every sub menu item (2.1,2.1.1,...) returns false, and I cant understand why?
anyone has any idea, or maybe theres other way to get all menu
items in tree structure.  Thank you

 

Apr 7, 2011 at 12:07 PM

random wru man? :)

Apr 7, 2011 at 1:58 PM

Heh. Trying to get some actual work done, instead of spending too much time on forums :)

I can't see any reason why child.Is<MenuItemPart>() would be false, since the same call returns true in the Display. Are you sure it's that line that's failing?

Apr 7, 2011 at 2:33 PM

this works

 public class FatFooterDriver : ContentPartDriver<FatMenuItem>
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly INavigationManager _nav;
        private readonly RouteCollection _routeCollection;
        private readonly IMenuService _menuService;
        private readonly IOrchardServices _services;
        private List<MenuItemEntry> menuitems;
        private List<FatMenuItem> FatMenu;

        public Localizer T { get; set; }

        public FatFooterDriver(IOrchardServices services, INotifier notifier, IHttpContextAccessor httpContextAccessor, INavigationManager nav, RouteCollection routeCollection, IMenuService menuService)
        {
            _httpContextAccessor = httpContextAccessor;
            _nav = nav;
            _routeCollection = routeCollection;
            _menuService = menuService;
            _services = services;
        }
        private MenuItemEntry CreateMenuItemEntries(MenuPart menuPart)
        {
            return new MenuItemEntry
            {
                MenuItem = new MenuItem
                {
                    Text = menuPart.MenuText,
                    Position = menuPart.MenuPosition,
                    Url = menuPart.Is<MenuItemPart>()
                              ? menuPart.As<MenuItemPart>().Url
                              : _nav.GetUrl(null, _services.ContentManager.GetItemMetadata(menuPart).DisplayRouteValues)
                },
                MenuItemId = menuPart.Id,
                IsMenuItem = menuPart.Is<MenuItemPart>()
            };
        }
        protected override DriverResult Display(FatMenuItem part, string displayType, dynamic shapeHelper)
        {
            menuitems =_menuService.Get().Select(CreateMenuItemEntries).OrderBy(menuPartEntry => menuPartEntry.MenuItem.Position, new FlatPositionComparer()).ToList();
            return ContentShape("Parts_FatFooter",
                    () => shapeHelper.Parts_FatFooter(
                     fatmenu: menuitems
                                         ));
        }

    }

Jan 5, 2014 at 9:03 AM
Found this post to be very helpful and wanted to thank both of you.