Skip to main content
added 1451 characters in body
Source Link

You can add additional attributesBefore implementing this solution you will need to download the general link usinglatest nightly. I made a couple of changes to make it easier to solve this problem.

Ok, firstly create a sub-type to map the anonymous typeadditional link attributes onto, e.g.:

public @Html.class MyLink:Glass.Mapper.Sc.Fields.Link{
   public string MyAttribute{get;set;}
}

Then create a new DataMapper:

public class SitecoreFieldMyLinkMapper : SitecoreFieldLinkMapper
{
    public SitecoreFieldMyLinkMapper()
        : base(new UrlOptionsResolver(), typeof(MyLink))
    {

    }

    public override object GetField(Sitecore.RenderLinkData.Fields.Field field, SitecoreFieldConfiguration config,
        SitecoreDataMappingContext context)
    {
        if (field == null || field.Value.Trim().IsNullOrEmpty()) return null;

        MyLink link = new MyLink();
        LinkField linkField = new LinkField(field);

        MapToLinkModel(link, xlinkField, =>config);

 x       link.LinkMyAttribute = linkField.GetAttribute("something");

        return link;

    }
}

Then create a HTML extension:

public static class HtmlExtensions
{
    public static string RenderMyLink<T>(this IGlassHtml html, T model, Expression<Func<T, object>> field,
        bool isEditable = false, string contents = null)
   new {@class
        var myLink = classNamehtml.GetCompiled(field).Invoke(model) as MyLink;

        return html.RenderLink(model,data_target= "#orderModal"field, new {data_type = myLink.MyAttribute}, falseisEditable, contents);
    }
}

You could then create an extension methodAgain this only works with the latest nightly because I made some changes to IGlassHtml that acceptssupport this. If you don't want to use the correct parametersnightly you will need to copy and constructspaste some code from the anonymous type for other developersSitecoreFieldLinkMapper and the GlassHtml classes.

You can add additional attributes to the general link using the anonymous type:

 @Html.Glass().RenderLink(link, x => x.Link, 
               new {@class = className,data_target= "#orderModal" }, false)

You could then create an extension method to IGlassHtml that accepts the correct parameters and constructs the anonymous type for other developers.

Before implementing this solution you will need to download the latest nightly. I made a couple of changes to make it easier to solve this problem.

Ok, firstly create a sub-type to map the additional link attributes onto, e.g.:

public class MyLink:Glass.Mapper.Sc.Fields.Link{
   public string MyAttribute{get;set;}
}

Then create a new DataMapper:

public class SitecoreFieldMyLinkMapper : SitecoreFieldLinkMapper
{
    public SitecoreFieldMyLinkMapper()
        : base(new UrlOptionsResolver(), typeof(MyLink))
    {

    }

    public override object GetField(Sitecore.Data.Fields.Field field, SitecoreFieldConfiguration config,
        SitecoreDataMappingContext context)
    {
        if (field == null || field.Value.Trim().IsNullOrEmpty()) return null;

        MyLink link = new MyLink();
        LinkField linkField = new LinkField(field);

        MapToLinkModel(link, linkField, config);

        link.MyAttribute = linkField.GetAttribute("something");

        return link;

    }
}

Then create a HTML extension:

public static class HtmlExtensions
{
    public static string RenderMyLink<T>(this IGlassHtml html, T model, Expression<Func<T, object>> field,
        bool isEditable = false, string contents = null)
    {
        var myLink = html.GetCompiled(field).Invoke(model) as MyLink;

        return html.RenderLink(model, field, new {data_type = myLink.MyAttribute}, isEditable, contents);
    }
}

Again this only works with the latest nightly because I made some changes to support this. If you don't want to use the nightly you will need to copy and paste some code from the SitecoreFieldLinkMapper and the GlassHtml classes.

Source Link

You can add additional attributes to the general link using the anonymous type:

 @Html.Glass().RenderLink(link, x => x.Link, 
               new {@class = className,data_target= "#orderModal" }, false)

You could then create an extension method to IGlassHtml that accepts the correct parameters and constructs the anonymous type for other developers.