This project is read-only.

Menu - Custom Link

Topics: Customizing Orchard
Oct 24, 2015 at 3:09 PM
I added two fields to the Custom Link Content Type. How do I get access to the value of those fields in the menu item template (I copied it into my theme)? I am having trouble understanding the shape that the menu item provides and how to access the fields.


Oct 31, 2015 at 7:01 PM
Anybody? :)
Nov 4, 2015 at 3:40 PM
Edited Nov 4, 2015 at 3:41 PM
The following snippet demonstrates accessing an attached TextField from the MenuItemLink.cshtml shape template:
@using Orchard.Core.Common.Fields
var myTextField = (TextField)Model.Content.ContentItem.MenuItem.MyField;
var myTextFieldValue = myTextField.Value;
The key is to know how to access the content item associated with the menu item. Once you have your hands on a content item, you can access its parts and fields.
In the case of the Custom Link (where its technical content type name is MenuItem), you attached the fields "directly" to the content type (MenuItem). However, Orchard can only attach fields to parts, so what it really did is creating a part called "MenuItem" and attached the fields to that one. That's why you see ContentItem.MenuItem.MyField (MenuItem is the part name you're accessing on the ContentItem).