3
votes

I would like to create a custom Package content type in Orchard 1.6. I already have a content part that represents the whole db record and UI for a Package, but now I'm wondering if I am going about this correctly.

It seems to me the next step is to use Orchard dashboard to create a new content type, and add my custom content part to the type. But, then the content type is internal to Orchard, with a dependency on my 'external' module that hosts the content part. How can I make it so that my content type is only available when my module is enabled?

1

1 Answers

5
votes

For convenience, you could create the content type as part of one of the migrations in your module. This would only run when enabled. It would look something like this...

   //Inside of your migration file... 
   public int UpdateFrom1(){
     ContentDefinitionManager.AlterTypeDefinition("Package", cfg=> cfg
        .Creatable()
        .WithPart("YourCustomPart")
        .WithPart("CommonPart")
        .WithPart("Whatever other parts you want..."));
        return 2;
   }

Removing this content type when you disable your module would be the tricky part because it might be kind of unexpected by the user. Maybe "Package" is a type they still want to use with different parts attached. Also, if they manually delete your module without disabling, you can't really write code to respond to that event. The only reliable thing I know of is the IFeatureEventHandler. This would allow you to remove your content type if they disable the module in the admin...

public PackageRemover : IFeatureEventHandler {
  private readonly IContentDefinitionManager _contentDefinitionManager;
  public PackageRemover(IContentDefinitionManager contentDefinitionManager){
    _contentDefinitionManager = contentDefinitionManager;
  }

  public void Installed(Feature feature){}
  public void Enabling(Feature feature){}
  public void Enabled(Feature feature){}
  public void Disabling(Feature feature){
    _contentDefinitionManager.DeleteTypeDefinition("Package");
  }
  public void Disabled(Feature feature){}
  public void Uninstalling(Feature feature){}
  public void Uninstalled(Feature feature){}
}