Copied to clipboard

Flag this post as spam?

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


  • Daniel Gillett 72 posts 149 karma points
    Feb 08, 2020 @ 11:08
    Daniel Gillett
    0

    Get the Nested Content Element Parent Ids

    Hello,

    This was one sent me a journey. I have a nested element that lists and describes a pieces of artwork that I selected from different galleries. I used the media picker.

    Gallery with artwork items

    I needed to figure out which gallery each piece of artwork came from because with nested elements you can't get the source gallery id. ...a nested element is not connected/rooted to anything.

    My solution was to to iterate over each gallery to collect each of the artwork items and add the nodeId along side of each gallery, but you cannot create a List like -> Object, Int

    Tuple to the rescue! I created a tuple list class in my Helpers library to take the GalleryItems list and the Id of each gallery.

    public class TupleList<T1, T2> : List<Tuple<T1, T2>>
    {
        public void Add(T1 item, T2 item2)
        {
            Add(new Tuple<T1, T2>(item, item2));
        }
    }
    

    This gave me a list of lists and the nodeId of each gallery:

    • Water Color, 1181
    • Acrylic, 1179

    The next thing was to flatten the lists of GalleryItems into one list:

    • flowers, 1181
    • tulips, 1181
    • Woods, 1780
    • Sunrise, 1780
    • etc...

    I was originally using the list's AddRange but you can't use it with Tuple so I had to write a few foreach loops to acheive this.

            var galleryItems = new TupleList<IEnumerable<IPublishedElement>, int>();
            foreach (var gallery in galleryList){
                galleryItems.Add(gallery.Value<IEnumerable<IPublishedElement>>("galleryItems"), gallery.Id);
            }
    
            var flattenedList= new TupleList<GalleryItem, int>();
            foreach (var gi in galleryItems){
                foreach (GalleryItem i in gi.Item1){
                    flattenedList.Add(i, gi.Item2);
                }
            }
    

    From here I was able to sort the list by the GalleryItem createdDate property and take the most recent to be displayed, paged, etc.

    var recentProjects = flattenedList.OrderByDescending(d => d.Item1.CreatedDate).Take(3);
    

    Finally, I could add it to the HTML. Not all GalleyItems have their own page. Where gallery items have a page link, i used that. If there is no page link for a gallery item, I was able to use the gallery id to create the url for the gallery it belongs to with the Umbraco.Content(galleryId) ...

    @if (linkToPage != null){
                    <a href="@linkToPage.Url" class="i" style="padding-left: 10px; padding-right: 10px;">More about @(x.Item1.ImageTitle)...</a>
                    }
                    else{
                    IPublishedContent nodeId = Umbraco.Content(x.Item2);
                    <a href="@(nodeId.Url)" class="i" style="padding-left: 10px; padding-right: 10px;">View my @nodeId.Name gallery</a>
                    }
    

    Job done! Hope this helps and that it saves you the hours I spent figuring it out. ;-)

    Best, Daniel

Please Sign in or register to post replies

Write your reply to:

Draft