Custom Content Part - Reusing existing fields

Topics: Customizing Orchard
Jan 6, 2013 at 7:34 PM

I'm designing a custom content part and one of the attributes of the part will be to choose an image similar to how the image field works in Orchard today. My question is, when designing my custom content part, how can I have it use the Media Picker field already built into Orchard as one of it's properties?

Jun 4, 2013 at 6:12 PM
Edited Jun 4, 2013 at 6:13 PM
have the same question!

as far as I known, we can add a media picker field into our custom content type by following code:
ContentDefinitionManager.AlterPartDefinition("Product",
    builder => builder.WithField("ProductImage",
        fieldBuilder => fieldBuilder
            .OfType("MediaPickerField")
            .WithDisplayName("Product Image")));
more info: http://stackoverflow.com/questions/10369967/orchard-cms-how-to-add-media-picker-field-to-anew-module

But I have a problem how to display the media picker field (Upload image control) in my Editor??? any help will be appreciated
Coordinator
Jun 4, 2013 at 7:03 PM
The problem is that this is a misconception about what fields are for. Fields are added to a part (usually by the administrator of the site), but they are independent entities. It's the field's driver that will take care of everything, but your part should never assume that a certain field is going to be there. If something is essential to a part, then it should not be a field, but a part property.
Jun 6, 2013 at 4:47 PM
Hi BertrandLeRoy,

Thanks for your quick response. But I still have a little confuse.

I have a custom content part called "DestinationPart":
    public class DestinationPart : ContentPart<DestinationPartRecord>
    {
        public string Name
        {
            get { return Record.Name; }
            set { Record.Name = value; }
        }

        public decimal? Longitude
        {
            get { return Record.Longitude; }
            set { Record.Longitude = value; }
        }

        public decimal? Latitude
        {
            get { return Record.Latitude; }
            set { Record.Latitude = value; }
        }
    }
Now I want to add a media picker field into this content part to have an image in each destination item.
ContentDefinitionManager.AlterPartDefinition("Product",
    builder => builder.WithField("ProductImage",
        fieldBuilder => fieldBuilder
            .OfType("MediaPickerField")
            .WithDisplayName("Product Image")));
The driver will take care how to display the content part, but the Editor method just return the shape only include Destination model, how to add a MediaPicker into the result???
        protected override DriverResult Editor(DestinationPart part, dynamic shapeHelper) {
            return ContentShape("Parts_Destination_Edit", () => shapeHelper
                .EditorTemplate(TemplateName: "Parts/Destination", Model: part, Prefix: Prefix));
        }
If this is wrong way to implement, could you please tell me how to resolve this problem? any help will be appreciated.,
Coordinator
Jun 8, 2013 at 12:08 AM
The media picker field's driver should be taking care of displaying its own editor. It is not the job of your part driver. If it doesn't maybe you have a placement issue.