When rendering a hyperlink in my view that will take the user to another content page, I have access to the destination Url from the ‘Link’ object, but what if I want to display some information from the linked content node near my hyperlink?
I’d like to find the linked IPublishedContent while rendering the hyperlink but I don’t see a way to do this unless I use the destination url to loop recursively through the content tree looking for a matching url. I don’t think we have access to a ‘find by path’ method anymore. Is that true?
NOTE: This is for ‘content’ link types that link to other content nodes, not external or manual links.
Hey! Assuming you’re using the Umbraco.Cms.Core.Models.Link model that is used by the URL picker, it should have the following properties on it:
public class Link
{
public string? Name { get; set; }
public string? Target { get; set; }
public LinkType Type { get; set; }
public Udi? Udi { get; set; }
public IPublishedContent? Content { get; set; }
public string? Url { get; set; }
}
Therefore, you should be able to get the content from the Content property
You can do this by either checking if the Content property is null, then using it if it isn’t, or seeing if the LinkType is set to Content - in case you’re not wanting to link to Media.
Ps watch out for unpublished content that has been selected for the Link.. as it will return null..
So there is no way to know if a Link wasn’t selected or a link was selected but subsequently unpublished