Skip to content

Instantly share code, notes, and snippets.

@jeremy-farrance
Last active June 4, 2022 01:48
Show Gist options
  • Save jeremy-farrance/11a65b63bb9861d802cb260ab5c9a458 to your computer and use it in GitHub Desktop.
Save jeremy-farrance/11a65b63bb9861d802cb260ab5c9a458 to your computer and use it in GitHub Desktop.
DNN (DotNetNuke) Class to build DDRMenu Nodes for use Outside of DDRMenu Templates
// lazy usage, put this file in /App_Code in your DNN instance
// the benefit of dropping this as a .cs file in /App_Code is that it
// is now available THROUGHOUT DNN. You can use it in 2sxc VIews, RazorHost,
// theme (skins/containers), etc - wherever
// the benefits of doing this over working with Tabs/TabController are many; you get permissions
// applied and all the fields (properties) you need for doing navigation links (menus), you can
// pass in settings (include or exclude nodes), etc.
//// this was mildly adapted from:
//// DNNHero Forum post, Running Razor Menu code within a module
//// https://www.dnnhero.com/Forum/forumid/6/postid/4053/scope/posts
/*
// example usage in RazorHost or 2sxc View, this should output the page names of your top level menus items
@using DotNetNuke.Web.DDRMenu;
@{
var myMenu = new MyDDRMenu();
var myNodes = myMenu.GetMenu(null);
}
@foreach(var node in (IList<MenuNode>)myNodes.Children)
{
<p>@node.Text</p>
}
*/
using System;
using DotNetNuke.Web.DDRMenu;
public class MyDDRMenu: DotNetNuke.Web.DDRMenu.ModuleBase
{
public DotNetNuke.Web.DDRMenu.MenuNode GetMenu(EventArgs e)
{
DotNetNuke.Web.DDRMenu.MenuNode rootNode = null;
using (new DotNetNuke.Web.DDRMenu.DNNCommon.DNNContext(this))
{
try
{
base.OnPreRender(e);
//var menuStyle = GetStringSetting("MenuStyle");
//if (String.IsNullOrEmpty(menuStyle))
//{
// rootNode = null;
// return rootNode;
//}
var menuSettings = new DotNetNuke.Web.DDRMenu.Settings
{
MenuStyle = "", //GetStringSetting("MenuStyle"),
NodeXmlPath = "", //GetStringSetting("NodeXmlPath"),
NodeSelector = "", //GetStringSetting("NodeSelector"),
IncludeContext = false, //GetBoolSetting("IncludeContext"),
IncludeHidden = false, //GetBoolSetting("IncludeHidden"),
IncludeNodes = "", //GetStringSetting("IncludeNodes"),
ExcludeNodes = "", //GetStringSetting("ExcludeNodes"),
NodeManipulator = "", //GetStringSetting("NodeManipulator"),
//TemplateArguments =
// DotNetNuke.Web.DDRMenu.Settings.TemplateArgumentsFromSettingString(GetStringSetting("TemplateArguments")),
//ClientOptions =
// DotNetNuke.Web.DDRMenu.Settings.ClientOptionsFromSettingString(GetStringSetting("ClientOptions"))
};
rootNode = new DotNetNuke.Web.DDRMenu.MenuNode(
DotNetNuke.Web.DDRMenu.Localisation.Localiser.LocaliseDNNNodeCollection(
DotNetNuke.UI.Navigation.GetNavigationNodes(
ClientID,
DotNetNuke.UI.Navigation.ToolTipSource.None,
-1,
-1,
GetNavNodeOptions(menuSettings.IncludeHidden)
)
)
);
}
catch (Exception Ex)
{
}
return rootNode;
}
}
// copied from DDRMenu/DNNAbstract.cs because it's internal
public static int GetNavNodeOptions(bool includeHidden)
{
return (int)DotNetNuke.UI.Navigation.NavNodeOptions.IncludeSiblings +
(int)DotNetNuke.UI.Navigation.NavNodeOptions.IncludeSelf +
(includeHidden ? (int)DotNetNuke.UI.Navigation.NavNodeOptions.IncludeHiddenNodes : 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment