Read my blog here
Created
April 29, 2022 10:27
-
-
Save jstemerdink/f41186e59a142c4f4321411c68501337 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void GetProductUrl(ProductContent product, ContentReference catalogLink, out string path) | |
{ | |
path = product.RouteSegment; | |
ContentReference parentLink = product.ParentLink; | |
if (catalogLink.CompareToIgnoreWorkID(contentReference: parentLink)) | |
{ | |
return; | |
} | |
ContentReference contentLink = product.ContentLink; | |
int objectId = this.referenceConverter.GetObjectId(contentLink: catalogLink); | |
NodeContent content; | |
while (this.contentLoader.TryGet(contentLink: parentLink, content: out content)) | |
{ | |
if (content.CatalogId != objectId) | |
{ | |
NodeContent linkedParentNode = this.GetLinkedParentNode( | |
contentLink: contentLink, | |
catalogLink: catalogLink); | |
if (linkedParentNode == null) | |
{ | |
return; | |
} | |
content = linkedParentNode; | |
contentLink = linkedParentNode.ContentLink; | |
} | |
path = content.RouteSegment + "/" + path; | |
parentLink = content.ParentLink; | |
if (this.referenceConverter.GetContentType(contentLink: parentLink) == CatalogContentType.Catalog) | |
{ | |
break; | |
} | |
} | |
} | |
private NodeContent GetLinkedParentNode(ContentReference contentLink, ContentReference catalogLink) | |
{ | |
List<ContentReference> source = this.relationRepository.GetParents<NodeRelation>(childLink: contentLink) | |
.Where(r => r.TargetCatalog.CompareToIgnoreWorkID(contentReference: catalogLink)).Select(r => r.Parent) | |
.ToList(); | |
if (!source.Any()) | |
{ | |
return null; | |
} | |
LoaderOptions settings = | |
new LoaderOptions { new LanguageLoaderOption { Language = ContentLanguage.PreferredCulture } }; | |
return this.contentLoader.GetItems(contentLinks: source, settings: settings).OfType<NodeContent>() | |
.FirstOrDefault(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment