-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IPublishedContent extension method AncestorOrSelf(string contentTypeAlias) is inconsistent with the other method overloads #14345
Comments
Hi there @iliyan-kulishev! Firstly, a big thank you for raising this issue. Every piece of feedback we receive helps us to make Umbraco better. We really appreciate your patience while we wait for our team to have a look at this but we wanted to let you know that we see this and share with you the plan for what comes next.
We wish we could work with everyone directly and assess your issue immediately but we're in the fortunate position of having lots of contributions to work with and only a few humans who are able to do it. We are making progress though and in the meantime, we will keep you in the loop and let you know when we have any questions. Thanks, from your friendly Umbraco GitHub bot 🤖 🙂 |
Hey @iliyan-kulishev I'm sorry you're having issues, however, as I see it the method is perfectly consistent. It's called |
I think I've just struck this same issue in Umbraco 13.x (13.2.2 and 13.3.0 to be exact) expected: AncestorOrSelf("myContentAlias") returns an Ipc of that content type, otherwise null from "Umbraco.Extensions.PublishedContentExtensions" https://docs.umbraco.com/umbraco-cms/reference/templating/mvc/querying |
I agree with the previous comments that this is inconsistent and a breaking change from earlier versions I would expect AncestorOrSelf("myContentAlias") should return null if there are no nodes of the specified type |
Hey y'all good points about the earlier versions, I looked into this a bit again and found that the change was introduced in this PR: #12703, where similar issues were stated. Now this put us in a bit of a predicament since this issue has been there since V11, this means that it's very likely that a good amount of people have already migrated to the new behaviour or made new solutions relying on the current behaviour, and it would be breaking for them to unbreak this now. Given this I think a discussion with the team is warranted as to the best way to proceed, so I'll bring this up with the team, and get back to you 😄 |
Heya, we just had a discussion about this in the team. We concluded that we're not changing the behaviour of the With both of these in mind we've opted to leave the behaviour as is, I have made this snippet though, where you can add your own extension with your expected behaviour, thus working around the issue. using Umbraco.Cms.Core.Models.PublishedContent;
namespace Umbraco.Extensions;
public static class CustomPublishedContentExtensions
{
/// <summary>
/// Gets the content or its nearest ancestor, of a specified content type.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="contentTypeAlias">The content type.</param>
/// <returns>The content or its nearest (in down-top order) ancestor, of the specified content type.</returns>
/// <remarks>May or may not return the content itself depending on its content type. May return <c>null</c>.</remarks>
public static IPublishedContent? NullableAncestorOrSelf(this IPublishedContent content, string contentTypeAlias) => content
.EnumerateAncestors(true).FirstOrDefault(x => x.ContentType.Alias.InvariantEquals(contentTypeAlias));
private static IEnumerable<IPublishedContent> EnumerateAncestors(this IPublishedContent content, bool orSelf)
{
if (orSelf)
{
yield return content;
}
while ((content = content.Parent) != null)
{
yield return content;
}
}
} |
thanks for update and workaround sample @nikolajlauridsen 😊 |
Which Umbraco version are you using? (Please write the exact version, example: 10.1.0)
11.4.0
Bug summary
The method returns the published model itself when no ancestors type alias match the input, even if its alias doesn't match the input.
Specifics
No response
Steps to reproduce
Expected result / actual result
Expected: the result should be null.
Actual result: the result is the value of the content itself.
The text was updated successfully, but these errors were encountered: