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 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

Code to help you debug an umbraco issue on a remote site

This post gives you some razor code to help you see the values of the IPublishedContent item's prope…

Read Post

How to fix the 403 error on preview in Umbraco after upgrading

This post shows you how to fix the 403 error on preview after you have upgraded it.

Read Post

How to restrict Content Apps by user groups in Umbraco

In this post I give you some code so you can restrict the Content Apps on your Umbraco site by user…

Read Post