Insights

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 *@
	
}
else
{
	@* 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,

Marcel

👋 Hey Sitecore Enthusiasts!

Sign up to our bi-weekly newsletter for a bite-sized curation of valuable insight from the Sitecore community.

What’s in it for you?

  • Stay up-to-date with the latest Sitecore news
  • New to Sitecore? Learn tips and tricks to help you navigate this powerful tool
  • Sitecore pro? Expand your skill set and discover troubleshooting tips
  • Browse open careers and opportunities
  • Get a chance to be featured in upcoming editions
  • Learn our secret handshake
  • And more!
Sitecore Snack a newsletter by Fishtank Consulting