3

I need an alternate for the EditorTemplate of an Enumerator Field that's used when the Field has a particular name (PublishingMethod).

Based on the docs, I created a view with the pattern [ShapeType__FieldName] in the same folder as the original shape:

Alternate Template

This is not working and still uses the original. I've thought of changing the Editor method in the Driver, but I think that defeats the purpose of alternates, which is that Orchard automatically detects the correct shape as I understand from the docs:

The Orchard framework automatically creates many alternates that you can use in your application. However, you can create templates for these alternate shapes.

Note: I can't use the Shape Tracing module, it never worked even with a clean Orchard install.

Velair
  • 227
  • 3
  • 12
  • Where do you want to use that template? In the admin? Note that you should never change or add files in a module you don't own. – Bertrand Le Roy Jan 25 '18 at 06:17
  • 1
    To debug this you can implement `IShapeTableProvider` and have a look at the shape table and the alternates found. If your alternate is not in there maybe the naming is wrong or there is simply no implementation that adds your alternate automatically (in this case you can add the alternate in the implementation yourself). – ViRuSTriNiTy Jan 25 '18 at 06:49
  • @BertrandLeRoy Yes, in the Admin when editing a content I want to use my own control for EnumeratorFields with this name. The alternate will be in my Theme, right now it's with the original to discard routing or naming errors while getting it to work – Velair Jan 25 '18 at 18:05

1 Answers1

3

The editors in Orchard work different to how Display works. I guess it is so you get a MVC-style experience. Basically, the actual shape returned is of type EditorTemplate, which then binds your model and prefix then renders a partial view with the template name you gave it. What this means is alternates wont work as expected, or as the docs state. The alternates for your field name are actually added to the EditorTemplate shape. So what you can do is add a view called EditorTemplate-PublishingMethod.cshtml with contents like:

@{ 
    var m = (Orchard.Fields.Fields.EnumerationField)Model.Model;
}

@Html.Partial("PublishingMethodEditor", m, new ViewDataDictionary {
    TemplateInfo = new TemplateInfo { HtmlFieldPrefix = Model.Prefix }
})

Then add another view called PublishingMethodEditor.cshtml with the overrides you want for your editor. All these views should go in the root of your Views folder.

Another approach would be to implement the IShapeTableProvider interface and adjust the TemplateName property on a certain condition but, meh, that requires code...

Edit 1 If you have that field name on other content types that you don't want to override you can use the override EditorTemplate-ContentTypeName-PublishingMethod.cshtml

Hazza
  • 6,441
  • 3
  • 24
  • 37
  • 2
    Note that if you want to do things properly, this new view needs to go into an admin theme (which are not trivial to make in Orchard 1.x). Another, maybe saner option may be to build your own field type. You also didn't specify the exact use case. It may very well be that there already is a perfectly adequate field for you to use, such as taxonomy. – Bertrand Le Roy Jan 25 '18 at 18:09
  • 1
    I would guess the likely place you would put this view override would be in your own module, one that maybe provides a part with an attached field that you want to customize the editor for. The requirement I had when I needed this was we had a Speaker taxonomy and the editor for selecting Speakers, the taxonomy field, needed to display the speakers name and thumbnail, not just the default checkbox list. I thought it was dumb then and I think it is dumb now, but hey – Hazza Jan 25 '18 at 19:03