1

I am following this blog on how to set a background image from a field on the context item:

background-image: url($(Image))

However it only brings back the raw URL, excluding any height/width properties set on the field. Is there a way to have the height/width properties appended as well as the necessary media request protection hash?

I would like this:

background-image:url(/-/media/image.png?h=350&w=350&hash=97EBEDB2323B8EB7E419E49B35C1095390534044)

Instead of this:

background-image:url(/-/media/image.png)

2 Answers 2

1

There is no out of the box way to do this as of SXA 1.8.1 or below. With the following code you can extend the existing Rendering Variant section renderer to include this logic when it encounters an Image field.

First, override the built-in RenderSection pipeline:

public class RenderSection : Sitecore.XA.Foundation.RenderingVariants.Pipelines.RenderVariantField.RenderSection
{
    protected override string GetAttributeTokenValue(string fieldName, Item item)
    {
        var field = FieldTypeManager.GetField(item.Fields[fieldName]);
        if (field is ImageField)
        {
            return item.ImageFieldUrl(fieldName);
        }
        return base.GetAttributeTokenValue(fieldName, item);
    }
}

Patch it as follows

<pipelines>
    <renderVariantField>
      <processor type="Your.Foundation.Variants.Pipelines.RenderSection, Your.Foundation.Variants"
                  patch:instead="processor[@type='Sitecore.XA.Foundation.RenderingVariants.Pipelines.RenderVariantField.RenderSection, Sitecore.XA.Foundation.RenderingVariants']"
                  resolve="true" />
    </renderVariantField>
</pipelines>

And you'll need to put this in a static class (e.g. ItemExtensions)

public static string ImageFieldUrl(this Item item, string fieldName)
{
    if (item == null)
    {
        throw new ArgumentNullException(nameof(item));
    }
    if (string.IsNullOrEmpty(fieldName))
    {
        throw new ArgumentNullException(nameof(fieldName));
    }

    var field = (ImageField) item.Fields[fieldName];
    if (field == null)
    {
        return string.Empty;
    }
    else
    {
        var urlOptions = new MediaUrlOptions();
        if (int.TryParse(field.Height, out var height) && int.TryParse(field.Width, out var width))
        {
            urlOptions.Width = width;
            urlOptions.Height = height;
        }
        return HashingUtils.ProtectAssetUrl(MediaManager.GetMediaUrl(field.MediaItem, urlOptions));
    }
}
-1

Media request protection Hash is controlled by the Sitecore config setting Media.AlwaysAppendRevision. However, I'm not able to locate the code which respects the dimensions.

1
  • It does add the revision when enabling this but it does not add the media hash. Commented Jul 19, 2019 at 3:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.