Tippu
Tippu

Reputation: 1261

Sitecore: How to get the class entered in General Link field

In the General Link field content author will set some class.

I want to change the class while rendering, so I used the reflector and got the code of Link.cs and trying to extend the PopulateParameters method and looks like this.CssStyle and this.CssClass are always blank. Is there any way to get the value of class entered in the General Link field?

enter image description here

Upvotes: 3

Views: 2394

Answers (1)

Marek Musielak
Marek Musielak

Reputation: 27132

Cast your field to LinkField class and use Class property:

LinkField field = Sitecore.Context.Item.Fields["Link"];
string cssClass = field.Class;

**EDIT: **

If you want to change behaviour of Sitecore sc:link to change css class of every link, you need to add your own processor to the renderField pipeline:

public class UpdateLinkClass
{
  public void Process(Sitecore.Pipelines.RenderField.RenderFieldArgs args)
  {
    if (args != null && (args.FieldTypeKey == "link" || args.FieldTypeKey == "general link"))
    {
      Sitecore.Data.Fields.LinkField linkField = args.Item.Fields[args.FieldName];
      if (!string.IsNullOrEmpty(linkField.Class))
      {
        args.Parameters["class"] = linkField.Class + "-custom";
      }
    }
  }
}

and register it before GetLinkFieldValue processor:

<processor type="My.Assembly.Namespace.UpdateLinkClass, My.Assembly" />
<processor type="Sitecore.Pipelines.RenderField.GetLinkFieldValue, Sitecore.Kernel" />

Upvotes: 5

Related Questions