Title Content Part that is a hyperlink to a dynamic link

Topics: Customizing Orchard
Sep 17, 2015 at 5:12 AM
Hi, I am new to orchard and trying to figure out the best way to make a Title Part link out to an external URL.

I have a custom Content Type named ExternalNews. It has the title part, body part, and a custom field called ExternalUrl (LinkField)

End goal - I would like to display something like this:

<Title> (Links to <External Url>)
<Body>

Now I can make an alternative to the Title Content type, but I don't think I have access to ExternalUrl to set the link. I wasn't sure if there was an easy way to do this or if I render my projection with a custom shape?

Thoughts, Tips, Hints would be great.
Sep 17, 2015 at 7:58 AM
Where do you have access to the external url? Seems to me that you should override the TitlePart for your content type
Sep 17, 2015 at 2:25 PM
Thank you @sanderg for the quick reply.

Here is a little more info:

Content Type : ExternalNews

Fields:
  • ExternalUrl (LinkField)
Content Parts:
  • Title Part
  • Body Part
If I look at the Template for title Parts_Title__Summary:
@{
    Orchard.ContentManagement.ContentItem contentItem = Model.ContentPart.ContentItem;
    string title = Model.Title.ToString();
}

<h1>@Html.ItemDisplayLink(title, contentItem)</h1>
I guess how would I know / get the ExternalUrl field so I can do something like this:
@{
    Orchard.ContentManagement.ContentItem contentItem = Model.ContentPart.ContentItem;
    string title = Model.Title.ToString();
}

<h1><a href="<ExternalUrl>"@Html.ItemDisplayLink(title, contentItem)</a></h1>
Sep 21, 2015 at 7:59 AM
Edited Sep 21, 2015 at 8:02 AM
In your override of Parts_Title__Summary you can do something like the following:
@{
    Orchard.ContentManagement.ContentItem contentItem = Model.ContentPart.ContentItem;
    string title = Model.Title.ToString();

    // ContentPart is the TitlePart
    // ContentItem is the content item where the parts are attached to (an instance of External News content type)
    // ExternalNews is the backing part of your content type "ExternalNews"
    // ExternalUrl is the name of your field
    // Value is the value of that field
    var externalUrl = Model.ContentPart.ContentItem.ExternalNews.ExternalUrl.Value;
}

<h1>@Html.ItemDisplayLink(title, contentItem)</h1>

<h3><a href="@externalUrl" target="_blank">@T("External url")</a></h1>