Copied to clipboard

Flag this post as spam?

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


  • bh 444 posts 1544 karma points
    Apr 05, 2018 @ 16:55
    bh
    0

    Alt Text on Media Picker Image

    At the bottom of this page there is a reference to a .GetPropertyValue("alt") on a media picker object. Where does one input the alt text associated with that "alt" attribute?

  • Nik 1612 posts 7258 karma points MVP 7x c-trib
    Apr 05, 2018 @ 17:25
    Nik
    4

    Hi Bh,

    By default, the Media item doesn't have an alt field. So what you'd need to do is, assuming you have access to the full back office of your Umbraco site, is find the "Media Types" entry. I believe it is in the Settings section. Under Media types you should find the Image media type.

    This is very similar to a document type in that you can add additional properties to it so the first step is to add a property called Alt (or Alt Description, or whatever you would like to call it).

    In most situations, I would add this as a Text String property type.

    Once you've added that, you can then start thinking about adding Alt Descriptions to your media items.

    This is done by going to the Media section of the website and finding the images in there that you want to add Alt text to. Browse to the image in the back office to the point where you can see the uploaded file and all the properties. This is where you'd now see your new Alt field. Add text here and then, your .GetPropertyValue("alt") should work. (Assuming the alias of your property is "alt", if it isn't, change it to be the correct alias.

    Hope that helps :-)

    Nik

  • bh 444 posts 1544 karma points
    Apr 05, 2018 @ 17:47
    bh
    0

    @Nik that's the direction, I was headed before I saw that .GetPropertyValue("alt") in the documentation. So, I already added the alt attribute to my image media type, and I get where I input that data. What i'm struggling with on that option is how to access my alt text in Razor.

    This isn't working...

    var heroInsetAlt = Umbraco.TypedMedia(Model.Content.GetPropertyValue<int>("homeHeroInsetImage")).GetPropertyValue("umbracoAltText");
    

    Any suggestions?

  • Nik 1612 posts 7258 karma points MVP 7x c-trib
    Apr 05, 2018 @ 17:56
    Nik
    0

    Hi BH,

    Can you show what your Image Media Type looks like?

    Nik

  • bh 444 posts 1544 karma points
    Apr 05, 2018 @ 18:00
    bh
    0

    enter image description here

  • Nik 1612 posts 7258 karma points MVP 7x c-trib
    Apr 05, 2018 @ 18:07
    Nik
    0

    Okay, so that all looks right to me.

    If you break your alt text retrieval down can you check what you are getting out of "homeHeroInsetImage"

    So do this:

    var imageId = Model.Content.GetPropertValue<int>("homeHeroInsetImage");
    
    var image = Umbraco.TypedMedia(imageId);
    
    var imageAlt = image.GetPropertyValue("umbracoAltText");
    

    Then you can check each line to see what you are getting back. I suspect that either the imageId could be 0, or the Cache isn't updated with your alt text value on the media item you think you are retrieving.

    That's where I would look next :-)

    Nik

  • bh 444 posts 1544 karma points
    Apr 05, 2018 @ 18:14
    bh
    1

    @Nik I get the image id

    Update my original way actually worked as written:

    var heroInsetAlt = Umbraco.TypedMedia(Model.Content.GetPropertyValue<int>("homeHeroInsetImage")).GetPropertyValue("umbracoAltText");
    

    I didn't apply the @heroInsetAlt to the correct img tag (i put it on one image and was looking on a different image).

    Thanks for your help @Nik. You pointed me in the right direction to find my error. Many thanks!

Please Sign in or register to post replies

Write your reply to:

Draft