Search code examples
c#orchardcms

Embedding a field in an alternate view


I'm building an alternate view for a custom type that will display a table of fields. Most fields can be displayed using something like this:

@Display(Model.ContentItem.CustomType.CustomField.Value)

However, a few of the fields I would prefer to have the full field view. For example, an IP address field that displays the result of pinging the address alongside the field value itself.

I found that this shape can be displayed using:

@Display.Fields_CustomField(Model.ContentItem.CustomType.Address)

but then that call results in the Model values being different than when displayed through the driver, such as needing to call Model.ContentField.Value instead of Model.Value.

var model = Model.ContentField ?? Model;
var settings = Model.Settings ?? Model.PartFieldDefinition.Settings;

and then even more complex checks:

bool customFieldSetting = settings.GetType() == typeof(CustomFieldSettings) ? 
    settings.CustomSetting : 
    bool.Parse(settings["CustomFieldSettings.CustomSetting"]);

My custom field can be rewritten to check multiple locations for these values, but that's an inconvenient solution when having to alter many fields with this logic.

Is there a way to render the full display of a field from a shape alternate?


Solution

  • I guess you are displaying a table of content items then? I'd say the easiest way to do this is to send all the fields to their own zones. So in your type alternate have something like this:

      <tr>
        <td>@Display(Model.Column1)</td>
        <td>@Display(Model.Column2)</td>
      </tr>
    

    And have a placement like this:

    <Place Fields_Text-FirstName="Column1:1"
        Fields_Text-LastName="Column2:1" />
    

    Another option you could look into is overtaking the rendering of the zone with your own custom implementation and adding your table column markup between each shape rendered