Related Links!

If you need to combine internal links and external links in the same Umbraco picker, you will need to use the related links picker.

This will generate a JSON array. This is how you extract the links on the other end.

 

var obj = JsonConvert.DeserializeObject<JArray>(test);

foreach (var a in obj)
{
var type = a.Value<string>(“type”);

var newWindow = a.Value<bool>(“newWindow”);
var title = a.Value<string>(“title”);

if (type.IsNullOrWhiteSpace() == false)
{
if (type == “internal”)
{
var linkId = a.Value<int>(“internal”);
var link = Umbraco.NiceUrl(linkId);
<li><a href=”@link” @(newWindow ? “target=_blank” : “”)>@title</a></li>
}

if(type == “external”)
{
var linkTitle = a.Value<string>(“caption”);
var link = a.Value<string>(“link”);
<li><a href=”@link” @(newWindow ? “target=_blank” : “”)>@title</a></li>
}
}
}

or

public static MvcHtmlString RelatedLinksGenerator(this JToken source, string urlCSS)
{
var helper = new UmbracoHelper(UmbracoContext.Current);
var type = source.Value(“type”);
var newWindow = source.Value(“newWindow”);
var title = source.Value(“title”);

var anchor = new TagBuilder(“a”);
string link = string.Empty;

if(type.Equals(“internal”))
{
var linkId = source.Value(“internal”);
link = helper.NiceUrlWithDomain(linkId);
}

if(type.Equals(“external”))
{
var linkTitle = source.Value(“caption”);
link = source.Value(“link”);
}

anchor.MergeAttribute(“href”, link);
anchor.MergeAttribute(“class”, urlCSS);
anchor.SetInnerText(title);

return MvcHtmlString.Create(anchor.ToString(TagRenderMode.Normal));
}

Advertisements

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