I am working on Sitecore version 9.1. When attempting to insert a link using the general link field, the user is prompted to enter the following information:
In an effort to enforce accessibility on the site, we would like the content authors to be prompted with a validation error message if they attempt to create a link without supplying a title attribute. This title text is supplied through the Alternate Text
field in the image above. Based on what I've seen from posts like the following:
https://www.akshaysura.com/2015/02/22/add-telephone-link-to-the-general-link-field-type-in-sitecore/
I have created a link text validator that works just fine if I apply it to an individual link field:
using Sitecore.Data.Validators;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;
using Sitecore.Data.Fields;
using Sitecore.Shell.Applications.ContentEditor;
namespace MyProject
{
public class LinkAltTextValidator : StandardValidator
{
public override string Name
{
get { return "Link alt text validator"; }
}
public LinkAltTextValidator() {}
public LinkAltTextValidator(SerializationInfo info, StreamingContext context) : base(info, context) { }
protected override ValidatorResult Evaluate()
{
Field field = this.GetField();
if (field == null)
return ValidatorResult.Valid;
string str1 = this.ControlValidationValue;
if (string.IsNullOrEmpty(str1) || string.Compare(str1, "<link>", StringComparison.InvariantCulture) == 0)
return ValidatorResult.Valid;
XmlValue xmlValue = new XmlValue(str1, "link");
string attribute = xmlValue.GetAttribute("title");
if (!string.IsNullOrEmpty(xmlValue.GetAttribute("title")))
return ValidatorResult.Valid;
this.Text = this.GetText("Alternate text is missing in the link field \"{0}\".", field.DisplayName);
// return the failed result value defined in the parameters for this validator; if no Result parameter
// is defined, the default value FatalError will be used
return this.GetFailedResult(ValidatorResult.CriticalError);
}
protected override ValidatorResult GetMaxValidatorResult()
{
return this.GetFailedResult(ValidatorResult.Error);
}
}
}
My issue is that I would like to apply this validator to every link on the site. Right now, to apply the validator (say for all Button templates on the site) I have to go to the individual link field in a template and select the validator in the Validation Rules
section of the standard fields list. I've tried applying the validator to the General Link
and Link
field types validators as follows but that did not seem to prompt the validator to run on save for those fields:
Any thoughts on how I can get this validator to be applied site-wide? Thanks for the help!