Copied to clipboard

Flag this post as spam?

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


  • David Gregory 85 posts 211 karma points
    Dec 11, 2015 @ 12:19
    David Gregory
    0

    Link to a page's relationship

    Hello, first look at relationships and I'm not sure if I am understanding it correctly.

    content structure is:

    home - page 1 - page 2

    I copied home and checked Relate to original so now have

    home - page 1 - page 2 home - page 1 - page 2

    All good. Question is, how do I put a link in my master template so that I can link each page with it's partner? I can't seem to find any documentation on this.

    Or have I misunderstood what relationships are?

    Thanks David

  • Ian 178 posts 752 karma points
    Dec 11, 2015 @ 12:55
    Ian
    0

    try this the idea is on copy pages to get the parent relation (the original), I believe the alias of the relation type for this is relateDocumentOnCopy, once you have the relation (an IRelation) you can get the IPublishedContent and get the url & other properties.

    string relationType = "relateDocumentOnCopy";
        int relatedNodeId = CurrentPage.Id;
        IRelationService rs = ApplicationContext.Current.Services.RelationService;
        IEnumerable<IRelation> parentRelations = rs.GetByParentId(relatedNodeId).Where(r => r.RelationType.Alias == relationType);
    

    then you can get ipublishedcontent of the related item by looping throughout the IEnumerable parentRelations setting 'relation' as the iterator

    IPublishedContent relatedItem = Umbraco.TypedContent(relation.ChildId);
    
  • David Gregory 85 posts 211 karma points
    Dec 11, 2015 @ 16:50
    David Gregory
    1

    Thanks Ian

    I have used your code and come up with this:

    int currentPage = CurrentPage.Id;
    var rs = ApplicationContext.Current.Services.RelationService;
    var ids = rs.GetByParentOrChildId(currentPage);
    
    foreach (var item in ids)
    {
        var id = (item.ParentId == currentPage) ? item.ChildId : item.ParentId;
        <li><a href="@Umbraco.NiceUrl(id)" id="cms-cymraeg">@Umbraco.Field("#Language")</a></li>
    }
    

    Does that look ok to you?

  • Ian 178 posts 752 karma points
    Dec 11, 2015 @ 16:53
    Ian
    0

    I see what you're doing, looks ok to me, is it picking everything up OK

  • David Gregory 85 posts 211 karma points
    Dec 11, 2015 @ 16:57
    David Gregory
    0

    yes it does the job. I'm using it for a 1:1 bilingual site. I was using Vorto but that doesn't give bilingual url's although it does give a better user/editor experience.

    All I have to do now is rely on my content editors to copy an English page when the create one and place it in the correct position in the opposite tree.

    Thanks for your help.

  • Ian 178 posts 752 karma points
    Dec 11, 2015 @ 18:59
    Ian
    0

    Glad it does what you need. I haven't used and only briefly seen vorto so i cant speak authorititively but would be surprised to find it couldn't be made to do what you want. When / if you have any time have a look at this to see if it would have helped you on your way https://our.umbraco.org/projects/backoffice-extensions/vorto/bugs-feedback-suggestions/71457-creating-page-urls-with-vorto

  • Laurence Gillian 600 posts 1219 karma points
    Feb 20, 2017 @ 12:08
    Laurence Gillian
    0

    In case it's useful for anyone, here's the above as an extension method that returns IPublishedContent:

    using System.Collections.Generic;
    using Umbraco.Core;
    using Umbraco.Core.Models;
    using Umbraco.Web;
    
    namespace Company.Project.Logic.Extensions
    {
       public static class IPublishedContentExtensions
        {
            /// <summary>Returns any related IPublishedContent</summary>
            public static IEnumerable<IPublishedContent> Relations(this IPublishedContent content)
            {
                var rs = ApplicationContext.Current.Services.RelationService;
                var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
    
                var relations = rs.GetByParentOrChildId(content.Id);
    
                foreach (var rel in relations) {
                    var id = (rel.ParentId == content.Id) ? rel.ChildId : rel.ParentId;
                    yield return umbracoHelper.TypedContent(id);
                }
            }
        }
    }
    

    Used in a View/Partial View like so:

    @using Company.Project.Logic.Extensions
    @model IPublishedContent
    @{
        var relations = Model.Relations();
    }
    
    @foreach (var relation in relations)
    {
        <p><a href="@relation.Url">@relation.Name</a></p>
    }
    

    References:

    http://sinfomania.com/tutorials/umbraco-7x/developer/multi-language-site/
    https://www.kenneth-truyers.net/2016/05/12/yield-return-in-c/

Please Sign in or register to post replies

Write your reply to:

Draft