Create a Phone Link with the SXA Link Component – How To Implement Series

Lately I have been seeing people having trouble to use a Link component for a Telephone Number. They have almost achieved it but there was the problem that when you put the external link it appends it with http:// and the link turn into like this http://tel:0800123456

Shortest Solution

The shortest solution is that before save the link you have just inserted with the external link, just manually remove the http:// and then save it. It is not so smart but as for the Zero Code Approch you would not want to modify the code, because that’s what SXA is for, Right?

Solution with Sitcore SXA Rendering Variant

You can use the SXA’s default Rendering Variant with some tricks. For that you just need create and modify a Rendering Variant for the Link Component and that’s it.

Before you think of modifying the Link component, think about extending it. You can create a simple template and inherit it into the Link Component. Take the field as Number because if you use Text you will be back to where you started. I will tell you why I used Number as we move further.

and then inherit it into the Link Component. Remember the C# SOLID Principles where instead of modifying something it is preferred to extend it. This way the logic the previous developers has written will not get an impact.

So now we have out Template ready, we can put our phone number in our newly created Template field, which is PhoneLink

Here comes the main part, what you have to do is to create a new Rendering Variant for the Link Component. Add a new Field for the PhoneLink , scroll to  Link attributes fields and set the Link attributes fields as href as key and tel:($PhoneLink) as its value. We are actually passing the token for the ($PhoneLink) in the href of the Link Component. Don’t forget to mark this Field as isLink or your Link wont be rendered.

Now change the Link Component Rendering variant to use the PhoneLink Rendering Variant from the rendering’s Control properties popup.

You can also change it from Experience Editor, where you click on the Component and from its properties change the Rendering variant via a dropdown.

And that’s it, your Link Component will render the Phone Link based on the Rendering Variant you have selected, And your Link options that were previously available won’t be available because of the PhoneLink Rendering Variant. You can also use the Prefix from you Field values as a constant. (You can play around to make the whole link clickable)

Cloning a Rendering

You can also clone the Link Component and instead of inheriting the PhoneLink template into the Link Component, you could change the fields of the newly cloned Phone Rendering Component from Link to Number and the rest of the steps still the same

Code Solution

If you are brave enough to spend time you can follow this code solution provided by Akshay Sura where you have to write C# classes, some XML, processor, etc. This solution will also work for the non-SXA Sites as well.

Or you can also handle this with the Glass extension (if you are using Glass mapper), you have to extend you solution to check for http://tel and then remove the http:// from the link you provided. See the solution provided by Adam Seabridge at Sitecore Stackexchange

Feel free, to use it and share your feedback if you find it useful. Cheers!

Reference Links

Sitecore Experience Platform 9.1 update 1
Sitecore SXA 1.8.1

Don’t forget to check my previous article for Sitecore SXA’s most wanted features requested by Clients, with implementation




3 thoughts on “Create a Phone Link with the SXA Link Component – How To Implement Series

Share what's in your Mind :)

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.