Creating a module that uses Orchard.MediaPicker

Topics: Writing modules
Apr 25, 2012 at 12:55 AM

OK...really green with Orchard and attempting to create a module that will allow the end user to pick media via the mediapicker.  I'm just not sure how I would wire that up and looking for some guidance or documentation that would explain how that is done.

I basically have my module working 100% in terms of the models/views/handlers/driver/etc. but when I edit my "part" on the backend, instead of a plain textbox I'd like to have the media picker functionality.

Apr 25, 2012 at 1:06 AM

Look at existing code that does it, such as the media picker field.

Apr 25, 2012 at 2:54 AM

OK...took a closer look and I think what I am after is in the MediaPickerFieldDriver.Display and MediaPickerFieldDriver.Editor methods.  The next question is do I have to recreate the views that are being returned from that driver/module or can I somehow instruct my module to re-use the existing views from that module??

In my module I have created a driver that currently does pretty much the same thing...has a Display and Editor method that returns a DriverResult using views from my module. i.e. - Parts_ContentName" and "Parts_ContentName_Edit"

Would it be as simple as using the full name of the module including namespace or something? i.e. - "Orchard_Fields_Fields_MediaPicker" and "Orchard_Fields_Fields_MediaPicker_Edit"

Apr 25, 2012 at 3:20 AM

I think duplicating is more than ok: it enables selective overriding. But yes, if you're sure the shape should be exactly the same, just re-use the same shape name. One caveat would be that it would only work if the module where the template is defined is enabled.

Apr 25, 2012 at 7:40 PM

OK...I figured it out.  I was just too far ahead of myself...I was thinking I'd need to create the models, handlers and drivers for my content type when all I really needed/wanted was to create a content part and then create a new content type using that part.  I basically just did this:

ContentDefinitionManager.AlterPartDefinition("MyCustomPart", builder => builder
                .WithField("MyCustomFieldName", fieldBuilder => fieldBuilder.OfType("MediaPickerField"))

//Create the content type...
ContentDefinitionManager.AlterTypeDefinition("MyCustomContentName", cfg => cfg

I was just making it way more complicated than it needed to I just need to add Orchard.Fields to my list of dependancies in the module.txt and I'm good to go I think...