This project is read-only.

Override Fields.ContentPicker.cshtml - get MediaLibraryPickerField from contentItem

Topics: Customizing Orchard
Sep 16, 2013 at 5:25 PM
Edited Sep 16, 2013 at 5:27 PM
I want to make related articles with small images via ContentPicker.
I have a blogpost and i added several other blogposts via ContentPicker.
There is MediaLibraryPickerField in each BlogPost.
I created Fields.ContentPicker.cshtml to override ContentPicker output.
I face a problem of getting MediaLibraryPickerField from related content.
I made it in a following way (but i am not sure if it is right and optimal)
@using Orchard.ContentPicker.Fields
@using Orchard.MediaLibrary.Fields
@using Orchard.Utility.Extensions;
@using System.Linq;
@{ var field = (ContentPickerField)Model.ContentField;
    var contentItems = field.ContentItems;}
<div class="post-meta">
        @if (contentItems.Any())
            foreach (var contentItem in contentItems)
                foreach (var part in contentItem.Parts.Where(p => p.Fields != null && p.Fields.Count() > 0))
                    var mediaField = part.Fields.Where(f => f.Name == "BeforePostImage").FirstOrDefault() as MediaLibraryPickerField;
Is there another way to do it simplier and faster(less conections to database)?
Sep 17, 2013 at 7:00 PM
Yes: contentItem.WhateverTheTypeNameIs.BeforePostImage
Jan 22, 2014 at 5:24 AM
Wondering if you can give me a quick hand with something similar. I'm creating a shape to override Recent Blog Posts. I have added a Media Library Picker Field to my BlogPost is named BlogPostImage.

In my shape, I'm using the following to get the BlogPosts
@using Orchard.ContentManagement;
@using Orchard.Blogs.Models;

    IEnumerable<object> blogPosts = Model.ContentItems.ContentItems;

@if (blogPosts == null || blogPosts.Count() < 1) {
    <p>@T("No posts.")</p>
else {
    <ul class="recent-posts">
        @foreach (dynamic post in blogPosts) {
            string title = post.Title;
            ContentItem contentItem = post.ContentItem;
            DateTime date = post.ContentItem.CommonPart.CreatedUtc;
                    @Html.ItemDisplayLink(title, contentItem)
                    <small>Posted @date.ToShortDateString()</small>
I'd like to get to the MediaUrl property of the BlogPostImage attached to each post. Using Bertrand's example doesn't work with what I have above, so I must not be at the proper ContentItem level to use what he suggested.

Any ideas on how I can get to the MediaUrl? Thanks for the help =)
Jan 22, 2014 at 5:55 AM
I moved the field from the BlogPost type to the BlogPost part...looks like this is where it's supposed to be based on other posts. Still working on getting to it from the blogpost though.
Jan 22, 2014 at 6:06 AM
Edited Jan 22, 2014 at 6:08 AM
You can try this:
@using Orchard.MediaLibrary.Models; ... 

@foreach(var mediaPart in (IEnumerable&lt;MediaPart&gt;) post.BlogPost.BlogPostImage.MediaParts) 
This is for when the field is added to the content type. When added to the BlogPostPart, it should be like:
@foreach(var mediaPart in (IEnumerable&lt;MediaPart&gt;) post.BlogPostPart.BlogPostImage.MediaParts) 
Jan 22, 2014 at 6:12 AM
Got it!
var field = post.ContentItem.BlogPostPart.BlogPostImage;
got me to the field, then
got me the URL.

Always seems that after searching for a couple of hours, then finally posting a question, I find the answer 10 minutes later. =) hahaha
Jan 22, 2014 at 6:32 AM
Now I just need to check that the BlogPostImage field exists before doing this since not all Blog Posts will have an image populated in the field.