Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public bool TryGetValue(IPublishedProperty property, string? culture, string? se
/// <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)
{
Expand Down Expand Up @@ -78,7 +78,7 @@ public bool TryGetValue<T>(IPublishedElement content, string alias, string? cult
return false;
}

_variationContextAccessor.ContextualizeVariation(propertyType.Variations, ref culture, ref segment);
_variationContextAccessor.ContextualizeVariation(propertyType.Variations, alias, ref culture, ref segment);

foreach (var f in fallback)
{
Expand Down Expand Up @@ -124,7 +124,7 @@ public virtual bool TryGetValue<T>(IPublishedContent content, string alias, stri
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);
}

Expand Down Expand Up @@ -195,7 +195,7 @@ private bool TryGetValueWithAncestorsFallback<T>(IPublishedContent? content, str
{
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);
Expand Down
12 changes: 9 additions & 3 deletions src/Umbraco.Core/Models/PublishedContent/VariationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,15 @@ public VariationContext(string? culture = null, string? segment = null)
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,71 @@

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)
{
if (culture != null && segment != null)
{
return;
}

// 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);
}

Check warning on line 75 in src/Umbraco.Core/Models/PublishedContent/VariationContextAccessorExtensions.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v13/dev)

❌ New issue: Complex Method

ContextualizeVariation has a cyclomatic complexity of 9, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
}
else
{
Expand Down
10 changes: 5 additions & 5 deletions src/Umbraco.PublishedCache.NuCache/Property.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public Property(Property origin, PublishedContent content)
// 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, PropertyType.Alias, ref culture, ref segment);

var value = GetSourceValue(culture, segment);
var hasValue = PropertyType.IsValue(value, PropertyValueLevel.Source);
Expand Down Expand Up @@ -148,7 +148,7 @@ public override bool HasValue(string? culture = null, string? segment = null)

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, PropertyType.Alias, 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.
Expand Down Expand Up @@ -262,7 +262,7 @@ private CacheValues GetCacheValues(IAppCache? cache)

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, PropertyType.Alias, ref culture, ref segment);

object? value;
CacheValue cacheValues = GetCacheValues(PropertyType.CacheLevel).For(culture, segment);
Expand All @@ -285,7 +285,7 @@ private CacheValues GetCacheValues(IAppCache? cache)
[Obsolete("The current implementation of XPath is suboptimal and will be removed entirely in a future version. Scheduled for removal in v14")]
public override object? GetXPathValue(string? culture = null, string? segment = null)
{
_content.VariationContextAccessor.ContextualizeVariation(_variations, _content.Id, ref culture, ref segment);
_content.VariationContextAccessor.ContextualizeVariation(_variations, _content.Id, PropertyType.Alias, ref culture, ref segment);

CacheValue cacheValues = GetCacheValues(PropertyType.CacheLevel).For(culture, segment);

Expand All @@ -304,7 +304,7 @@ private CacheValues GetCacheValues(IAppCache? cache)

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, PropertyType.Alias, ref culture, ref segment);

object? value;
CacheValue cacheValues = GetCacheValues(expanding ? PropertyType.DeliveryApiCacheLevelForExpansion : PropertyType.DeliveryApiCacheLevel).For(culture, segment);
Expand Down
Loading