Rendering fields

Topics: Writing modules
Aug 17, 2012 at 1:45 PM

I create custom ContentPart which will display the field ContentType to which it is attached. I need to render it and would like to control the order of rendering by placment.info.

 

In the CutomPartDriver class, in Display function i write

            var typeDefinition = _contentDefinitionManager.GetTypeDefinition(part.ItemContentType);
            var partDefinition = typeDefinition.Parts.FirstOrDefault(p => p.PartDefinition.Name == containerPart.ItemContentType);

            if (partDefinition == null) { return null; }

            var listShape = shapeHelper.List();
            foreach (var field in partDefinition.PartDefinition.Fields)
            {
                listShape.Add(shapeHelper.Fields_Numeric_My(param1: "param1"));
            }

            return ContentShape("Parts_Search", () => shapeHelper.Parts_Search(
                    List: listShape
                ));

 

And i get rendering all fileds by /Fields/Numeric.My.cshtml file.

How can I create a different layout files for different field types so that I did not have to rewrite the code when adding a new type of field, but only add a new markup file?

Aug 18, 2012 at 12:33 AM

A simple question: how can I create shape by string name

like this:

CreateShape("Fields_Numeric_My")

Coordinator
Aug 18, 2012 at 12:39 AM
Edited Aug 18, 2012 at 12:39 AM

Take a look at Orchard.DisplayManagement.ShapeFactory.

Example:
Display.Create(shapeType, Arguments.Empty())

Where "Display" is your shape factory. From a driver it's shapeHelper, you can find New and Display usages at some locations too.
You can give optional parameters too instead of Arguments.Empty()

Aug 18, 2012 at 1:33 AM

Thanks sebastienros, it works.

For parameters i use: (may be there is a more convenient way)

_shapeFactory.Create("Fields_Numeric_Search", Arguments.From(
                        new List<object>() { field.Name, field.DisplayName, field.Settings }, 
                        new List() { "Name", "DisplayName", "Settings" }))</object>