I've been recently exploring Sitecore DAM (Content Hub) module and installed it on existing website that used Glass Mapper. I followed the installation guide, configured all required properties, updated image field value to use the logo from the Content Hub and... nothing changed at front-end, the hero image was still being served from the media library.
Not for a moment did I think there is going to be an easy fix. I looked up the configs and noticed that the DAM module is customizing renderField pipeline which is known to have a different view on how fields should be rendered. Various blog posts provided solutions for earlier versions of Glass, so I had spend almost an hour trying to override image field rendering. Most of the tutorials at Glass official website returned 404 error and directed me to the Training page...
Long story short: you need to perform two simple steps to enable Content Hub with Glass version 5.x:
Firstly, create a custom class that inherits from standard Glass FieldImageMapper and checks whether image field contains Content Hub resource:
public class ContentHubImageFieldMapper : SitecoreFieldImageMapper
{
public override object GetField(Field field, SitecoreFieldConfiguration config, SitecoreDataMappingContext context)
{
if (string.IsNullOrEmpty(field?.Value))
{
return null;
}
var imageField = base.GetField(field, config, context);
if (field.Value.Contains(Utilities.Constants.CONTENT_HUB_CONTENT))
{
var xElement = XElement.Parse(field.Value);
var src = xElement.Attribute("src");
((Image) imageField).Src = src?.Value;
}
return imageField;
}
}
Secondly, update GlassMapperScCustom.cs (you should have this file in one of the projects if you use Glass), add the code below:
dependencyResolver.DataMapperFactory.Replace< SitecoreFieldImageMapper, ContentHubImageFieldMapper>(() => new ContentHubImageFieldMapper());
right after "var dependencyResolver = new DependencyResolver(config);"
That's it, now compile the project, deploy it and enjoy the benefits of centralized asset management, out of the box CDN and light content database.
No comments:
Post a Comment