Accessing a Custom Content Part from the Title Content Part

Topics: Customizing Orchard
Dec 10, 2012 at 4:42 PM


I'm new here. 

For a month more or less I've enjoyed learning and experimenting with Orchard, it has been a great time. The point it seems to be I cannot move forward much more alone ... 

I'm trying to work with Orchard using  in my themes the less number of templates that I can. The reason is I want to avoid as much as I can calls to modify templates for a new content from the designers of my Orchard sites.

In order to do so for example I have added a module that makes Orchard to look for the existence in the theme of a .css file with the same name of the ContentType which is going to be showed.  If that css  exists it is included in the output. In that way they only need to add an extra css if they want to change the look of a concrete Contenttype they have creates. It means no calls to me ;)

Another typical change that they ask often is to change some content types templates in order to make the html output of the their Title in the Summary view uses an href to an external url or an internal media instead of the normal href to the content itself. This kind of behaviour is useful for example to show projections of pdf catalogues with a picture of the frontpage of the pdf catalogue and a link in the title to the pdf. It also is useful for projections that show a list of collaborators with its logo and a external link in the title to the url of the collaborator, etc ...

Indeed an extension to this functionality would be to make that certain Content Part containing an  ImagePicker field also was rendered with an href to the same url I would use for the title.

In order to use a different solution than call me for adding an extra template for the summary of the content type to which they want to change its appearance I have thought that maybe they can add a new Content Part called ExternalContent to those ContentTypes for which they want their Summary view shows a link to a different href url instead of the standard one to the content.

So, at this point what I would need is to add an extra module to Orchard. It on the one hand will add an ExternalContent Part Type to Orchard composed only by a LinkField. And on the other hand it will add the new href behaviour for those Title Parts within a Content Type which also contain an ExternalContent part.

I've made a few attempts following documentation and this post from Piotr Szmyd, but with no good results, I imagine the problem is I'm a newbie.

Can you please give me some feedback? Is it feasible what I want to do? In your opinion is there a better strategy? Any information related with the creation of a Content Part using a LinkField? Any information on how to check if the ExternalContent part is available from the Title part in order to use the default href or the link provided by the ExternalContent part? Should I have to create a new Title part to avoid problems with updates? 

Thank you in advance


Dec 11, 2012 at 5:44 AM
Edited Dec 11, 2012 at 5:44 AM

Perhaps you could do something along the following lines:

Since you will be creating a new ContentType whose Title you want to be linkable to a custom url, you could create a new version of the TitlePart, maybe call it something like ExternalLinkTitlePart.

This ExternalLinkTitlePart will implement ITitleAspect, so it will be treated by the system as expected from TitlePart. It will also have one or two extra properties:

  1. ExternalUrl : string
  2. Target: string
  3. Anything else that's useful for your requirements

The driver of the ExternalLinkTitlePart will emit a shape whose template will render the clickable title.

Should be easy to do.

Dec 12, 2012 at 3:49 PM

Thank you sfmskywalker, it is a very straight forward solution.

So for the  extension of the functionality I mentioned: "To have also an ImagePicker field that is rendered with an href to the same url pointed by the title." wich will be best solution in your opinion. The point is I would like to avoid to force the user to insert the url  two times, one for the title and one for the ImagePicker.

Regards :)

Dec 15, 2012 at 5:39 PM

You're welcome.

I would start by defining the ExternalContentPart, attach the LinkField and MediaPickerField, and create a template for the ExternalContentPart shape (you will have to implement a driver for ExternalContentPart to emit a shape).

Within this template, you will be able to access the two content fields, e.g.:

   var externalContentPart = Model.ContentPart;
   var linkField = externalContentPart.MyLinkField;
   var mediaPickerField = externalContentPart.MyMediaPickerField;

<a href="@linkField.Href"><img src="@mediaPickerField.Url" /></a>

And regarding the TitlePart, you could override the Parts.Title.cshtml template (possibly using an alternate) and access the ExternalContentPart from there, e.g.:

   var titlePart = Model.ContentPart;
   var contentItem = titlePart.ContentItem;
   var externalContentPart = contentItem.ExternalContentPart;

@if(externalContentPart != null ){
   <a href="@externalContentPart.MyLinkField.Href">@titlePart.Title</a>
else {



Dec 20, 2012 at 7:33 AM
Edited Dec 20, 2012 at 10:01 AM

That's great!

I didn't know I can access from the TitlePart Template to the externalContentPart.

Thank you!!