Solved: Projector Module Layout to display Media Picker Field as Image

So I ran into a problem a couple of days ago, and jumped on StackOverFlow hoping I could get a quick and painless answer.  I wanted to use the snazzy new Projector Module to both simplify my life and confer to the site administrator a great sense of managing a site without the need to call for a developer.  The Projector Module was going to solve my problem of displaying a list of “Our People” on an About Us Page.  So, I created a Content Type, OurPeople, complete with a FirstName, LastName, Avatar, Professional and Personal Biography, etc.  Then I created a Query to select all OurPeople Content Types with an Active Field set to true.  My next step involved adding a new layout to display the fields I wanted in the manner that I wanted.  All of this is splendidly accomplished through the Project Module.  Then I hit a little problem:  My Avatar, which is a MediaPickerField, would only display the link and not produce an image.  I used the Rewrite Results option of the Property Field to convert the URL into an image that would display on the page, but discovered the tilde (~/Media/../picture.jpg) from the {Text} token did not get converted to the relative Url path and, as a result, did not find the image.

image

Problem:  Converting the tilde (~)  from MediaPickerField to the necessary relative path?

The solution appears to be figuring out how to get the {Text} token field to display the relative resolved path of the image provided by the MediaPickerField.  Or instead of using the {Text} token using some other token to achieve the result.

image

With that in mind, I went to the code and peeked around until I stumbled across the code for creating the tokens.  A few hours later, I figured out that if I include two lines of code in the ContentToken.cs file (Orchard.Tokens/Providers/ContentToken.cs), I could effectively get the results I was anticipating.

Solution:

I added a line of code that creates a Url Token to be used for a MediaPickerField in the Describe method of the ContentTokens class:

image

Then I added a line of code in the Evaluate method of the ContentTokens class to retrieve the value from the MediaPickerField and convert it using the System.Web.MVC UrlHelper class’ Content method:

image

Now Instead of referencing the {Text} token, I reference the MediaPickerField with the following token:

</pre>
<img class="our-people-avatar" src="{Content.Fields.OurPeople.Avatar.Url}" alt="" />
<pre>

Where “OurPeople” is the ContentType being referenced, “Avatar” is the MediaPickerField being referenced and “Url” is the newly created token that converts the tilde into a relative path!

Notes:

Now by no means do I consider myself being adept enough in understanding all that encompasses the Orchard CMS.  This was simply a problem I ran into and a solution that has so far worked for me.  If any Orchard experts read through this post, and would like to offer any feedback as to whether this would be the recommended way to solve the problem or not, I would love to hear from you.  I have not contributed this to the project because, again, I’m a newbie and don’t care to be ridiculed or ostracized for doing something incorrectly (if that is indeed what the superior intelligence takes levity in).

Advertisements

5 thoughts on “Solved: Projector Module Layout to display Media Picker Field as Image

  1. You can use {Content.Fields.OurPeople.Avatar.Url.Absolute} to get the full path to the image. No need to edit ContentToken.cs.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s