* Fixes #19654 Adds the propertyAlias to the VariationContext so that products implementing the GetSegment method are aware which propertyAlias it's being called for * Re-implement original variation context for backwards compatibility * Fixes hidden overload method Ensures the `GetSegment` method overload is not hidden when a null `propertyAlias` is passed. * Resolve backward compatibility issues. * Improved comments. --------- # Conflicts: # src/Umbraco.PublishedCache.NuCache/Property.cs
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Umbraco.Cms.Core.DependencyInjection;
|
||||
using Umbraco.Cms.Core.PublishedCache;
|
||||
using Umbraco.Cms.Core.Services;
|
||||
using Umbraco.Cms.Core.Services.Navigation;
|
||||
using Umbraco.Extensions;
|
||||
@@ -31,7 +30,7 @@ public class PublishedValueFallback : IPublishedValueFallback
|
||||
/// <inheritdoc />
|
||||
public bool TryGetValue<T>(IPublishedProperty property, string? culture, string? segment, Fallback fallback, T? defaultValue, out T? value)
|
||||
{
|
||||
_variationContextAccessor.ContextualizeVariation(property.PropertyType.Variations, ref culture, ref segment);
|
||||
_variationContextAccessor.ContextualizeVariation(property.PropertyType.Variations, property.Alias, ref culture, ref segment);
|
||||
|
||||
foreach (var f in fallback)
|
||||
{
|
||||
@@ -79,7 +78,7 @@ public class PublishedValueFallback : IPublishedValueFallback
|
||||
return false;
|
||||
}
|
||||
|
||||
_variationContextAccessor.ContextualizeVariation(propertyType.Variations, ref culture, ref segment);
|
||||
_variationContextAccessor.ContextualizeVariation(propertyType.Variations, alias, ref culture, ref segment);
|
||||
|
||||
foreach (var f in fallback)
|
||||
{
|
||||
@@ -125,7 +124,7 @@ public class PublishedValueFallback : IPublishedValueFallback
|
||||
IPublishedPropertyType? propertyType = content.ContentType.GetPropertyType(alias);
|
||||
if (propertyType != null)
|
||||
{
|
||||
_variationContextAccessor.ContextualizeVariation(propertyType.Variations, content.Id, ref culture, ref segment);
|
||||
_variationContextAccessor.ContextualizeVariation(propertyType.Variations, content.Id, alias, ref culture, ref segment);
|
||||
noValueProperty = content.GetProperty(alias);
|
||||
}
|
||||
|
||||
@@ -196,7 +195,7 @@ public class PublishedValueFallback : IPublishedValueFallback
|
||||
{
|
||||
culture = null;
|
||||
segment = null;
|
||||
_variationContextAccessor.ContextualizeVariation(propertyType.Variations, content.Id, ref culture, ref segment);
|
||||
_variationContextAccessor.ContextualizeVariation(propertyType.Variations, content.Id, alias, ref culture, ref segment);
|
||||
}
|
||||
|
||||
property = content?.GetProperty(alias);
|
||||
|
||||
@@ -25,9 +25,15 @@ public class VariationContext
|
||||
public string Segment { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the segment for the content item
|
||||
/// Gets the segment for the content item.
|
||||
/// </summary>
|
||||
/// <param name="contentId"></param>
|
||||
/// <returns></returns>
|
||||
/// <param name="contentId">The content Id.</param>
|
||||
public virtual string GetSegment(int contentId) => Segment;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the segment for the content item and property alias.
|
||||
/// </summary>
|
||||
/// <param name="contentId">The content Id.</param>
|
||||
/// <param name="propertyAlias">The property alias.</param>
|
||||
public virtual string GetSegment(int contentId, string propertyAlias) => Segment;
|
||||
}
|
||||
|
||||
@@ -8,25 +8,45 @@ namespace Umbraco.Extensions;
|
||||
|
||||
public static class VariationContextAccessorExtensions
|
||||
{
|
||||
[Obsolete("Please use the method overload that accepts all parameters. Scheduled for removal in Umbraco 18.")]
|
||||
public static void ContextualizeVariation(
|
||||
this IVariationContextAccessor variationContextAccessor,
|
||||
ContentVariation variations,
|
||||
ref string? culture,
|
||||
ref string? segment)
|
||||
=> variationContextAccessor.ContextualizeVariation(variations, null, ref culture, ref segment);
|
||||
=> variationContextAccessor.ContextualizeVariation(variations, null, null, ref culture, ref segment);
|
||||
|
||||
public static void ContextualizeVariation(
|
||||
this IVariationContextAccessor variationContextAccessor,
|
||||
ContentVariation variations,
|
||||
string? propertyAlias,
|
||||
ref string? culture,
|
||||
ref string? segment)
|
||||
=> variationContextAccessor.ContextualizeVariation(variations, null, propertyAlias, ref culture, ref segment);
|
||||
|
||||
[Obsolete("Please use the method overload that accepts all parameters. Scheduled for removal in Umbraco 18.")]
|
||||
public static void ContextualizeVariation(
|
||||
this IVariationContextAccessor variationContextAccessor,
|
||||
ContentVariation variations,
|
||||
int contentId,
|
||||
ref string? culture,
|
||||
ref string? segment)
|
||||
=> variationContextAccessor.ContextualizeVariation(variations, (int?)contentId, ref culture, ref segment);
|
||||
=> variationContextAccessor.ContextualizeVariation(variations, (int?)contentId, null, ref culture, ref segment);
|
||||
|
||||
public static void ContextualizeVariation(
|
||||
this IVariationContextAccessor variationContextAccessor,
|
||||
ContentVariation variations,
|
||||
int contentId,
|
||||
string? propertyAlias,
|
||||
ref string? culture,
|
||||
ref string? segment)
|
||||
=> variationContextAccessor.ContextualizeVariation(variations, (int?)contentId, propertyAlias, ref culture, ref segment);
|
||||
|
||||
private static void ContextualizeVariation(
|
||||
this IVariationContextAccessor variationContextAccessor,
|
||||
ContentVariation variations,
|
||||
int? contentId,
|
||||
string? propertyAlias,
|
||||
ref string? culture,
|
||||
ref string? segment)
|
||||
{
|
||||
@@ -37,18 +57,22 @@ public static class VariationContextAccessorExtensions
|
||||
|
||||
// use context values
|
||||
VariationContext? publishedVariationContext = variationContextAccessor?.VariationContext;
|
||||
if (culture == null)
|
||||
{
|
||||
culture = variations.VariesByCulture() ? publishedVariationContext?.Culture : string.Empty;
|
||||
}
|
||||
culture ??= variations.VariesByCulture() ? publishedVariationContext?.Culture : string.Empty;
|
||||
|
||||
if (segment == null)
|
||||
{
|
||||
if (variations.VariesBySegment())
|
||||
{
|
||||
segment = contentId == null
|
||||
? publishedVariationContext?.Segment
|
||||
: publishedVariationContext?.GetSegment(contentId.Value);
|
||||
if (contentId == null)
|
||||
{
|
||||
segment = publishedVariationContext?.Segment;
|
||||
}
|
||||
else
|
||||
{
|
||||
segment = propertyAlias == null ?
|
||||
publishedVariationContext?.GetSegment(contentId.Value) :
|
||||
publishedVariationContext?.GetSegment(contentId.Value, propertyAlias);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Concurrent;
|
||||
using Umbraco.Cms.Core.Cache;
|
||||
using Umbraco.Cms.Core.Collections;
|
||||
using Umbraco.Cms.Core.Models;
|
||||
@@ -21,6 +21,8 @@ internal sealed class PublishedProperty : PublishedPropertyBase
|
||||
private readonly ContentVariation _variations;
|
||||
private readonly ContentVariation _sourceVariations;
|
||||
|
||||
private readonly string _propertyTypeAlias;
|
||||
|
||||
// the variant and non-variant object values
|
||||
private bool _interInitialized;
|
||||
private object? _interValue;
|
||||
@@ -71,6 +73,8 @@ internal sealed class PublishedProperty : PublishedPropertyBase
|
||||
// it must be set to the union of variance (the combination of content type and property type variance).
|
||||
_variations = propertyType.Variations | content.ContentType.Variations;
|
||||
_sourceVariations = propertyType.Variations;
|
||||
|
||||
_propertyTypeAlias = propertyType.Alias;
|
||||
}
|
||||
|
||||
// used to cache the CacheValues of this property
|
||||
@@ -89,7 +93,7 @@ internal sealed class PublishedProperty : PublishedPropertyBase
|
||||
// determines whether a property has value
|
||||
public override bool HasValue(string? culture = null, string? segment = null)
|
||||
{
|
||||
_content.VariationContextAccessor.ContextualizeVariation(_variations, _content.Id, ref culture, ref segment);
|
||||
_content.VariationContextAccessor.ContextualizeVariation(_variations, _content.Id, _propertyTypeAlias, ref culture, ref segment);
|
||||
|
||||
var value = GetSourceValue(culture, segment);
|
||||
var hasValue = PropertyType.IsValue(value, PropertyValueLevel.Source);
|
||||
@@ -103,7 +107,7 @@ internal sealed class PublishedProperty : PublishedPropertyBase
|
||||
|
||||
public override object? GetSourceValue(string? culture = null, string? segment = null)
|
||||
{
|
||||
_content.VariationContextAccessor.ContextualizeVariation(_sourceVariations, _content.Id, ref culture, ref segment);
|
||||
_content.VariationContextAccessor.ContextualizeVariation(_sourceVariations, _content.Id, _propertyTypeAlias, ref culture, ref segment);
|
||||
|
||||
// source values are tightly bound to the property/schema culture and segment configurations, so we need to
|
||||
// sanitize the contextualized culture/segment states before using them to access the source values.
|
||||
@@ -146,7 +150,7 @@ internal sealed class PublishedProperty : PublishedPropertyBase
|
||||
|
||||
public override object? GetValue(string? culture = null, string? segment = null)
|
||||
{
|
||||
_content.VariationContextAccessor.ContextualizeVariation(_variations, _content.Id, ref culture, ref segment);
|
||||
_content.VariationContextAccessor.ContextualizeVariation(_variations, _content.Id, _propertyTypeAlias, ref culture, ref segment);
|
||||
|
||||
object? value;
|
||||
CacheValue cacheValues = GetCacheValues(PropertyType.CacheLevel).For(culture, segment);
|
||||
@@ -209,7 +213,7 @@ internal sealed class PublishedProperty : PublishedPropertyBase
|
||||
|
||||
public override object? GetDeliveryApiValue(bool expanding, string? culture = null, string? segment = null)
|
||||
{
|
||||
_content.VariationContextAccessor.ContextualizeVariation(_variations, _content.Id, ref culture, ref segment);
|
||||
_content.VariationContextAccessor.ContextualizeVariation(_variations, _content.Id, _propertyTypeAlias, ref culture, ref segment);
|
||||
|
||||
object? value;
|
||||
CacheValue cacheValues = GetCacheValues(expanding ? PropertyType.DeliveryApiCacheLevelForExpansion : PropertyType.DeliveryApiCacheLevel).For(culture, segment);
|
||||
|
||||
Reference in New Issue
Block a user