ZimmertyZim
08/30/2023, 10:23 AMpublic static class IPublishedContentExtensions
{
public static string TitleOrPageName(this IPublishedContent content, string titleAlias = "navigationLabel")
{
return content.HasValue(titleAlias) ? content.GetPropertyValue<string>(titleAlias) : content.Name;
}
public static int GetYearFromDateOrLatest(this IPublishedContent content, string fieldAlias)
{
return content.HasValue(fieldAlias) ? content.GetPropertyValue<DateTime>(fieldAlias).Year : DateTime.Now.Year;
}
}
I obviously need to do something like this for V10:
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Extensions;
namespace MyApplication.Web.Extensions;
public static string TitleOrPageName(this IPublishedContent content, string titleAlias = "navigationLabel")
{
return content.HasValue(alias: titleAlias) ? content.Value<string>(_publishedValueFallback, titleAlias) : content.Name;
}
As this is a static class I cannot inject _publishedValueFallback. I am aware Umbraco.Extensions should have a method that doesn't need the interface but it doens't seem to be available here.
What way should I be doing this?Rachel D
08/30/2023, 10:32 AMcsharp
private static IPublishedValueFallback PublishedValueFallback { get; } =
StaticServiceProvider.Instance.GetRequiredService<IPublishedValueFallback>();
I'm not a fan of that personally, but it should work.ZimmertyZim
08/30/2023, 10:35 AMRachel D
08/30/2023, 10:35 AMZimmertyZim
08/30/2023, 10:38 AMkdx-perbol
08/30/2023, 12:01 PMZimmertyZim
08/30/2023, 12:20 PM