I did a talk at Umbraco UK Festival 2019 called 10 tips for building better Umbraco websites.

One of those tips was about animating and changing the colour of tree icons in the media section when the media item doesn't have an Alt Text property value.

no alt text image animation

This blog post will show you how to do it yourself.

Add the Alt Text property

In the Settings section of Umbraco, go to Media Types > Images and add a new textstring property called 'Alt Text' with the alias 'altText'

Create a component to hook into the Tree Nodes Rendering event.

using Umbraco.Core; using Umbraco.Core.Composing; using Umbraco.Web; using Umbraco.Web.Trees;

namespace Talk.Core.Composing
{

    [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
    public class TreeNodeRenderingComposer : ComponentComposer<TreeNodeRenderingComponent>, IUserComposer
    { }

    public class TreeNodeRenderingComponent : IComponent
    {
        private readonly IUmbracoContextAccessor _umbracoContextAccessor;

        public TreeNodeRenderingComponent(IUmbracoContextAccessor umbracoContextAccessor)
        {
            _umbracoContextAccessor = umbracoContextAccessor;
        }

        public void Initialize()
        {
            TreeControllerBase.TreeNodesRendering += TreeControllerBase_TreeNodesRendering;
        }

        public void Terminate()
        { }

        private void TreeControllerBase_TreeNodesRendering(TreeControllerBase sender, TreeNodesRenderingEventArgs e)
        {
            if (sender.TreeAlias == "media")
            {
                foreach (var node in e.Nodes)
                {
                    if (node.NodeType == "media")
                    {
                        if (int.TryParse(node.Id.ToString(), out var nodeId) && nodeId > 0)
                        {
                            var mediaItem = _umbracoContextAccessor.UmbracoContext.Media.GetById(nodeId);
                            if (mediaItem != null)
                            {
                                if (mediaItem.ContentType.Alias != "Folder" && (!mediaItem.HasValue("altText")
                                    || string.IsNullOrWhiteSpace(mediaItem.Value<string>("altText"))))
                                {
                                    node.CssClasses.Add("alt-text-missing");
                                }
                            }
                            else
                            {
                                var contentService = Current.Services.MediaService;
                                var mediaItemFromService = contentService.GetById(nodeId);
                                if (mediaItemFromService != null && mediaItemFromService.ContentType.Alias == "Folder"
                                    && (!mediaItemFromService.HasProperty("altText")
                                    || string.IsNullOrWhiteSpace(mediaItemFromService.GetValue<string>("altText"))))
                                {
                                    node.CssClasses.Add("alt-text-missing");
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

This will add class to any image which doesn't have the 'altText' value filled in.

Create a CustomStyles folder in App_Plugins

This will allow you to change the style of the backoffice.

Add a package.manifest file to this CustomStyles folder with these contents:

{
    "css": [
        "~/App_Plugins/CustomStyles/css/customstyles.css"
    ]
}

In this folder, add a css folder and in it create a file named customstyles.css

Put this in the css file:

li.alt-text-missing i.umb-tree-icon {
    color: red;
    animation: infinite-spinning 2s infinite;
}

@keyframes infinite-spinning {
    from {
        transform: rotate(0deg);
    }
    to {
        transform: rotate(360deg);
    }
}

View the changes

That should be it. To view the changes, make sure you are running in debug mode in your local environment, or increment the Client Dependency version. Reload the page using Ctrl + F5 to empty the cache and hard reload.

Want to thank me?

If I've helped you out and you want to thank me, why not buy me a coffee?

Buy me a coffee

About the author

Paul Seal

Umbraco MVP and .NET Web Developer from Derby (UK) who specialises in building Content Management System (CMS) websites using MVC with Umbraco as a framework. Paul is passionate about web development and programming as a whole. Apart from when he's with his wife and son, if he's not writing code, he's thinking about it or listening to a podcast about it.

Related Posts

How to search by picked multi node tree picker values in Umbraco v8

This post shows you how you can use examine to search for articles which have the category picked in…

Read Post

How to enable AMP in Umbraco 7 Websites

This post shows you how I set up AMP in my Umbraco 7 website

Read Post

How to render the actual document type icons in the tree in Umbraco

This post gives you the code to be able to see the icons for your document types in the tree view ra…

Read Post

How to solve the error assets file project.assets.json not found in Visual Studio

This post tells you how to solve the error assets file project.assets.json not found in Visual Studi…

Read Post