4

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; }
}
Liam
  • 27,717
  • 28
  • 128
  • 190
Derek Hunziker
  • 12,996
  • 4
  • 57
  • 105

1 Answers1

4

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));
} 
Liam
  • 27,717
  • 28
  • 128
  • 190
Michael Edwards
  • 6,308
  • 6
  • 44
  • 75
  • The link to tutorial 19 in this post has expired. For those looking for this page, the updated link is http://www.glass.lu/Mapper/Sc/Tutorials/Tutorial19 – NJH Aug 13 '15 at 04:30
  • I have problems with my extended image. My type also derives from Glass.Image, but RenderImage() or Editable() are not rendering a sitecore image edit field in the experience editor. I'm using the latest version of Glass with Sitecore 8.0.4. But I suspect this might be, because of the typeof(Glass.Image) check in GlassHtml ? Is there any way around this? – qkp Oct 13 '15 at 22:42
  • @qkp I'm having the same problem. The standard Image field for glass renders out the image fine but as as soon as I update it to use my custom glass image field (that inherits from image) I get no image rendered out. I've created a data mapper for it and registered it with our IOC (Simple Injector). Did you get to the bottom of the issue? – Adam Seabridge Apr 06 '16 at 10:16
  • Unfortunately not, only way I could think of to fix it was to change the source code of glass where it has this check typeof(Glass.Image).. – qkp Apr 07 '16 at 13:04
  • I've just come back to this same issue 4 years later as having the same issue with 8.2 u7 and simpleinjector. In the end to fix this I used x => x.Delegate(y => y.Image).GetValue(GetImage) in my model mapper for my Sitecore item and then called mapper.GetField(imageField, config , context) within the GetImage() method. Essentially manually calling my mapper instead of letting IoC do it. This seems to work fine. – Adam Seabridge Jul 17 '20 at 02:45