Thursday, 9 July 2009

How to add items to the Document Types context menu.

I am currently working on an extension to Umbraco that needed an extra menu item on the Document Types menu in the Settings section, my first thought was to use the BeforeNodeRender event to add the menu item, but this event only fires on the Content and Media menu trees.

See: Codeplex Issue 21623

The "temporary" solution has been to extend the "loadNodeTypes" class, I say temporary because I assume that the core team may well extend the event functionality to the entire menu tree in the future which would be much better and would prevent you from having to complete override the menu items.

This was suggested by Slace on my "is it possible to add an item to the context menu in tree's other than media content" post on the our.umbraco.org site.

There are three things you need to do:

1) Override the loadNodeTypes

using System.Collections.Generic;
using umbraco.interfaces;
using umbraco.BusinessLogic.Actions;

namespace Vizioz.xMind2Umbraco
{
// Note: Remember these menu's might change in future versions of Umbraco

public class loadNodeTypesMenu : umbraco.loadNodeTypes
{
public loadNodeTypesMenu(string application) : base(application) { }

protected override void CreateRootNodeActions(ref List<IAction> actions)
{
actions.Clear();
actions.Add(ActionNew.Instance);
actions.Add(xMindImportAction.Instance);
actions.Add(ContextMenuSeperator.Instance);
actions.Add(ActionImport.Instance);
actions.Add(ContextMenuSeperator.Instance);
actions.Add(ActionRefresh.Instance);
}
protected override void CreateAllowedActions(ref List<IAction> actions)
{
actions.Clear();
actions.Add(ActionCopy.Instance);
actions.Add(xMindImportAction.Instance);
actions.Add(ContextMenuSeperator.Instance);
actions.Add(ActionExport.Instance);
actions.Add(ContextMenuSeperator.Instance);
actions.Add(ActionDelete.Instance);
}
}
}


2) Create a new Action to be used on the menu
using umbraco.interfaces;

namespace Vizioz.xMind2Umbraco
{
public class xMindImportAction : IAction
{
#region Implementation of IAction

public static xMindImportAction Instance
{
get { return umbraco.Singleton<xMindImportAction>.Instance; }
}

public char Letter
{
get { return 'p'; }
}

public bool ShowInNotifier
{
get { return true; }
}

public bool CanBePermissionAssigned
{
get { return true; }
}

public string Icon
{
get { return "editor/OPEN.gif"; }
}

public string Alias
{
get { return "Import from xMind"; }
}

public string JsFunctionName
{
get
{
return "openModal('dialogs/xMindImport.aspx?id=' + nodeID,
'Publish Management', 550, 480);";
}
}

public string JsSource
{
get { return string.Empty; }
}

#endregion
}
}


3) Update the UmbracoAppTree table, my case I changed the following:

TreeHandlerType
From: loadNodeTypes
To: loadNodeTypesMenu

TreeHandlerAssembly
From: umbraco
To: Vizioz.xMind2Umbraco

And the result is:

4 comments:

  1. Just a note - you don't need to implement all the action adding yourself, unless you want to do what Chris has done and inject your action somewhere other than the end.
    If you want it at the end you can just call the base method then your own IAction addition.

    Also, if anyone is wondering why there are [] around the menu item (something I just learnt) it's because there is no entry in the language file for that action (same goes where ever you see [] in Umbraco)

    ReplyDelete
  2. Hi Slace,

    Thanks for the comment, I was actually wondering about the [] brackets and was intending to look it up tomorrow, so you've saved me a bit of research, thanks :)

    ReplyDelete
  3. More on the brackets around the menu text here -> http://blog.sitereactor.dk/2009/04/26/google-analytics-for-umbraco-first-installment/

    ReplyDelete
  4. Hi, i am looking to implement a feature allowing users to add sidebar widgets. I saw a post you wrote previously allowing this to be done with a custom data type. Are you going to be speaking about this in the near future? I am very interested in how to implement this. Thanks

    ReplyDelete

Top 5 most popular posts