How to extend Glass.Mapper.Sc.Fields.Image with additional properties?

I save crop information on my images in the Sitecore library in the field that was added to the /sitecore/templates/System/Media/Unversioned/Image .

I would like to access this field along with all the other properties that exist in a complex field like Glass.Mapper.Sc.Fields.Image so that I can continue to use GlassHtml.RenderImage() in my views.

My initial attempts to inherit from the class were unsuccessful - it seems to have violated the display behavior - so I wonder if there is another way to extend this class with additional properties?

Here is what I tried:

 [SitecoreType(AutoMap = true)] public class MyImage : Glass.Mapper.Sc.Fields.Image { public virtual string CropInfo { get; set; } } 
+4
source share
1 answer

You will need to implement a custom data handler to map an additional field.

I would create a data handler that inherits from the standard Image data handler:

https://github.com/mikeedwards83/Glass.Mapper/blob/master/Source/Glass.Mapper.Sc/DataMappers/SitecoreFieldImageMapper.cs

Then configure GetField and SetField .

Once you have created a custom data handler, you need to register it in the Windsor container. See Tutorial 19 on how to do this:

http://glass.lu/docs/tutorial/sitecore/tutorial19/tutorial19.html

An important part:

 public static void CastleConfig(IWindsorContainer container){ var config = new Config(); container.Register( Component.For < AbstractDataMapper>().ImplementedBy<TweetsDataHandler>().LifeStyle.Transient ); container.Install(new SitecoreInstaller(config)); } 
+4
source

Source: https://habr.com/ru/post/1501715/


All Articles