When defining content parts programmatically, is it necessary to use WithField()?

Topics: Writing modules
Jul 3, 2012 at 11:04 PM
Edited Jul 3, 2012 at 11:05 PM

When should I use WithField() and when should I use a driver with an Editor view? Are these equivalent?

Developer
Jul 3, 2012 at 11:48 PM

You use WithField if you want to attach a content field to a content part.
A driver does not have to implement the Editor method; this is only required if you want the user to be able to edit some settings / properties on your part.
WithField and Editor are completely different things (well, they are both methods of course, but that's where the similarity ends).

Jul 4, 2012 at 12:16 AM

You must be the author of skywalkersoftwaredevelopment.net. I've been reading your blog about Orchard module development. Thanks for publishing it. So... If a part has a content field, its model does not need a property to store the value generated by the field. Is that correct?

Coordinator
Jul 4, 2012 at 6:43 AM

That's correct, but having properties on the part has a number of advantages in terms of flexibility. Depends on your scenario. What's your scenario?

Jul 5, 2012 at 5:03 PM

I'm creating a module that defines a very simple type with a very simple part. The ultimate goal is a larger set of types that I can interact with using plain old MVC controllers that will define a JSON API. So far it only seems to save if I create the part using code but create the type using the Orchard admin. However, I don't want to create a database dependency by creating the types using the Orchard admin. There must be a problem with the way I'm defining the type. Is CommonPart required for all types?

Jul 5, 2012 at 6:54 PM
Edited Jul 5, 2012 at 7:04 PM

I think not having a CommonPart / assigning an owner must have been the problem!

Oct 23, 2012 at 7:23 AM

I have a similar question, hence posting on this thread...might sound basic but I'm new here...

Here is my scenario..

I'm creating a Portfolio Content Type (user will be able to create portfolios) which will have several content parts (all available in gallery) and needs to have several content fields (Image, Textbox , taxonomy field etc). I have seen at several places that people creating a ContentType also create a ContentPart with same name as ContentType even though it is blank with no content fields. here are my questions..

1) When should i consider creating a Content Part and adding a content field to it vs. directly adding content fields to a Content Type?

2) If I choose later option in point 1 ( i.e directly add content fields to content type), is there any benefit in creating an empty Content Part with Portfolio as name and attach to Portfolio content type?

3) So far, I have seen that we can add content fields to a part and content part to a content type in Migrations.cs. Is there any way to add Content Field to a Type directly in Migrations.cs?

Thanks

Coordinator
Oct 23, 2012 at 8:31 AM

1. Almost never. Only when you want to attach those fields to all contents using that part.

2. Yes, it will only affect that content type, which is what you want most of the time.

3. Yes, use WithField through a part with the same name as the type.

Oct 23, 2012 at 7:33 PM

Thanks Bertrand for your response... it clarifies several things for me...

another followup question... in my migration.cs, code I'm writing is resulting in creation of PortfolioPart and when I'm adding fields, they are getting added to Part eather than the Portfolio Content type.

here is what I'm writing...

 ContentDefinitionManager.AlterTypeDefinition("Portfolio", builder =>
                    builder.WithPart("CommonPart")
                            .WithPart("TitlePart")
                            .WithPart("....")
                            .WithPart("...")
                            .Creatable());


//.... more settings for above parts...

SchemaBuilder.CreateTable("PortfolioPartRecord", table =>
                                                          table.ContentPartRecord()
                                                          .Column<string>("PortfolioId")
                                                          .Column<string>("PortfolioOwner")

ContentDefinitionManager.AlterTypeDefinition("Portfolio", builder =>
                                                                      builder.WithPart("PortfolioPart")); 

 ContentDefinitionManager.AlterPartDefinition("PortfolioPart", builder =>
                builder.WithField("Summary", definitionBuilder => definitionBuilder
                    .OfType("TextField")
                    .WithDisplayName("Summary")
                    .WithSetting("TextFieldSettings.Flavor", "text")
                    .WithSetting("TextFieldSettings.Required", "true")));

Based on your answer earlier, I think I should see the fields added to the Content Type rather than the Content Part but that's not the case.

Am I missing something?

 ContentDefinitionManager.AlterTypeDefinition("Portfolio", builder =>
                    builder.WithPart("CommonPart")
                            .WithPart("TitlePart")
                            .WithPart("AutoroutePart")
                            .WithPart("BodyPart")
                            .WithPart("CommentsPart")
                            .WithPart("TagsPart")
                            .WithPart("UserViewPart")
                            .Creatable());
Oct 25, 2012 at 2:30 AM

Please help with a response!!

Thanks

Coordinator
Oct 25, 2012 at 7:07 AM

What makes you say it's not the case? What are you expecting and what's happening?

Oct 25, 2012 at 8:37 AM

When I go into Content Part, I see fields listed nested in Content Part portfolio Type and not on top section of Content Type (fields section). With this behavior, I'm thinking that fields are getting added to the part and not the type.

This means that if I use the ContentPart with some other ContentType, I will have to use Content Fields which i intended to use only for my current Content Type. My intention is to add the fields to ContentType only and not to the part.

Hoping I made it clear...can provide screenshot if needed.

Developer
Oct 25, 2012 at 9:32 AM

In that case, you should create another part that has the exact same name as the content type, "Portfolio" (without the "Part" suffix). If you add content fields to this part, it will appear as if the content fields are attached to the 'Portfolio" content type.

In the migration code you listed above, you are defining a content part named "PortfolioPart", while this should have been "Portfolio" as Bertrand mentions.

Developer
Oct 25, 2012 at 9:35 AM

You may also want to define your content part before defining a content type that uses this part (although I haven't tried defining a type before a part that is used by the type, but I can see that Orchard wouldn't be able to find the part if it's not yet defined).