Search code examples
orchardcms

Add custom action to Orchard CMS Media Library


I see that the Media Library has the ability to attach shapes to the navigation and actions. However I am trying to work out how to add an action shape to the actions so that I can have a new button added to the Media Library to invoke an action in my module.

From the Media Library AdminController.cs:

// let other modules enhance the ui by providing custom navigation and actions
var explorer = Services.ContentManager.New("MediaLibraryExplorer");
explorer.Weld(new MediaLibraryExplorerPart());

var explorerShape = Services.ContentManager.BuildDisplay(explorer);

var viewModel = new MediaManagerIndexViewModel {
    CustomActionsShapes = explorerShape.Actions, // <-- I need to have my shape that is rendered as a button in here
};

Still bit too green with Orchard, but feels like a hole in my understanding of using shapes and placing them. I don't think a part needs to be involved for a shape that renders button as there is nothing to be stored?


Solution

  • I have managed to work this out. First return a new shape for the MediaLibraryExplorerPart in a MediaLibraryExplorerPartDriver

    public class MediaLibraryExplorerPartDriver : ContentPartDriver<MediaLibraryExplorerPart> {
        protected override DriverResult Display(MediaLibraryExplorerPart part, string displayType, dynamic shapeHelper) {
            return ContentShape("Parts_MediaLibraryExplorer_MyActionShape", () => shapeHelper.Parts_MediaLibraryExplorer_MyActionShape());
        }
    }
    

    Then place in the Actions zone

    <Placement>
      <Place Parts_MediaLibraryExplorer_MyActionShape="Actions:6" />
    </Placement>
    

    Then create template (Views/Parts/MediaLibraryExplorer.MyActionShape.cshtml) for the shape with the button to invoke the module controller action :)

    @Html.ActionLink(T("Click Me").Text, "Index", "Admin", new { area = "Orchard.MyModule" }, new { id = "click-me-link", @class = "button" })
    

    How easy was that?!