Search code examples
sitecoreglass-mapper

For Glass Mapper if Internal Link is selected for General Link field then Glass Mapper is using Item Name for Link Text


How can we avoid this behavior so that if Link Description is set on the General Link Field then it takes that value and not fallback to Item name ? Glass Mapper is using the Item name of the target item for type of Internal Link in General Link Field. This behavior is because of the below

case "internal":
                var urlOptions = Utilities.CreateUrlOptions(config.UrlOptions);
                if (linkField.TargetItem == null) link.Url = string.Empty;
                else link.Url = LinkManager.GetItemUrl(linkField.TargetItem, urlOptions);
                link.Type = LinkType.Internal;
                link.TargetId = linkField.TargetID.Guid;
                link.Text =  linkField.Text.IsNullOrEmpty() ? (linkField.TargetItem == null ? string.Empty : linkField.TargetItem.Name) : linkField.Text;
                break;

in the SitecoreFieldLinkMapper.cs class. I am able to override this class and then register the Component to use the new overridden class to avoid this behavior. However this doesn't work in Page Editor and it doesn't use the new overridden class which has been registered using below code

container.Register(Component.For<Glass.Mapper.AbstractDataMapper>().ImplementedBy<MyProjectSitecoreFieldLinkMapper>().LifestyleCustom<NoTrackLifestyleManager>());

The page editor is not honoring this one. It looks like it is using different technique for resolving the dependency.


Solution

  • This is actually the default behavior of the Sitecore. So Glass is actually doing the right thing. I have to do some other tricks to make it work with my situation.