teran
teran

Reputation: 3234

Orchard CMS: add image to custom Item

Hi once again :) My next question about Orchard CMS looks trivial but..

I have my custom NewsModule with NewsItem type which consists of TitlePart, BodyPrt, and my custom NewsPart. NewsPart is based on NewsPartRecord.

Now I have to extend my NewsItem document type with Image. So far I found that there are ImagePart and MediaLibraryPickerField. ImagePart stores width and heihgt of image, so I think it is something for internal use.

The next one is MediaLibraryPickerField. After googling for a while, I found that I can extend my NewsPart using WithField() method. But! First question here: when I'm configuring Page type with Admin UI, I can add field to the type. But using Migrations I can't add field to NewsItem, only to NewsPart.

The second question here. MediaLibraryPicker allows me to select any type of file from library, image, text, zip or any other. And this is not what I actually need.

So, what are the best practices in Orchard to extend Items with icons?

thanks.

Upvotes: 0

Views: 593

Answers (1)

Xeevis
Xeevis

Reputation: 4521

You can add fields to NewsItem, it just requires a different type of definition. It confused me too as it's not as intuitive as you'd like it to be.

To add parts you are using AlterTypeDefinition.

this.ContentDefinitionManager.AlterTypeDefinition(
    "NewsItem", 
    cfg => cfg.WithPart("NewsPart"));

But to add fields you have to also use AlterPartDefinition and specify the NewsItem not the NewsPart.

this.ContentDefinitionManager.AlterPartDefinition(
    "NewsItem", 
    builder =>
        builder.WithField(
            "NewsImages", 
             fieldBuilder =>
                 fieldBuilder.OfType("MediaLibraryPickerField")
                 .WithDisplayName("News Images")
                 // Allow multiple images
                 .WithSetting("MediaLibraryPickerFieldSettings.Multiple", "true"))

Considering second question, MediaLibraryPickerField has option to display only selected types/parts.

.WithSetting(MediaLibraryPickerFieldSettings.DisplayedContentTypes="Document,Video")

If you need new media type you have to create new ContentType with Stereotype "Media", it also needs "MediaPart" and part that defines metadata for that type. You can look at how Document or Video is defined for sample code.

Upvotes: 2

Related Questions