Copied to clipboard

Flag this post as spam?

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


  • Dave 8 posts 79 karma points
    Mar 04, 2019 @ 09:05
    Dave
    0

    Siblings() ? Umbraco 8

    I think Umbraco 8 is missing the siblings / sibling method?

    Anyone managed to find it / its replacement?

  • Søren Gregersen 288 posts 1150 karma points MVP c-trib
    Mar 04, 2019 @ 10:05
    Søren Gregersen
    0

    You could use .Parent().Children() as a temporary solution

    I can’t seem to find the impl in either v7 or v8. Should be an Easy addition, if it was not removed on purpose

  • Dave 8 posts 79 karma points
    Mar 04, 2019 @ 10:14
    Dave
    0

    Hey, I thought the same, unfortunately the node i need is a sibling of the root node, so in this instance there is no .parent()

  • Bjarne Fyrstenborg 1136 posts 3218 karma points MVP 3x c-trib
    Mar 04, 2019 @ 11:16
    Bjarne Fyrstenborg
    2

    Hi Dave

    I have created an issue for this https://github.com/umbraco/Umbraco-CMS/issues/4839

    It seems many of these extension methods is not available in v8 and they are in v7.

    /Bjarne

  • Dave 8 posts 79 karma points
    Mar 05, 2019 @ 13:23
    Dave
    0

    Thanks Bjarne, hopefully there'll be an update soon

  • k 226 posts 590 karma points
    30 days ago
    k
    0

    Hello, I am having same issue. I am using Next() but it is not working on Umbraco 8. Has Next() been replaced by something else? Thanks, Kusum

  • Kerri Mallinson 113 posts 497 karma points
    1 week ago
    Kerri Mallinson
    0

    Hi,

    Did you resolve this? I'm also wanting to use .Next() in v8

    Thanks

    Kerri

  • Marc Goodson 916 posts 5949 karma points MVP 4x c-trib
    1 week ago
    Marc Goodson
    1

    Hi Kerri, k, Dave

    V8.1 has seen the return of Siblings() and indeed SiblingsAndSelf()

    Siblings will return the Siblings of the current item (but not the current item) and you guessed it SiblingsAndSelf() will include the current item too.

    Next() and Previous() haven't been added back in however 'you could' create your own extension methods onto IPublishedContent to implement them, and now that we can easily get Siblings, it's quite straightforward. If you add something like the following to your projects:

    using System.Linq;
    using Umbraco.Core.Models.PublishedContent;
    using Umbraco.Web;
    
    namespace Our.Umbraco.V7Pontoon.Extensions
    {
        public static class IPublishedContentExtensions
        {
            public static IPublishedContent Next(this IPublishedContent content)
            {
                IPublishedContent nextPublishedContentItem = default(IPublishedContent);
                IndexedArrayItem<IPublishedContent>[] indexedSiblings = content.SiblingsAndSelf().ToIndexedArray();
                IndexedArrayItem<IPublishedContent> currentContentItem = indexedSiblings.FirstOrDefault(f => f.Content.Id == content.Id);
                if (currentContentItem.IsNotLast())
                {
                    IndexedArrayItem<IPublishedContent> nextItem = indexedSiblings[currentContentItem.Index + 1];
                    nextPublishedContentItem = nextItem.Content;
                }
                return nextPublishedContentItem;
            }
            public static IPublishedContent Previous(this IPublishedContent content)
            {
                IPublishedContent prevPublishedContentItem = default(IPublishedContent);
                IndexedArrayItem<IPublishedContent>[] indexedSiblings = content.SiblingsAndSelf().ToIndexedArray();
                IndexedArrayItem<IPublishedContent> currentContentItem = indexedSiblings.FirstOrDefault(f => f.Content.Id == content.Id);
                if (currentContentItem.IsNotFirst())
                {
                    IndexedArrayItem<IPublishedContent> nextItem = indexedSiblings[currentContentItem.Index -1];
                    prevPublishedContentItem = nextItem.Content;
                }
                return prevPublishedContentItem;
            }          
        }
    }
    

    and then if you add a using reference to this namespace:

    using Our.Umbraco.V7Pontoon.Extensions
    

    Then you'll be able to use the basic form of Next() and Previous() in a similar way to V7:

      @{
            var nextPage = Model.Next();
            var prevPage = Model.Previous();
       }
                <h2>
                    Next: @if (nextPage != null)
            {<a href="@nextPage.Url">@nextPage.Name</a>}
                </h2>
                <h2>
                    Previous: @if (prevPage != null)
            {<a href="@prevPage.Url">@prevPage.Name</a>}
                </h2>
    

    There are lots of other V7 PublishedContentExtensions... and overloads of Next and Previous here:

    https://github.com/umbraco/Umbraco-CMS/blob/v7/dev/src/Umbraco.Web/PublishedContentExtensions.cs

    that could be implemented in the same way if you need them.

    Maybe we could coordinate a central git repository of these helpers to build them up as required as people bridge existing V7 solutions to V8.

    regards

    Marc

  • Bjarne Fyrstenborg 1136 posts 3218 karma points MVP 3x c-trib
    1 week ago
    Bjarne Fyrstenborg
    2

    Hi Kerri

    There was another forum thread here about the missing Previous() and Next() extension methods.

    Here is a simple example of how to solve this.

    https://our.umbraco.com/forum/umbraco-8/97496-previous-next-plus-umvbraco-8#comment-308874

    var newsList = Model.Parent.Children("alias");
    var currentIndexInNewsList = newsList.FindIndex(x => x.Id == Model.Id);
    var totalResults = newsList.Count();
    

    Then use the following to get previousItem and nextItem:

    var previousItem = newsList.ElementAtOrDefault(currentIndexInNewsList - 1);
    var nextItem = newsList.ElementAtOrDefault(currentIndexInNewsList + 1);
    

    Instead of Children() you could also use SiblingsAndSelf() or Siblings() extension methods, but when getting previous or next node, you probably don't need the included the node itself, so it will probably work with both of these extension method.

    In Umbraco v7 for Previous() extension method it returned the parent node, when the current node was first child.

    So for a structure like the following and if current step was "Billing" I think Previous() extension method in v7 returned the "Basket" node.

    • Basket

      • Billing
      • Shipment
      • Payment
      • Confirmation

      /Bjarne

  • Kerri Mallinson 113 posts 497 karma points
    6 days ago
    Kerri Mallinson
    1

    Thanks for the replies Bjarne and Marc,

    I ended up using Bjarne's solution.

Please Sign in or register to post replies

Write your reply to:

Draft