6

I have a multimedia component. The multimedia component implements the "image" schema, and the image schema defines an "AltText" field on the "Metadata Design" tab. The alt text field is marked as "mandatory".

I have a Dreamweaver Template which tries to render the multimedia component (added to the actual component in the "thumbnail" field), along with its alt text:

<!-- TemplateBeginIf cond="Component.Thumbnail" -->
    <img src="@@Component.Thumbnail@@" alt="@@Component.Thumbnail.Metadata.AltText@@" />
<!-- TemplateEndIf -->

The image renders with a source, but with no alt text:

<img alt="" src="/site/Multimedia/myimage.jpg">

The image definitely has alt text in its metadata. How do I render the alt text field for my image?

3 Answers 3

8

Write a C# TBB to pull the image (I just use a C# Fragment TBB for this kind of small stuff). Then you should be able to do @@thumbnail.Metadata.AltText@@:

        Component comp = engine.GetObject(package.GetByName(Package.OutputName)) as Component;
        ItemFields fields = new ItemFields(comp.Content, comp.Schema);
        MultimediaLinkField thumbField = fields["Thumbnail"] as MultimediaLinkField;
        Component mmComp = thumbField.Value;

        package.PushItem("thumbnail", package.CreateTridionItem(ContentType.Component, mmComp));

You can even go a few steps further and push the actual metadata onto the package so that you can do "@@thumbnailAlt@@":

        ItemFields mmMetaFields = new ItemFields(mmComp.Metadata, mmComp.MetadataSchema);
        TextField altTextField = mmMetaFields["AltText"] as TextField;
        string altText = altTextField.Value;

        package.PushItem("thumbnailAlt", package.CreateStringItem(ContentType.Text, altText));
7

I think I just need the Dreamweaver Get Extension, and then I can just do:

<img src="@@Component.Thumbnail@@" alt="@@Get("Component.Thumbnail.Metadata.AltText")@@" />

I'd like to know if there's any "simpler" way to do this — it seems strange that I need a templating extension just to output the metadata field on my MMC.

1
  • 6
    That's exactly why I wrote that extension... It should be simple.
    – Nuno Linhares
    May 8, 2013 at 1:53
1

You Can also use the DWT extension method to get the value of any field of your component. Add this method to DWT Extension(For DWT Extensions Click ). Pass the component uri of component(Image in your case), field name whose value is required and pass value "yes" if field you are passing is metadata field. In your case pass call this method as @@GetTextFieldValue(thumbnail,"thumbnailAlt","Yes")@@

when you publish this will be replaced with you thumbnail's alt text

 [TemplateCallable]
    public String GetTextFieldValue(String compUri, string fieldName, string isMetaData)
    {
        if (!string.IsNullOrEmpty(compUris))
        {
            Component component = m_engine.GetObject(compUri) as Component;
            ItemFields itemFields = null;
            String strFieldValue = String.Empty;

            if (isMetaData.Equals("Yes"))
            {
                if (component.Metadata != null)
                {
                    ItemFields metadataFields = new ItemFields(component.Metadata, component.Schema);
                    return metadataFields[fieldName].ToString();
                }
            }
            else
            {
                itemFields = new ItemFields(component.Content, component.Schema);

                foreach (ItemField field in itemFields)
                {
                    if (field.Name.Equals(fieldName))
                    {
                        if (field is TextField)
                        {
                            TextField fieldValue = field as TextField;
                            strFieldValue = fieldValue.Value.ToString();
                            return strFieldValue;
                        }
                        else if (field is DateField)
                        {
                            DateField dtField = field as DateField;
                            strFieldValue = dtField.Value.ToString();
                            return strFieldValue;
                        }
                        else if (field is KeywordField)
                        {
                            KeywordField kwField = field as KeywordField;
                            for (int keywords = 0; keywords < kwField.Values.Count; keywords++)
                            {
                                strFieldValue += kwField.Values[keywords].Title.ToString();
                                if (keywords != (kwField.Values.Count - 1))
                                {
                                    strFieldValue += ", ";
                                }
                            }
                            return strFieldValue;

                        }
                        else if (field is NumberField)
                        {
                            NumberField nmField = field as NumberField;
                            strFieldValue = nmField.Value.ToString();
                            return strFieldValue;
                        }
                        else return String.Empty;
                    }
                }
            }
        }
        return String.Empty;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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