Search code examples
sitecoresitecore8glass-mapper

Sitecore Glass Mapper (v4): Add custom query string parameter via RenderImage


Using Glass Mapper for Sitecore version 4 (MVC), I need the following method call:

@Html.Glass().RenderImage(Model, x => x.PhotoField, new {cropregion = xmlValue.GetCropRegion()}, true, true)

to result in:

<img src="/sitecore/shell/-/media/Default-Website/cover.jpg?h=550&amp;w=1600&amp;la=en&amp;hash=2986CA8291803D4A8EAC3B5A1C276E27D4877DCC&amp;cropregion=534,11,1348,548" width="1600" height="550" />

Currently, the RenderImage call will result in the cropimage being added as an attribute rather than on the query string of the image. There seems to be a list of values that, if passed in RenderImage, will be added as query string parameters rather than attributes, and this list seems to be defined in the main Config class:

            ImageQueryString = new HashSet<string>(new[]
        {
            ImageParameterKeys.OUTPUT_METHOD,
            ImageParameterKeys.ALLOW_STRETCH,
            ImageParameterKeys.IGNORE_ASPECT_RATIO,
            ImageParameterKeys.SCALE,
            ImageParameterKeys.MAX_WIDTH,
            ImageParameterKeys.MAX_HEIGHT,
            ImageParameterKeys.THUMBNAIL,
            ImageParameterKeys.BACKGROUND_COLOR,
            ImageParameterKeys.DATABASE,
            ImageParameterKeys.LANGUAGE,
            ImageParameterKeys.VERSION,
            ImageParameterKeys.DISABLE_MEDIA_CACHE,
            ImageParameterKeys.WIDTH,
            ImageParameterKeys.HEIGHT
        });

So, my question is: what's the best way for me to add "cropregion" to this image parameter key hashset? Should I write my own Config file and substitute it for the stock Glass Mapper config class? Or is there some way for me to insert another value into this HashSet when Glass is initialized?


Solution

  • I think you should be able to just add a single line to your GlassMapperScCustom.CreateResolver method. Try this:

    public static IDependencyResolver CreateResolver(){
        var config = new Glass.Mapper.Sc.Config();
        config.ImageQueryString.Add("cropregion");
    
        var dependencyResolver = new DependencyResolver(config);
        // add any changes to the standard resolver here
        return dependencyResolver;
    }