10

I am using DD4T (1.26.x) and ASP.NET MVC, and just noticed the useful looking AltText property on DD4T.ContentModel.IComponent.Multimedia. I have a metadata field alt_text on my multimedia component, however this (unsurprisingly) is not magically appearing in the AltText property. Is there some simple way to hook this up in the TBBs or MVC app?

6

Although Rob's suggestion to add the field name to use for alt text as a parameter to the DD4T parameter schemas is a good one, it would require some work to the DD4T framework.

There is also a way to achieve what you want without having to change DD4T. You could write a template by implementing the DD4T BaseComponentTemplate. In this template, you implement the method TransformComponent(DD4T.ContentModel.Component component). You can get to the original Tridion component by calling GetTcmComponent().

In the template, you can easily find the metadata field containing the alt text in the tcm component, and copy the value to the Multimeda.AltText property inside the DD4T component. The base template makes sure that the DD4T component is serialized and written back into the package.

5

I've tried setting a metadata value called "AltText" in the past in the hope this would go through but I now see why it didn't.

I've had a quick look at the publishing code and it looks like this property is never actually set. My guess is it's an unfinished idea, but would defer to someone to confirm.

I suspect this would need an addition to the parameter schema for the DD4T TBBs to indicate the field to look at on the schema. Although you may want to consider Alvin's points about Alt Text.

Here's the relevant portion of code from the DD4T.Templates.Base.Builder.ComponentBuilder class

      if (tcmComponent.ComponentType.Equals(TCM.ComponentType.Multimedia))
      {
          GeneralUtils.TimedLog("start building multimedia");
          Multimedia multimedia = new Multimedia();
          multimedia.MimeType = tcmComponent.BinaryContent.MultimediaType.MimeType;
          multimedia.Size = tcmComponent.BinaryContent.FileSize;
          multimedia.FileName = tcmComponent.BinaryContent.Filename;
          // remove leading dot from extension because microsoft returns this as ".gif"
          multimedia.FileExtension = System.IO.Path.GetExtension(multimedia.FileName).Substring(1);

          if (resolveWidthAndHeight)
          {
              try
              {
                  MemoryStream memstream = new MemoryStream();
                  tcmComponent.BinaryContent.WriteToStream(memstream);
                  Image image = Image.FromStream(memstream);
                  memstream.Close();

                  multimedia.Width = image.Size.Width;
                  multimedia.Height = image.Size.Height;
              }
              catch (Exception e)
              {
                  log.Warning("error retrieving width and height of image: " + e.Message);
                  multimedia.Width = 0;
                  multimedia.Height = 0;
              }
          }
          else
          {
              multimedia.Width = 0;
              multimedia.Height = 0;
          }
          c.Multimedia = multimedia;
          GeneralUtils.TimedLog("finished building multimedia");
      }
      else
      {
          c.Multimedia = null;
      }

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.