In my code, if the Link field target Item matches the context item I want to add CSS class as active and also add the attribute aria-current=page.
Is there a way to add custom attributes to the Glass Mapper Link
?
foreach (var languageResource in languageResourcesList.Children)
{
var languageResourceItem = new MvcContext().SitecoreService.GetItem<LanguageResources>(languageResource.Id);
Glass.Mapper.Sc.Fields.Link linkField = languageResourceItem.LanguagePageLink;
if (linkField != null)
{
var targetItemId = linkField.TargetId;
if (targetItemId == Sitecore.Context.Item.ID.ToGuid())
{
linkField.Class = "active";
// add attribute aria-current=page to the Link field
}
}
languageResources.Add(languageResourceItem);
}
Cshtml:
@using (Html.Glass().BeginRenderLink(@languageResource, y => y.LanguagePageLink, isEditable: true))
{
<span class="label">@languageResource.LanguageLabel</span>
<span class="lang" lang="@languageResource.LangaugeCode">@languageResource.NativeLanguageLabel</span>
}
Expected output:
<a href="/xx/xx/xxx" aria-current="page" class="active">
<span class="label">Chinese</span><span class="lang" lang="ch">中文</span>
</a>