How to get url of item in content picker field?

Topics: Customizing Orchard, General, Writing themes
Sep 28, 2012 at 8:38 PM
Edited Sep 28, 2012 at 8:43 PM

Hi,

I have created a content type "Product" with a content picker field "Informatie". I use this field in the content items to reference a related page.

I tried to access this related page in a view "Content-Product.Summary.cshtml" (which is used in a projection).

@if (Model.ContentItem.Product.Informatie.ContentItems != null) {
if (Model.ContentItem.Product.Informatie.ContentItems.Length > 0) {
var contentItem = Model.ContentPart.Product.Informatie.ContentItems[0] as Orchard.ContentManagement.ContentItem;
@Url.ItemDisplayUrl(contentItem)
}
}

It's not null and the length > 0, but the cast fails so contentItem is null...

How can I get the url of the content item? Thanks in advance!

Coordinator
Sep 28, 2012 at 9:02 PM

Please use the template which is used by default for this field in ~/Views/Fields/ContentPicker.cshtml

It will give you the whole code to iterate over the content items and display a link to them.

Sep 29, 2012 at 12:59 PM
Edited Sep 29, 2012 at 1:00 PM

Thanks. The code in ContentPicker.cshtml worked so I looked for differences in my code

var contentItem = Model.ContentPart.Product

should be

var contentItem = Model.ContentItem.Product

I was sooo close :) Don't know where ContentPart came from...

Learning about that view, however, popped up a new question. How can I use that default view if I wanted to? The default view of a ContentType with fields shows all the fields (in format 'label: value') but this isn't the case for content picker fields.

Oct 5, 2012 at 8:08 AM

If you don't override templates, that default view would be used automatically.

Oct 5, 2012 at 5:12 PM

That's what I tried.

I renamed the custom view for "Product" so it would no longer match the Content Type.

This resulted in the default view for all fields (in format 'label: value') but nothing comes out for the content picker fields. Hence my question...

Oct 6, 2012 at 5:31 AM

Then I'm confused. When I add a content picker field to a content type and select some content items, they show up in the front-end like this:

Related Pages:Contact Us,About Us

Oct 18, 2012 at 2:49 PM
Edited Oct 18, 2012 at 2:50 PM

Just to add my own experience: also in my site ContentPicker fields don't show up in the front end at all...

Even if I override the field template (Fields.ContentPicker.cshtml) with something like:

 

<p>Hello World!</p>

 

the HTML for the template is not rendered at all. Shape tracer gives me "Fields_ContentPicker [empty]" but it isn't empty!

I'm struggling with this and I can't figure out what's happening...

EDIT: ..and I DON'T hide the part in Placement.info...

Coordinator
Oct 18, 2012 at 4:45 PM

Do you have simple repro steps on a vanilla Orchard install?

Oct 18, 2012 at 6:40 PM
bertrandleroy wrote:

Do you have simple repro steps on a vanilla Orchard install?

Hi, thx for your promptly response!

Now it works, and I can't reproduce it anymore..

At some point I've tried to delete my template alternates, and made some changes to Placement.info in the theme, and the content picker field started to show up again.

Then I moved my cshtmls back in place, and restored my custom Placement.info, and now it works as expected... I think it was only a temporary condition.

But if it happens again I'll post here or in the issue tracker.

 

Paolo