Copied to clipboard

Flag this post as spam?

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


  • Keith Boynton 19 posts 61 karma points
    May 30, 2020 @ 18:36
    Keith Boynton
    0

    How to get a media item when in a publishing event handler

    I have a custom publishing component that I use for some custom validation.

    In this handler I'm applying some validation to some simple fields such as checked publishing dates and keywords.

    Here's a snippet of the code:

    private void ContentService_Publishing(IContentService sender, ContentPublishingEventArgs e)
        {
            // Handle any blog articles being published
            var blogArticleEntitiesBeingPublished = e.PublishedEntities.Where(x => x.ContentType.Alias.Equals("blogArticle")).ToList();
            foreach (var blogArticleEntity in blogArticleEntitiesBeingPublished)
            {
                // Check keywords (must have a minimum of 3)
                var keywords = blogArticleEntity.GetValue<string>("keywords");
                var keywordsList = JsonConvert.DeserializeObject<List<string>>(keywords);
                if (keywordsList != null && keywordsList.Count() < 3)
                {
                    e.CancelOperation(new EventMessage("Keywords", "The article needs at least 3 keywords", messageType: EventMessageType.Error));
                    break;
                }
    
                // Force articles with a future publish date to use the scheduled publish feature
                var publishDate = blogArticleEntity.GetValue<DateTime>("publishDate");
                if (publishDate != null && publishDate > DateTime.Now)
                {
                    e.CancelOperation(new EventMessage("Publish Date", "The article has a publish date in the future, please use the Schedule feature to publish the article on the publish date", messageType: EventMessageType.Error));
                    break;
                }
    
                // Make sure there is alt text on the main image
                var mainImage = blogArticleEntity.GetValue("newMainImage");
                if (mainImage != null)
                {
                    // Hmmm?
                }
            }
        }
    

    The keywords and publish date validation is working fine, but I can't work out for the life of me how to get the newMainImage media item.

    enter image description here

    As you can see I can get something for the newMainImage property value but I have no idea how to get at the media item so I can access my custom alternativeText property on it.

    Hopefully this is something pretty simple, but I'm drawing a blank everywhere I look....?

  • Keith Boynton 19 posts 61 karma points
    May 30, 2020 @ 19:23
    Keith Boynton
    0

    I've found something that seems to work, I'm just not sure if it's a good way to do it...

                // Make sure there is alt text on the main image
                var mainImage = blogArticleEntity.GetValue("newMainImage");
                if (mainImage != null)
                {
                    var mainImageUdi = Udi.Parse((string)mainImage);
                    using (var contextReference = _contextFactory.EnsureUmbracoContext())
                    {
                        var mainImageMediaItem = contextReference.UmbracoContext.Media.GetById(mainImageUdi);
                        if (mainImageMediaItem != null)
                        {
                            var mainImageAltText = mainImageMediaItem.Value<string>("alternativeText");
                            if (string.IsNullOrWhiteSpace(mainImageAltText))
                            {
                                e.CancelOperation(new EventMessage("Main Image", "The main image is missing alternative text", messageType: EventMessageType.Error));
                                break;
                            }
                        }
                    }
                }
    

    Is there a better way to do it or does this look like a sensible way?

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies