orchardcmsorchardcms-1.9

How to use placement or template overrides to hide part of a Part in a Content Editor?


I'm using Orchard 1.9.3 and have set up a couple of custom ContentTypes that mimic the standard Page type with an Autoroute and Layout Part etc.

These types of pages should never be set as the homepage so I want to hide just the Set as home page field of the Autoroute part but only for my custom types. I'm not sure what the most efficient way is to go about this. Can I target this field specifically in a placement file?


Solution

  • You can override the Parts.Autoroute.Edit.cshtml and include some custom logic:

    @{
        var canSetAsHomePage = true;
        var myTypesToDisableHomePageFor = ["MyCustomContentType", "AnotherCustomContentType"];
        if (myTypesToDisableHomePageFor.Contains(Model.ContentType)) {
            canSetAsHomePage = false;
        }
    }
    
    // ..
    
    @if (!Model.IsHomePage && canSetAsHomePage) {
        if (AuthorizedFor(Permissions.SetHomePage)) {
    // ..
    

    For this to work you also have to add an extra property to Orchard.Autoroute.ViewModels.AutoroutePartEditViewModel:

    public class AutoroutePartEditViewModel {
        ...
        public string ContentType { get; set; }
    }
    

    and make sure to set it in the Editor method of Orchard.Autoroute.Drivers.AutoroutePartDriver:

    var viewModel = new AutoroutePartEditViewModel {
        CurrentUrl = part.DisplayAlias,
        Settings = settings,
        ContentType = part.ContentItem.ContentType
    };