Building a Custom Content Element

Topics: Customizing Orchard
Dec 30, 2014 at 11:02 PM
Edited Jan 1, 2015 at 12:21 AM
I'm a little confused on how to build a custom Content Element. I was trying to use the HTML Element as a basis to work from, but am having some problems. The EditorResult OnBuildEditor function seems fairly straight forward and calls the Elements.Html.cshtml view from the editor templates which makes sense but where is the display function and how does it render the content on the front end? Also the Html.cs file for the element appears to be similar to a part.cs file such as flexslidergrouppart.cs, but it uses State.Get("stuff") and State["stuff"] = value how does this work? I need to be able to get/set an int and an int for the number of items to show and the id of the flexslidergrouppart to show. Many thanks.

Edit: will I need to create a table for my element similar to the way you create a table for content parts in the migrations.cs file?

Edit: How do you pass a view model to the OnDisplaying function? I see how to do it with the OnBuildEditor function but can't find a way to do so with the OnDisplaying function. How do you specify which view should be used for the OnDisplaying function? Once again I can see how to do this with the OnBuildEditor function but it appears to be missing from the onDisplaying function? Thank you.

Edit: I also need a way of grabbing the CSS Class specified in the element's settings and pass that to the display view, any ideas?

Edit: I think I have this partly figured out but for some reason the EnableEditorDialog variable for my element is set to false so it won't render the editor for my element and closes instead.
Jan 1, 2015 at 12:49 AM
Everything is working except for the EnableEditorDialog. If I put a breakpoint and manually change its value to true then everything works as expected. Why isn't the EnableEditorDialog being defaulted to true and how do I fix this problem? I'm also curious if there is a way to change the name and location of the display view. Currently I have to place the display views in the root of the Views folder and name them thusly: Element-Media-GoldSlider.cshtml, Element-Media-GoldSlider.Design.cshtml but I would like to be able to place them in an Elements folder within the Views folder instead if that is possible. I don't care as much about the actual name of the file just the location. Thank you.
Jan 1, 2015 at 5:19 PM
Edited Jan 1, 2015 at 6:36 PM
Got the EnableEditorDialog problem fixed. I was just missing the HasEditor bool in my element.cs file.

I would still like to know if there is a way to move my display views into an Elements folder within the Views folder. Thank you.

Edit: Ran into another problem with testing whether or not the element's Css Class has been set in the settings for the element or not. I was trying to pass the value of the Css Class to the context thusly:

var elementCSSClass = element.State.GetModel<CommonElementSettings>().CssClass;
context.ElementShape.ElementItems = elementCSSClass;

This works fine when the class has been specified but if the class is null and I try to do a test in the view using String.IsNullOrEmpty(Model.ElementItems) or String.IsNullOrWhiteSpace(Model.ElementItems) then I get an error where the parameters don't match the function type. How would I go about testing whether Model.ElementItems has a value or not within my view? I could bandaid it and just hard code a value when the elementCSSClass variable is null, something like noclass.