Question

I am storing cropping information on my Sitecore Media Library images in a field that was added to the /sitecore/templates/System/Media/Unversioned/Image template.

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

My initial attempts to inherit from the class were unsuccessful - it appears to break the mapping behavior - so I am wondering if there is another way to extend this class with additional properties?

Here's what I've tried:

[SitecoreType(AutoMap = true)]
public class MyImage : Glass.Mapper.Sc.Fields.Image
{
    public virtual string CropInfo { get; set; }
}
Was it helpful?

Solution

You will need to implement a custom data handler to map the 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 customise GetField and SetField.

Once you have created the custom data handler you need to register it with the Windsor container. See tutorial 19 for how to do this:

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

The 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));
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top