I've a multi language site with 3 main roots ( each for a language ) that is it, en, de ( it, en and de are the root name too ).
The en and de are copied from it ( with linked option on ).
You can use the relations service. If you copied the sites with linked option checked then there will be a relation in the db. So in your partial / template you can for current page do:
var relationServce = Services.RelationService;
IEnumerable<Relation> alternate = relationServce.GetByParentId(nodeId);
Happy to share this Macro for creating the hreflang tags. The only requirement is that all pages are copied from the same, original so the parent is the same for everything. So you'd create a new page in the original site and copy it out to another site to maintain the relationship tree:-
@using Umbraco.Core;
@using System.Linq;
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@{
// get a reference to the relation service
var rs = ApplicationContext.Current.Services.RelationService;
// get a list of all relations caused by copying from the current node
var copies = rs.GetByParentId(Model.Content.Id).Where(f => f.RelationType.Alias == "relateDocumentOnCopy").ToList();
// if this is the copied page see, if it is related to an original page
var originals = rs.GetByChildId(Model.Content.Id).Where(f => f.RelationType.Alias == "relateDocumentOnCopy").ToList();
//there should only be one original
var originalRelation = originals.FirstOrDefault();
// If there are no originals but there are copies then this is the original so list out copies
if (originalRelation == null) {
if (copies.Any()) {
foreach (var copyRelation in copies) {
// write out each node copied from this original
var copiedNode = Umbraco.TypedContent(copyRelation.ChildId);
if (copiedNode != null) {
<link rel="alternate" href="@copiedNode.Url" hreflang="@copiedNode.GetCulture().ToString().ToLower()">
}
}
}
<link rel="alternate" href="@Umbraco.TypedContent(Model.Content.Id).Url" hreflang="x-default">
}
// If there is an original, then we are on a copy. List out the copies of it's original except this copy
if (originalRelation != null) {
var theSiblings = rs.GetByParentId(originalRelation.ParentId).Where(f => f.RelationType.Alias == "relateDocumentOnCopy").ToList();
foreach (var copyRelation in theSiblings) {
// write out each node copied from this original
var copiedNode = Umbraco.TypedContent(copyRelation.ChildId);
if (copiedNode != null) {
if (copiedNode.Id != Model.Content.Id) {
<link rel="alternate" href="@copiedNode.Url" hreflang="@copiedNode.GetCulture().ToString().ToLower()">
}
}
}
// List out the original
<link rel="alternate" href="@Umbraco.TypedContent(originalRelation.ParentId).Url" hreflang="@Umbraco.TypedContent(originalRelation.ParentId).GetCulture().ToString().ToLower()">
<link rel="alternate" href="@Umbraco.TypedContent(originalRelation.ParentId).Url" hreflang="x-default">
}
}
Would it be better to create a new relationship type rather than using relateDocumentOnCopy - although it means manually adding all the relations it reduces the problem of inaccurate relations being generated when a page is copied within a language or for testing purposes.
Just wondering what people's thoughts are on this?
From a technical point of view, maybe - but as an editor in umbraco I´d like to have the simplicity of just copying the page and it is automatically linked.
We added a simple interface to umbraco where you can manually add / remove these relations from the GUI. If we can find the time we will add this as a package.
How to build link rel="alternate"?
Hi,
I've a multi language site with 3 main roots ( each for a language ) that is it, en, de ( it, en and de are the root name too ). The en and de are copied from it ( with linked option on ).
So, How to build the alternate link?
Thank you.
Biagio,
You can use the relations service. If you copied the sites with linked option checked then there will be a relation in the db. So in your partial / template you can for current page do:
More information here
https://our.umbraco.org/Documentation/Reference/Management/Services/RelationService
I had to do something very similar in fact its due to go live very soon although its for v6.
Which are the methods of relation object?
Thanks. How to add a new page? Must be added to parent tree parent site and then Umbraco create the copy or not?
Biagio,
I think there is a component in nupickers package that will let you create relations. Or you can as you say create in parent then copy with relate.
Regards
Ismail
Hi Biagio,
Happy to share this Macro for creating the hreflang tags. The only requirement is that all pages are copied from the same, original so the parent is the same for everything. So you'd create a new page in the original site and copy it out to another site to maintain the relationship tree:-
Hope this helps
Craig
I suggest for SEO to change from Url to UrlAbsolute()
Hi,
it works. I create a Partial Views and included into the of the main view. Thanks
Would it be better to create a new relationship type rather than using relateDocumentOnCopy - although it means manually adding all the relations it reduces the problem of inaccurate relations being generated when a page is copied within a language or for testing purposes.
Just wondering what people's thoughts are on this?
From a technical point of view, maybe - but as an editor in umbraco I´d like to have the simplicity of just copying the page and it is automatically linked. We added a simple interface to umbraco where you can manually add / remove these relations from the GUI. If we can find the time we will add this as a package.
Hey! You might want to check this out: https://our.umbraco.org/projects/backoffice-extensions/linked-pages/
I haven't tried it, but it's been in my radar for some time.
is working on a reply...