Versions used: Sitecore Experience Platform 9.2 Initial Release. Content Hub 3.2.0
This post is way overdue as I was supposed to release it after my last talk at The Sitecore Sessions. Sorry for the delay 😞.
This post explains the CMP connector extension that synchronises Content Hub images set on
M.Content entities. Using the same methodology, this could grow into a module and include other field types (e.g. references to other entities). Source Code here.
This solution extends the existing CMP connector by adding a new processor to process a new mapping field type that maps an image relation to a Sitecore field using the same format supported by Sitecore connector for Content Hub DAM; so DAM connector needs to be installed.
The default connector will process items of template type
/sitecore/templates/CMP/Field Mapping, so we need a new template item (
/sitecore/templates/CMP/Image Field Mapping) that our custom processor can use to identify image mapping configurations. This template has
Field Mapping as its base template plus 2 extra fields:
AssetIndex to specify the index of the asset relation to grab (if multiple are allowed), and
RenditionToUse to specify which asset rendition public link to use.
CMP Field Name is used as the relation to find the asset on (
CmpContentToLinkedAsset is available OOTB).
The new processor runs after the default ones.
<processor type="Sitecore.SharedSource.CMP.Connector.CustomFields.Pipelines.SaveImageFieldValues, Sitecore.SharedSource.CMP.Connector.CustomFields" patch:after="processor[@type='Sitecore.Connector.CMP.Pipelines.ImportEntity.SaveFieldValues, Sitecore.Connector.CMP']"/>
This processor SaveImageFieldValues.cs fetches all
Field Mapping items (from
ImportEntityPipelineArgs object) under the
Entity Mapping item being processed, reads required field values to find the rendition from the relation and stores the formatted public link URL into the specified sitecore field.
foreach (Item item in from i in args.EntityMappingItem.Children
As you can see, the code is quite simple. It loops through
ImageFieldMapping items and reads the pertinent fields; gets or creates a public link to the defined rendition, and stores the formatted image tag into the image field value.
<image stylelabs-content-type="Image" mediaid="" src="https://HOST/api/public/content/364944895?v=779a" height="1100" alt="" stylelabs-content-id="1235" width="790" thumbnailsrc="https://HOST/api/gateway/1235/thumbnail" />
Please let me know what you think and/or if you can spot any errors.