Sitecore Content Hub - Using Transformations on our Sitecore site

Transformations are a very important (and often ignored) feature, it basically lets you crop images on the fly and set the quality of images, without the need of creating custom public links. This is Sitecore's documentation related to Content Hub Transformations, however, how do we actually use these from a Sitecore site? 

The short answer is you can't use Transformations using the Sitecore Connect for Content Hub connector. I have talked to Sitecore Support and they added a feature request so that public links inserted from the connector support transformations (reference number 518659). 

In the meantime, what we can do, is add the transformations programmatically (I know, it's not the best) but if we want to leverage Content Hub Transformations on our Sitecore sites that's the only thing we can do for now. Sitecore support recommended using something like this in code: 

Sitecore.Data.Fields.XmlField imageField = Sitecore.Context.Item.Fields["image"];

string src = imageField.GetAttribute("src") + "&t=mytransformation";

Then we would be able to use the src as our img src. 

Luckily, on the project I needed to use this, I was using Constellation For Sitecore and after talking to Rick Cabral (and after a couple rounds of collab and bug fixes), we were able to add support for this with the latest versions of Model Mapper. 

In order to insert a Content Hub transformation with Model Mapper, we only need to map our image field to an ImageModel and do something like this: 

@if (Model.MediaImage.IsContentHubContent)
{
  <img src="@(Model.MediaImage.GetContentHubTransformationSrc("TransformationNameGoesHere"))" alt="@(Model.MediaImage.Alt)" />
}


Comments