Copied to clipboard

Flag this post as spam?

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


  • Darren Hunter 137 posts 229 karma points
    Jun 02, 2023 @ 13:36
    Darren Hunter
    0

    Check Content to see if it contains a media item

    Hi,

    I am working on a media dashboard in Umbraco 10, that shows the related content to a Media Item, we are doing this so we dont have to go through each media item and check what node it attached to.

    My question is, is there a way to check an item content after it is returned in a search as IPublishedContent contains a specific media item.

    here the code I am working with at the moment.

    private PageObjectList searchContent(IMedia media, PageObjectList pageObjectList) {

            string contentName = "";
            string checkString = "Folder";
            Boolean foundFound = false;
    
            var content = _contentTypeService.GetAll();
            foreach(var itemContent in content)
            {
                var Results = _publishedContentQuery.Search(media.Name, string.Empty)
                   .Where(x => x.Content.IsDocumentType(itemContent.Alias) == true);
    
                if (Results.Any())
                {
                    foreach(var item in Results)
                    {
                        contentName = item.Content.GetType().Name.ToString();
                        foundFound = contentName.Contains(checkString);
    
                        if (!foundFound)
                        {
                            var c = item.Content;
    
                            pageObjectList.Add(item.Content.Name, item.Content.Id);
                        }
                    }
                }
    
    
            }
    
            return pageObjectList;
        }
    

    The issues I have say you have an image with the Name Double Glazing and you have some text with the same word, the search at the moment it not destination the image from the text and I am getting ironies results back in my search.

    I been trying to work out all afternoon how to check for the media key or some thing like that in the returned results with little headway.

  • Marc Goodson 2155 posts 14406 karma points MVP 9x c-trib
    Jun 03, 2023 @ 10:28
    Marc Goodson
    0

    Hi Darren

    When a page is published in Umbraco, the Reference Tracking implementation for each property on the Document Type scans for any picked or inserted images and any linked documents from the Media Section.

    When it finds them, it creates a new 'relation' between the two types of things, under the Relation Types: umbMedia and umbDocument.

    If you go into the backoffice 'Settings - RelationTypes' and click on one of Related Media or Related Document, you will see the definition of these Relation Types, and in the top right hand corner a 'Relations' icon that will show you ALL the relations between documents/media and content items.

    The RelationService is designed to allow you to query this data by either the Ids of the Content Item or the Media Item to find all the related Items.

    https://docs.umbraco.com/umbraco-cms/reference/management/services/relationservice

    So here if you have the Id of the Content Item you can use GetByParentId to return all the ids of the related media for the content item for the umbMedia Relation Type

    Similarly if you have the Id of the Media Item, you can use GetByChildId to find all the relations between that item and its 'parent' content where it is being used.

    If you are trying to avoid using this store of relation information, and want to parse the content yourself? then have a look at how the inbuilt reference tracking works - basically each property editor has a 'GetReferences' method that is called when the page is published, so for the RichTextEditor it looks something like this:

    https://github.com/umbraco/Umbraco-CMS/blob/9bafdbd992534c14d8ee8fad53c700258c15b3f3/src/Umbraco.Infrastructure/PropertyEditors/RichTextPropertyEditor.cs#L224

    and you can see it uses a service called the HtmlImageSourceParser, where you can see the implementation for how images and links are being found in the raw property editor data:

    https://github.com/umbraco/Umbraco-CMS/blob/9bafdbd992534c14d8ee8fad53c700258c15b3f3/src/Umbraco.Core/Templates/HtmlImageSourceParser.cs#L7

    regards

    Marc

  • Darren Hunter 137 posts 229 karma points
    Jun 05, 2023 @ 10:14
    Darren Hunter
    0

    Thank you that work a treat.

Please Sign in or register to post replies

Write your reply to:

Draft