Link Field Does Not Display If "Description" AKA Anchor Text Has No Value

But Sometimes You Want To Display a Link With No Visible Text

Typically it's best practice for all links on a webpage to include anchor text to give users and search engines context about what a link is for. However, for some fields, you may not want to actually display the anchor text. One example is if you have a link to Twitter and the link appears as a Twitter icon without any visible text.

This case can be problematic for Sitecore because if one checks to see if the field has a value, Sitecore will return no value if the "Description" subfield of a General Link field has no value; even if the URL does have a value.

Here's some boilerplate code with a few tips and tricks to get you on your way.

The Model

// Constructor 
public ModelName() 
	TwitterLink = new HtmlString(FieldRenderer.Render(_item, "Twitter"));
	LinkField li = _item.Fields["Twitter"];

	if (li != null)
		TwitterLinkRaw = li.GetFriendlyUrl(); 
		// Use authored anchortext value or pull the default anchortext value for this field from the dictionary to allow for support of multiple languages
		TwitterLinkDescRaw = !String.IsNullOrWhiteSpace(li.Text) ? li.Text : @Translate.Text("Twitter"); 

The View

@if (ViewHelper.IsEditMode())
	@* Render the field directly in edit mode so that the user can visually modify the links *@
	@* Preview or Normal modes *@
	@* Ensure the link field has a URL which will be used to determine whether or not the link should be displayed *@
	@* Use the authored link anchortext or a default value if it is not set *@
	if (!String.IsNullOrWhiteSpace(Model.TwitterLinkRaw))

All the very best,


Hey, Developers!

We're on the look out for talented developers to join our team.

Think you have what it takes?