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.
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.
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.
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>
}
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
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.
then you can get ipublishedcontent of the related item by looping throughout the IEnumerable parentRelations setting 'relation' as the iterator
Thanks Ian
I have used your code and come up with this:
Does that look ok to you?
I see what you're doing, looks ok to me, is it picking everything up OK
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.
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
In case it's useful for anyone, here's the above as an extension method that returns IPublishedContent:
Used in a View/Partial View like so:
References:
http://sinfomania.com/tutorials/umbraco-7x/developer/multi-language-site/
https://www.kenneth-truyers.net/2016/05/12/yield-return-in-c/
is working on a reply...