This project is read-only.

Field Access From view

Topics: Troubleshooting, Writing modules, Writing themes
Aug 29, 2012 at 9:53 PM

Out of the Box install; added new field to the "Page" content type used to select an image (called "Header Image") and it uses the built-in MediaPickerField.

While in the Layout.cshtml - I can see that it renders that image fine after calling @Display(Model.Content) - I would like to manually render that field elsewhere in this same .cshtml; how can I access that field/value?

I would much rathe use the Content Item Picker as my field; and then render that actual content that was selected; is there a way to-do that?

If the above is not recommended; then my only other option would be to write a widget that essentially has one field (Image) - that I would like it to use the MediaPickerField; I have tried several times and looked at quite a bit of internet stuff and nothing out there seems to use that Field; so I can't seem to-do that either? (I would want my widget to allow the user to select an Image) - then I'd place that widget on a zone; and then I wouldn't have to manually render it as above.

Any ideas would be greatly appreciated!




Aug 30, 2012 at 2:05 PM

You could override the Content.cshtml file (or rather create a template for one of its alternates). Alternatively, you could use to specify the zone and location within that zone for the MediaPickerField or even ContentPickerField. If you choose to create a Content template (e.g. Content-Page.cshtml), the model for that template will be the content item, from which you can access all of the attached parts and fields. If you attached a field directly to the content type using the admin, that field will be attached to an implicitly created content part which has the same name as the content type ("Page"). That allows you to access the field like so: Model.ContentItem.Page.HeaderImage.Url in the case of a media picker field. Check out the docs for more details.

Aug 30, 2012 at 2:44 PM
Edited Aug 30, 2012 at 2:49 PM

Very helpful - thanks!

Can you use different Zones within the - wow for some reason I thought that would only render based on where it was placed (via the Admin UI).  Which means if I place a Widget in the Header Zone but use Content in the placement.Info - it will NOT be rendered within the header but instead within the content/body?  Currently I use "Content:#" for any widget I've made and I typically place them within the "Header" zone - and it still renders correctly within my "Header".  So I don't competely understand what "Content" means versus using some other team like "Header or Body".  I'll read up on that.

Any ideas on how to render a content item that was selected via the Content Item Picker? Based on what you've said - I can access the values of the items that were selected by the Content Item Picker (when used as a field on a Content Item or field etc) - but I don't know how to "New.()" or "Display()" it.



Aug 30, 2012 at 2:56 PM

I think you would need to use ContentManager.BuildDisplay(), which takes a content item as an argument and returns a shape to be rendered. Although you could do this from the view, it would be recommended to do so from a driver or shape table provider.

Aug 30, 2012 at 7:43 PM

This is where understanding more about Orchard would be much more helpful to me. What is the Content Item Picker used for if it's not going to render the actual item?  I assume that if I don't do this in a View "cshtml" file - then I'd be forced to build a widget that does this.  I haven't seen build a widget that does something like this - rendering shapes based off of existing content items that have been seleted - so I'd have to look around to see if there's anything in-depth about it - otherwise - I'd end up still doing it in a "cshtml" file.

Essentially - I'd like to build a bunch of content items (that are not pages - just content items that have some field on it like an image field etc) and then add a new field to something like the Page content type that is a content picker - then a user could just select the content - save & publish and then it would render.

Right now - the item itself is not rendered; instead it's just the information about that item (e.g. path & name) - which generates an anchor tag (by default).  So I was going to create an alternate and modify the cshtml to render the actual item instead of creating a link for that item.  Make Sense? - is there a way to already do this and I'm over thinking it?

thanks again.


Aug 30, 2012 at 9:21 PM

I imagine the purpose of ContentPicker is to manually relate content. Nothing more. How you use that relation, of course, is entirely up to you. I imagine the most common use case would be to render a list of related content. The nice thing about Orchard is that if something isn't already there, it provides excellent infrastructure to build it.

Now, I don't think it requires you to build a widget. Instead, you could write a little driver or even just a shape table provider that inspects the attached fields of the content item, and for each content item picker field, load the selected content items, and render them. Add the resulting shapes to the shape currently being rendered, and you're ready to rock. It sounds like this could be a generic shape provider, so perhaps do a quick search on the gallery first.