Dheeraj Palagiri
Dheeraj Palagiri

Reputation: 1879

GlassMapper Render Custom Link Field

We are on sitecore 8.1 update 3 and Glass Mapper 4.2.1.188

For normal Link field its working fine in experienced editor and normal mode.

We have duplicated General Link field in core database and removed "Javascript" menu item. That's the only change we have made for custom Link field.

This makes field disappeared in experience editor mode. Its fine in normal mode.

@RenderLink(x => x.CallToActionButton, new { @class = "c-btn c-btn--strong c-btn--large" }, isEditable: true)

Edit 1:

when i use Sitecore Field renderer its all good.

 @Html.Sitecore().Field(FieldIds.HomeCallToActionButton, new { @class = "c-btn c-btn--strong c-btn--large" })

Any suggestions will be appreciated.

Upvotes: 1

Views: 733

Answers (1)

Marek Musielak
Marek Musielak

Reputation: 27132

The reason of you problem is that Sitecore checks the field type key while generating the code which is displayed in Experience Editor.

Sitecore.Pipelines.RenderField.GetLinkFieldValue class checks if the field type key is either link or general link and from what you wrote, you had copied original General Link field so your name of the field is Custom Link or something like that. Which means that field type key is custom link in your case (field type name lowercase). SkipProcessor method compares custom link with field type key, and because it's different, processor ignores your field.

You cannot simply rename your field to General Link and put it under Field Types/Custom folder cause Sitecore doesn't keep id of the field type (it stores field type key instead).

What you can do is override Sitecore.Pipelines.RenderField.GetLinkFieldValue class and one of its method like that:

using Sitecore.Pipelines.RenderField;

namespace My.Assembly.Namespace
{
    public class GetLinkFieldValue : Sitecore.Pipelines.RenderField.GetLinkFieldValue
    {
        /// <summary>
        /// Checks if the field should not be handled by the processor.
        /// </summary>
        /// <param name="args">The arguments.</param>
        /// <returns>true if the field should not be handled by the processor; otherwise false</returns>
        protected override bool SkipProcessor(RenderFieldArgs args)
        {
            if (args == null)
                return true;
            string fieldTypeKey = args.FieldTypeKey;
            if (fieldTypeKey == "custom link")
                return false;
            return base.SkipProcessor(args);
        }
    }
}

and register it instead of the original class:

<sitecore>
  <pipelines>
    <renderField>
        <processor type="Sitecore.Pipelines.RenderField.GetLinkFieldValue, Sitecore.Kernel">
           <patch:attribute name="type">My.Assembly.Namespace.GetLinkFieldValue, My.Assembly</patch:attribute>
        </processor>
    </renderField>
  </pipelines>
</sitecore>

Upvotes: 2

Related Questions