7
votes

Consider the template:

Company

  • Logo (Image field)
  • Company Name (Text field)

The Company template has standard values set on both fields. If we get a Company item and save it using Glass without making any changes, the Logo field no longer uses the standard value. (The Company Name field is untouched.)

The issue, it seems, is that the Glass.Mapper.Sc.DataMappers.SitecoreFieldImageMapper serializes the value of that field differently than Sitecore does. When it tries to save, it thinks it's a change to the field and no longer uses the standard value.

Standard Value:

<image mediaid="{GUID}" />

Glass-generated Value:

<image height="64" width="64" mediaid="{GUID}" alt="Alt text" />

Is there a way to make Glass generate the same output as Sitecore?

1

1 Answers

0
votes

I think that problem is in a way how SitecoreFieldImageMapper map ImageField to Image. For getting Height, Width and Alt are used public properties. If we look on them via reflector we will see that values of it get not directly from field:

public string Alt
{
    get
    {
        string text = base.GetAttribute("alt");
        if (text.Length == 0)
        {
            Item item = this.MediaItem;
            if (item != null)
            {
                MediaItem mediaItem = item;
                text = mediaItem.Alt;
                if (text.Length == 0)
                {
                    text = item["Alt"];
                }
            }
        }
        return text;
    }
    set
    {
        base.SetAttribute("alt", value);
    }
}

If field does not contain value(e.g. for "alt": if (text.Length == 0)) then value will be received from MediaItem that is linked. It cause adding Height, Width and Alt from media library item after saving of field.

To fix this problem you could try replace this code:

int height = 0;
int.TryParse(field.Height, out height);
int width = 0;
int.TryParse(field.Width, out width);

img.Alt = field.Alt;
img.Height = height;
img.Width = width;

with direct getting attributes rather than usage of properties:

int height = 0;
if(int.TryParse(field.GetAttribute("height"), out height))
{
    img.Height = height;
}


int width = 0;
if(int.TryParse(field.GetAttribute("width"), out width))
{
    img.Width = width;
}

img.Alt = field.GetAttribute("alt");

With Alt property everything should be ok. But there could be problems with Width and Height as they are not Nullable and I am not sure how GlassMapper will handle Image with Width and Height that you haven't set.