Home » Customization » Set Default Alternate Text on Images Uploaded to the Sitecore Media Library

Set Default Alternate Text on Images Uploaded to the Sitecore Media Library

Sitecore Technology MVP 2016
Sitecore MVP 2015
Sitecore MVP 2014

Enter your email address to follow this blog and receive notifications of new posts by email.

Tweets

For the past couple of years, I have been trying to come up with an idea for adding a custom <getMediaCreatorOptions> pipeline processor — this is no lie or exaggeration — but had not thought of any good reason to do so until today: I figured out that I could add a processor to set default alternate text on an image being uploaded into the Sitecore Media Library.

The following class contains code to serve as a <getMediaCreatorOptions> pipeline processor to set default alternate text on an image Item during upload:

using Sitecore.Diagnostics;
using Sitecore.Pipelines.GetMediaCreatorOptions;

namespace Sitecore.Sandbox.Pipelines.GetMediaCreatorOptions
{
    public class SetDefaultAlternateTextIfNeed
    {
        public void Process(GetMediaCreatorOptionsArgs args)
        {
            Assert.ArgumentNotNull(args, "args");
            if (!string.IsNullOrWhiteSpace(args.Options.AlternateText))
            {
                return;
            }

            args.Options.AlternateText = GetAlternateText(args);
        }

        protected virtual string GetAlternateText(GetMediaCreatorOptionsArgs args)
        {
            Assert.ArgumentNotNull(args, "args");
            if (string.IsNullOrWhiteSpace(args.Options.Destination) || args.Options.Destination.IndexOf("/") < 0)
            {
                return string.Empty;
            }

            int startofNameIndex = args.Options.Destination.LastIndexOf("/") + 1;
            return args.Options.Destination.Substring(startofNameIndex);
        }
    }
}

The code above will set the AlternateText property of the Options property of the GetMediaCreatorOptionsArgs instance when its not set: I set it to be the name of the Media Library Item by default — I extract this from the path destination of the Item.

I then registered the above class as a <getMediaCreatorOptions> pipeline processor in the following Sitecore configuration file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <getMediaCreatorOptions>
        <processor type="Sitecore.Sandbox.Pipelines.GetMediaCreatorOptions.SetDefaultAlternateTextIfNeed, Sitecore.Sandbox"/>
      </getMediaCreatorOptions>
    </pipelines>
  </sitecore>
</configuration>

Let’s try this out.

I went to my Media Library, and selected an image to upload:

selected-image-upload

During the upload, I did not specify its alternate text.

As you can see, it was given an alternate text value by default:

default-alt-text-set

If you have any thoughts on this, please drop a comment.

But whatever you do, just don’t let this happen to you:

anxiety-cat

Advertisements

6 Comments

  1. Great post! I’ve done similar things previously and one, slightly related problem, is multi-lingual solutions and things that may happen when you add for example a language to it. By default, adding a language means you end up with missing versions, i.e. null values for that language. As far as I remember, during upload, the alt-text set on all language versions. One option is of course changing the alt-field to shared.

  2. Jan Sommer says:

    An alternative could be to add default values to the image template and write $name as the default for the Alt field. This should be done early, but one could also just write a script that takes the name of an item and sets it as the Alt text if $name is added later.

  3. Alt text is there for accessibility, and shouldn’t always be set, so setting a default value might not be the best thing to do here. The alt attribute should always be present in the img tag however, but can be empty. It should only contain a value when there is a descriptive benefit to people using assistive tech like screen readers. The file name wouldn’t be an appropriate alt tag very often.
    I don’t think there’s an elegant way to handle this unfortunately, outside of training editors to manage alt tags properly!

    • I believe an Alt text value — not an empty Alt attribute — is required for 508 Compliance — if I am incorrect on this assumption, please let me know — hence providing an Alt text value by default would be beneficial, albeit I do agree it should be set by the uploading party to set context for the uploaded image.

      Mike

  4. Jan Sommer says:

    While upgrading to Sitecore 7 I noticed this setting: Media.AutoSetAlt
    Description: Indicates if the Alt field on the Image template is set automatically to the file path when the image is uploaded. If false, the Alt field is blank.
    Could be that one should use this field if using Sitecore 7.

Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: