0

When we try to add a data-attribute to a field which refers to an image field, it displays an image tag instead of the image path/url. Screen shot below for your reference:

enter image description here

Using SXA 1.9.

Thanks in Advance

Regards

1

You could just use a Scriban object here instead of a field. Looking at your example, I can understand you have 2 images, Image and ThumbImage and I believe your goal is to have a Html element like that: <img src="URL_to_IMAGE" useful params data-progressive="URL_TO_THUMBIMAGE"/>

This can be done in a one-liner in Scriban:

{{ sc_field i_item 'Image' [['data-progressive', i_item.ThumbImage.media_url]] }}
0

I believe only text field are supported, you will have to extend to support link/image fields:

Have a look at following article:

https://ggullentops.blogspot.com/2018/10/using-tokens-in-sitecore-sxa-variant.html?m=1

Regards Bryan

1
  • 2
    Answers need to be able to stand on their own; not just be a link to some other resource. – Mark Cassidy Mar 2 '20 at 23:21
0

The attribute value being output is the raw value of the associated image field value.

<image mediaid=''

I followed the same image responsive blog and after some investigation and decompiling the DLLs I found that the value is printed out by:

    <renderVariantField>
        <processor type="Sitecore.XA.Foundation.RenderingVariants.Pipelines.RenderVariantField.RenderVariantField, Sitecore.XA.Foundation.RenderingVariants" resolve="true" />
    </renderVariantField>

Essentially this happens in the method:

protected override string ResolveAttributeValues(string value, Item item, object model)

The token is replaced by the raw field value.

I got this same result in Sitecore 9.3 and the matching SXA version.

This is the first processor in that pipeline and whether or not its a bug or just an omission in the code to handle image URL attributes I'm not sure.

Either way here is a solution/patch:

 public class MyRenderVariantField : RenderVariantField
{
    protected override string ResolveAttributeValues(string value, Item item, object model)
    {
        var imageResult = RetrieveImageTokenUrl(value, item);
        if (!string.IsNullOrEmpty(imageResult))
            return imageResult;
        IKeyValueSourceFactory <Item> service = new ItemFieldNameKeyValueSourceFactory();
        IKeyValueSourceFactory<object> service2 = ServiceLocator.ServiceProvider.GetService<IKeyValueSourceFactory<object>>();
        IPatternProcessor service3 = new PatternProcessor();
        var processed = service.Create(item);
        value = service3.Process(value, processed);
        value = service3.Process(value, service2.Create(model));
        return value;
    }
    protected string RetrieveImageTokenUrl(string value, Item item)
    {
        string singleToken = string.Empty;
        foreach (Match regexItem in new Regex("\\$\\(([^$()]+)\\)", RegexOptions.Compiled).Matches(value))
        {
            singleToken = regexItem.Groups[1].Value;
        }
        if (!string.IsNullOrEmpty(singleToken))
        {
            var matchingField = item.Fields[singleToken];
            if (matchingField != null && matchingField.HasValue)
            {
                var imageField = (Data.Fields.ImageField)matchingField;
                if (imageField != null && imageField.MediaItem != null)
                    singleToken = MediaManager.GetMediaUrl(imageField.MediaItem);
            }
        }
        return singleToken;
    }
}

Extend the existing RenderVariantField and override the ResolveAttributeValues method.

I added some code at the top of the ResolveAttributeValues that uses a new method to detect if we are handling an image in our Attribute.

Now patch your new Processor in place over the original like so:

    <pipelines>
        <renderVariantField>
            <processor type="Sitecore.Foundation.ImageCompression.MyRenderVariantField, Sitecore.Foundation.ImageCompression" resolve="true" />
        </renderVariantField>
    </pipelines>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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