0

I have a multilanguage setup in my sitecore instance and we are using the displayname for default URLs that works great. But we also want that the media Item (images) to show up translated in the HTML.

That was working in 9.0.2 with this Implementation => Render image with display name by default

But after upgrading my current system to Sitecore 9.3 this is not working anymore. I was hoping that the new option

 var options = MediaUrlBuilderOptions.Empty;
 options.UseDisplayName = true;
 var url = MediaManager.GetMediaUrl(imageField.MediaItem, options)

is working as the name says UseDisplayName but this seems not to work. Are there some other options to get this working in sitecore 9.3?

3
  • 1
    Maybe setting options.Language = imageField.MediaItem.InnerItem.Language would help
    – Marek Musielak
    Apr 16, 2021 at 13:42
  • 1
    Have you also tried setting to true the ootb setting <links><urlBuilder><useDisplayName>true</useDisplayName> from sitecore.config? By default <mediaUrlBuilder is referring to this setting. Apr 16, 2021 at 14:16
  • Both hints are not working. Apr 19, 2021 at 5:16

3 Answers 3

2

I have tried with a vanilla 9.3 and it seems that UseDisplayName isn't taken into consideration at all. I have looked in the MediaUrlBuilder implementation and it seems that UseDisplayName is referenced only by the ItemUrlBuilder.

enter image description here

While looking at the sitecore 10.1 implementation I can see that the MediaUrlBuilder has UseDisplayName included:

public virtual string Build(MediaItem item, MediaUrlBuilderOptions options)
{
  Assert.ArgumentNotNull((object) item, nameof (item));
  Assert.ArgumentNotNull((object) options, nameof (options));
  options.SetDefaultOptions(this._defaultOptions);
  BaseUrlBuilder<MediaUrlBuilderOptions>.UrlBuildModel model = new BaseUrlBuilder<MediaUrlBuilderOptions>.UrlBuildModel()
  {
     Item = (Item) item
  };
  this.UpdateItemPath((Item) item, model, options);
  return this.BuildUrl(model, options);
}

private void UpdateItemPath(Item item,
      BaseUrlBuilder<MediaUrlBuilderOptions>.UrlBuildModel model,
      MediaUrlBuilderOptions options)
{
   ItemPathBuilder pathBuilder = this.CreatePathBuilder(options);
   model.Path = pathBuilder.Build(item, model.TargetSite);
}

internal virtual ItemPathBuilder CreatePathBuilder(MediaUrlBuilderOptions options) => new ItemPathBuilder(this._itemManager)
{
    UseDisplayName = options.UseDisplayName
};

I would get in touch with sitecore support regarding this as it seems to be a 9.3 bug. You could also try to use the code from above to see if it will fix your issue.

1
  • thx, I will make a Support Ticket for this issue. Apr 19, 2021 at 10:18
0

Starting from Sitecore 9.3 Sitecore generates links using a the new ItemUrlBuilder for content items and MediaUrlBuilder for media items: https://doc.sitecore.com/developers/93/sitecore-experience-manager/en/build-item-and-media-urls.html

You should try to initialize a new MediaUrlBuilderOption instead of empty like:

var options =new Sitecore.Resources.Media.MediaUrlOptions();
options.UseDisplayName = true;
var url = MediaManager.GetMediaUrl(imageField.MediaItem, options)

and you can do a sitecore config patch file(recommended solution), or change directly in the default Sitecore.config file the ItemUrlBuilder section:

<links>
  <urlBuilder>
    <useDisplayName>true</useDisplayName>
    ...
  </urlBuilder>
    <itemUrlBuilder type="Sitecore.Links.UrlBuilders.ItemUrlBuilder, Sitecore.Kernel">
            <param desc="defaultOptions" type="Sitecore.Links.UrlBuilders.DefaultItemUrlBuilderOptions, Sitecore.Kernel">
                <useDisplayName ref="links/urlBuilder/useDisplayName" />
                ....

Or create a custom ItemUrlBuilder for these media calls, and then initialize that one. Your choice.

1
  • I've tried all your settings the useDisplayname = true and the MediaUrlOptions but that was not working it still takes the itemname and not the Displayname Apr 19, 2021 at 5:13
0

I've talked to the Sitecore Service and this is not a bug that the useDisplayName is not working, its just like it is in 9.3.

But I got a code snipped I am currently trying the following Code to solve my problem:

public class CustomMediaProvider : MediaProvider
{
    public override string GetMediaUrl(MediaItem item, MediaUrlBuilderOptions options)
    {
     
        try
        {
            if (Context.PageMode.IsNormal)
            {
                string url = base.GetMediaUrl(item, options);
                string displayName = options.LowercaseUrls == true
                    ? MainUtil.EncodeName(item.DisplayName.ToLower())
                    : MainUtil.EncodeName(item.DisplayName);
                string itemName = options.LowercaseUrls == true
                    ? MainUtil.EncodeName(item.Name.ToLower())
                    : MainUtil.EncodeName(item.Name);
                string extension = WebUtil.SafeEncode(StringUtil.EnsurePrefix('.',
                    StringUtil.GetString(options.RequestExtension, item.Extension, "ashx")));
                if (!string.IsNullOrEmpty(displayName) && url.Contains(itemName + extension))
                {
                    url = url.Replace(itemName + extension, displayName + extension);
                }

                return url;
            }
        }
        catch (Exception exception)
        {
            Log.Error($"Fehler beim CustomMediaProvider für das Item mit der id: {item.ID}", exception, typeof(Exception));
        }

        return base.GetMediaUrl(item, options);
    }
}

and I've registered this

<mediaLibrary>
  <mediaProvider>
    <patch:attribute name="type">NamespaceTo.CustomMediaProvider, Sales.Test.Feature.Media</patch:attribute>
  </mediaProvider>
</mediaLibrary>

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.