Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ben Hill 26 posts 126 karma points
    Apr 12, 2021 @ 08:14
    Ben Hill
    0

    Adding Image Alt Tags in Bulk

    Hi There

    New to Umbraco and we are currently running Umbraco 7, I have just taken over the site from someone else who never added any alt tags to the site imagery.

    There are a few thousand images :-(, is there a way I can add the alt tag to the image in the media library and for that alt tag to propagate through every page that image has been used on?

    Thanks in advance Ben

  • Alex Skrypnyk 6166 posts 24146 karma points MVP 8x admin c-trib
    Apr 12, 2021 @ 15:43
    Alex Skrypnyk
    0

    Hi Ben

    It's actually not easy. You can add an alt tag in the rendering to all images, but the question is where to get the content for the alt tag?

    Do you want to add an editable dynamic alt tag to the CMS for each image? It's easy to add in code but quite complicated for content managers to add the text for each image.

    Thanks,

    Alex

  • Ben Hill 26 posts 126 karma points
    Apr 13, 2021 @ 07:16
    Ben Hill
    0

    Hi Alex, thanks for your reply, yes I would ideally like to add an Alt tag field for each image in the media management section, from there out content guys can populate the field.

  • Alex Skrypnyk 6166 posts 24146 karma points MVP 8x admin c-trib
    Apr 13, 2021 @ 18:52
    Alex Skrypnyk
    0

    Hi Ben

    Then it's easy

    Just add altText textstring field to image media type in Umbraco.

    And adjust image rendering on the page, to get altText you can use GetPropertyValue method on media item

    You can share the code snippet and we will adjust it together.

    Thanks,

    Alex

  • Ben Hill 26 posts 126 karma points
    Apr 14, 2021 @ 07:37
    Ben Hill
    0

    That's fantastic, thanks Alex, it will be something we are looking at next week so I will be back in touch then.

    Thanks again

  • Amir Khan 1287 posts 2744 karma points
    Apr 12, 2021 @ 18:11
    Amir Khan
    0

    Are the images named well? We usually grab the name from media as the alt tag.

  • Alex Skrypnyk 6166 posts 24146 karma points MVP 8x admin c-trib
    Apr 12, 2021 @ 22:49
    Alex Skrypnyk
    0

    Image name is not the best alt tag content. Alt tag should describe an image, not show the file name.

  • Ben Hill 26 posts 126 karma points
    Apr 13, 2021 @ 07:17
    Ben Hill
    0

    Thanks Amir, I don't actually think they are so we would have to add manually.

  • Amir Khan 1287 posts 2744 karma points
    Apr 13, 2021 @ 13:15
    Amir Khan
    0

    Correct but you can change the name in the media library to be descriptive without updating the file name.

  • Ben Hill 26 posts 126 karma points
    Apr 14, 2021 @ 07:37
    Ben Hill
    0

    Great thanks Amir

  • Ben Hill 26 posts 126 karma points
    Jun 18, 2021 @ 13:38
    Ben Hill
    0

    Hi again guys,

    Sorry it's been a while.

    I have successfully added the new property to the image media type under settings, however how would I adjust how the images render on the page to include this new property?

    Thank you in advance Ben

  • Marc Love (uSkinned.net) 447 posts 1789 karma points
    Jun 18, 2021 @ 14:31
    Marc Love (uSkinned.net)
    0

    Hi Ben,

    Your image will be of type IPublishedContent so you can get at the alternative text property the same way you would access a document type property:

    Model.Value<string>("alternativeText")
    

    Where Model is your image property and alternativeText is your alternative text property name alias.

    Cheers,

    Marc

  • Ben Hill 26 posts 126 karma points
    Jun 18, 2021 @ 14:38
    Ben Hill
    0

    Hey Marc, how have you been?

    Thanks for the reply, the problem I have is I don't know where to add that code, would it be in a file or via the Umbraco interface?

    Thanks Ben

  • Marc Love (uSkinned.net) 447 posts 1789 karma points
    Jun 18, 2021 @ 16:13
    Marc Love (uSkinned.net)
    0

    The code that outputs your image to a web page is likely within a partial view.

    If you can send me the code that is currently outputting your image I can send you the change you need to make to output the alt text.

  • Ben Hill 26 posts 126 karma points
    Jun 21, 2021 @ 12:04
    Ben Hill
    0

    Thanks Marc, I am trying to dig out the partial view now.

    Also just in case you didn't notice it's Ben from Seven Seas Worldwide :-)

  • Ben Hill 26 posts 126 karma points
    Jun 21, 2021 @ 12:14
    Ben Hill
    0

    I have found the line of code which outputs the image for the pod component which is below:

    <img class="@circleClass lazyload" src="@(pod.GetVortoValue<IPublishedContent>("podImage").GetUSNFixCropUrl(cropAlias: cropAlias, width: imageWidthSmall, height: imageHeightSmall))" data-src="@(pod.GetVortoValue<IPublishedContent>("podImage").GetUSNFixCropUrl(cropAlias: cropAlias))" alt="@pod.GetVortoValue("podImageAltText")">
    

    I have tried to change:

    @pod.GetVortoValue("podImageAltText")
    

    To

    Model.Value<string>("altText")
    

    But it didn't render as expected.

  • Marc Love (uSkinned.net) 447 posts 1789 karma points
    Jun 21, 2021 @ 16:08
    Marc Love (uSkinned.net)
    1

    Hi Ben,

    Have you added a field with alias 'altText' to your image media type?

    If you have, to get the alternative text value you would use:

    @(pod.GetVortoValue<IPublishedContent>("podImage").GetValue("altText"))
    
  • Ben Hill 26 posts 126 karma points
    Jun 22, 2021 @ 08:07
    Ben Hill
    0

    Hi Mark

    I have indeed added that field to the image media type, I will add your syntax and fingers crossed that should do it.

    Thanks Ben

  • Ben Hill 26 posts 126 karma points
    Jun 24, 2021 @ 08:07
    Ben Hill
    0

    Hi Marc

    Unfortunately I added that syntax and the page threw an error which I have listed below.

    Server Error in '/' Application.
    Compilation Error
    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
    
    Compiler Error Message: CS1928: 'Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'GetValue' and the best extension method overload 'USNStarterKit.USNHelpers.USNStringExtensions.GetValue(Examine.SearchResult, string)' has some invalid arguments
    

    Any ideas what's causing that error?

    Thanks Ben

  • Marc Love (uSkinned.net) 447 posts 1789 karma points
    Jun 24, 2021 @ 08:40
    Marc Love (uSkinned.net)
    100

    Hi Ben,

    Sorry, I didnt realise you were using Umbraco 7. The correct syntax is:

    @(pod.GetVortoValue<IPublishedContent>("podImage").GetPropertyValue("altText"))
    
  • Ben Hill 26 posts 126 karma points
    Jun 24, 2021 @ 08:43
    Ben Hill
    0

    Thanks Marc, will try that now.

  • Ben Hill 26 posts 126 karma points
    Jun 24, 2021 @ 09:01
    Ben Hill
    0

    That has worked Marc, thank you!

  • Marc Love (uSkinned.net) 447 posts 1789 karma points
    Jun 24, 2021 @ 09:27
    Marc Love (uSkinned.net)
    0

    Great, can you mark the last reply as the answer please.

  • Ben Hill 26 posts 126 karma points
    Jun 24, 2021 @ 09:28
    Ben Hill
    0

    No problem.

  • Ben Hill 26 posts 126 karma points
    Jun 24, 2021 @ 11:21
    Ben Hill
    0

    Sorry Marc, one more question, what would be the syntax to access the new "altText" property in the altText variable here:

    @if (Model.value != null)
    {   
        var url = Model.value.image;
        if(Model.editor.config != null && Model.editor.config.size != null){
            url += "?width=" + Model.editor.config.size.width;
            url += "&height=" + Model.editor.config.size.height;
    
            if(Model.value.focalPoint != null){
                url += "&center=" + Model.value.focalPoint.top +"," + Model.value.focalPoint.left;
                url += "&mode=crop";
            }
        }
    
        var altText = Model.value.altText ?? Model.value.caption ?? string.Empty;
    
        <img src="@url" alt="@altText">
    
        if (Model.value.caption != null)
        {
            <p class="caption">@Model.value.caption</p>
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft