Skip to content

Commit af87426

Browse files
authored
Umbraco Engage UmbracoUrlAlias Fix - Fixes #19654 (#19827) (#19850)
* 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
1 parent 4bf2fbf commit af87426

File tree

4 files changed

+55
-22
lines changed

4 files changed

+55
-22
lines changed

src/Umbraco.Core/Models/PublishedContent/PublishedValueFallback.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Microsoft.Extensions.DependencyInjection;
22
using Umbraco.Cms.Core.DependencyInjection;
3-
using Umbraco.Cms.Core.PublishedCache;
43
using Umbraco.Cms.Core.Services;
54
using Umbraco.Cms.Core.Services.Navigation;
65
using Umbraco.Extensions;
@@ -31,7 +30,7 @@ public bool TryGetValue(IPublishedProperty property, string? culture, string? se
3130
/// <inheritdoc />
3231
public bool TryGetValue<T>(IPublishedProperty property, string? culture, string? segment, Fallback fallback, T? defaultValue, out T? value)
3332
{
34-
_variationContextAccessor.ContextualizeVariation(property.PropertyType.Variations, ref culture, ref segment);
33+
_variationContextAccessor.ContextualizeVariation(property.PropertyType.Variations, property.Alias, ref culture, ref segment);
3534

3635
foreach (var f in fallback)
3736
{
@@ -79,7 +78,7 @@ public bool TryGetValue<T>(IPublishedElement content, string alias, string? cult
7978
return false;
8079
}
8180

82-
_variationContextAccessor.ContextualizeVariation(propertyType.Variations, ref culture, ref segment);
81+
_variationContextAccessor.ContextualizeVariation(propertyType.Variations, alias, ref culture, ref segment);
8382

8483
foreach (var f in fallback)
8584
{
@@ -125,7 +124,7 @@ public virtual bool TryGetValue<T>(IPublishedContent content, string alias, stri
125124
IPublishedPropertyType? propertyType = content.ContentType.GetPropertyType(alias);
126125
if (propertyType != null)
127126
{
128-
_variationContextAccessor.ContextualizeVariation(propertyType.Variations, content.Id, ref culture, ref segment);
127+
_variationContextAccessor.ContextualizeVariation(propertyType.Variations, content.Id, alias, ref culture, ref segment);
129128
noValueProperty = content.GetProperty(alias);
130129
}
131130

@@ -196,7 +195,7 @@ private bool TryGetValueWithAncestorsFallback<T>(IPublishedContent? content, str
196195
{
197196
culture = null;
198197
segment = null;
199-
_variationContextAccessor.ContextualizeVariation(propertyType.Variations, content.Id, ref culture, ref segment);
198+
_variationContextAccessor.ContextualizeVariation(propertyType.Variations, content.Id, alias, ref culture, ref segment);
200199
}
201200

202201
property = content?.GetProperty(alias);

src/Umbraco.Core/Models/PublishedContent/VariationContext.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,15 @@ public VariationContext(string? culture = null, string? segment = null)
2525
public string Segment { get; }
2626

2727
/// <summary>
28-
/// Gets the segment for the content item
28+
/// Gets the segment for the content item.
2929
/// </summary>
30-
/// <param name="contentId"></param>
31-
/// <returns></returns>
30+
/// <param name="contentId">The content Id.</param>
3231
public virtual string GetSegment(int contentId) => Segment;
32+
33+
/// <summary>
34+
/// Gets the segment for the content item and property alias.
35+
/// </summary>
36+
/// <param name="contentId">The content Id.</param>
37+
/// <param name="propertyAlias">The property alias.</param>
38+
public virtual string GetSegment(int contentId, string propertyAlias) => Segment;
3339
}

src/Umbraco.Core/Models/PublishedContent/VariationContextAccessorExtensions.cs

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,45 @@ namespace Umbraco.Extensions;
88

99
public static class VariationContextAccessorExtensions
1010
{
11+
[Obsolete("Please use the method overload that accepts all parameters. Scheduled for removal in Umbraco 18.")]
1112
public static void ContextualizeVariation(
1213
this IVariationContextAccessor variationContextAccessor,
1314
ContentVariation variations,
1415
ref string? culture,
1516
ref string? segment)
16-
=> variationContextAccessor.ContextualizeVariation(variations, null, ref culture, ref segment);
17+
=> variationContextAccessor.ContextualizeVariation(variations, null, null, ref culture, ref segment);
1718

19+
public static void ContextualizeVariation(
20+
this IVariationContextAccessor variationContextAccessor,
21+
ContentVariation variations,
22+
string? propertyAlias,
23+
ref string? culture,
24+
ref string? segment)
25+
=> variationContextAccessor.ContextualizeVariation(variations, null, propertyAlias, ref culture, ref segment);
26+
27+
[Obsolete("Please use the method overload that accepts all parameters. Scheduled for removal in Umbraco 18.")]
1828
public static void ContextualizeVariation(
1929
this IVariationContextAccessor variationContextAccessor,
2030
ContentVariation variations,
2131
int contentId,
2232
ref string? culture,
2333
ref string? segment)
24-
=> variationContextAccessor.ContextualizeVariation(variations, (int?)contentId, ref culture, ref segment);
34+
=> variationContextAccessor.ContextualizeVariation(variations, (int?)contentId, null, ref culture, ref segment);
35+
36+
public static void ContextualizeVariation(
37+
this IVariationContextAccessor variationContextAccessor,
38+
ContentVariation variations,
39+
int contentId,
40+
string? propertyAlias,
41+
ref string? culture,
42+
ref string? segment)
43+
=> variationContextAccessor.ContextualizeVariation(variations, (int?)contentId, propertyAlias, ref culture, ref segment);
2544

2645
private static void ContextualizeVariation(
2746
this IVariationContextAccessor variationContextAccessor,
2847
ContentVariation variations,
2948
int? contentId,
49+
string? propertyAlias,
3050
ref string? culture,
3151
ref string? segment)
3252
{
@@ -37,18 +57,22 @@ private static void ContextualizeVariation(
3757

3858
// use context values
3959
VariationContext? publishedVariationContext = variationContextAccessor?.VariationContext;
40-
if (culture == null)
41-
{
42-
culture = variations.VariesByCulture() ? publishedVariationContext?.Culture : string.Empty;
43-
}
60+
culture ??= variations.VariesByCulture() ? publishedVariationContext?.Culture : string.Empty;
4461

4562
if (segment == null)
4663
{
4764
if (variations.VariesBySegment())
4865
{
49-
segment = contentId == null
50-
? publishedVariationContext?.Segment
51-
: publishedVariationContext?.GetSegment(contentId.Value);
66+
if (contentId == null)
67+
{
68+
segment = publishedVariationContext?.Segment;
69+
}
70+
else
71+
{
72+
segment = propertyAlias == null ?
73+
publishedVariationContext?.GetSegment(contentId.Value) :
74+
publishedVariationContext?.GetSegment(contentId.Value, propertyAlias);
75+
}
5276
}
5377
else
5478
{

src/Umbraco.PublishedCache.HybridCache/PublishedProperty.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections.Concurrent;
1+
using System.Collections.Concurrent;
22
using Umbraco.Cms.Core.Cache;
33
using Umbraco.Cms.Core.Collections;
44
using Umbraco.Cms.Core.Models;
@@ -21,6 +21,8 @@ internal sealed class PublishedProperty : PublishedPropertyBase
2121
private readonly ContentVariation _variations;
2222
private readonly ContentVariation _sourceVariations;
2323

24+
private readonly string _propertyTypeAlias;
25+
2426
// the variant and non-variant object values
2527
private bool _interInitialized;
2628
private object? _interValue;
@@ -71,6 +73,8 @@ public PublishedProperty(
7173
// it must be set to the union of variance (the combination of content type and property type variance).
7274
_variations = propertyType.Variations | content.ContentType.Variations;
7375
_sourceVariations = propertyType.Variations;
76+
77+
_propertyTypeAlias = propertyType.Alias;
7478
}
7579

7680
// used to cache the CacheValues of this property
@@ -89,7 +93,7 @@ private static string PropertyCacheValues(Guid contentUid, string typeAlias, boo
8993
// determines whether a property has value
9094
public override bool HasValue(string? culture = null, string? segment = null)
9195
{
92-
_content.VariationContextAccessor.ContextualizeVariation(_variations, _content.Id, ref culture, ref segment);
96+
_content.VariationContextAccessor.ContextualizeVariation(_variations, _content.Id, _propertyTypeAlias, ref culture, ref segment);
9397

9498
var value = GetSourceValue(culture, segment);
9599
var hasValue = PropertyType.IsValue(value, PropertyValueLevel.Source);
@@ -103,7 +107,7 @@ public override bool HasValue(string? culture = null, string? segment = null)
103107

104108
public override object? GetSourceValue(string? culture = null, string? segment = null)
105109
{
106-
_content.VariationContextAccessor.ContextualizeVariation(_sourceVariations, _content.Id, ref culture, ref segment);
110+
_content.VariationContextAccessor.ContextualizeVariation(_sourceVariations, _content.Id, _propertyTypeAlias, ref culture, ref segment);
107111

108112
// source values are tightly bound to the property/schema culture and segment configurations, so we need to
109113
// sanitize the contextualized culture/segment states before using them to access the source values.
@@ -146,7 +150,7 @@ public override bool HasValue(string? culture = null, string? segment = null)
146150

147151
public override object? GetValue(string? culture = null, string? segment = null)
148152
{
149-
_content.VariationContextAccessor.ContextualizeVariation(_variations, _content.Id, ref culture, ref segment);
153+
_content.VariationContextAccessor.ContextualizeVariation(_variations, _content.Id, _propertyTypeAlias, ref culture, ref segment);
150154

151155
object? value;
152156
CacheValue cacheValues = GetCacheValues(PropertyType.CacheLevel).For(culture, segment);
@@ -209,7 +213,7 @@ private CacheValues GetCacheValues(IAppCache? cache)
209213

210214
public override object? GetDeliveryApiValue(bool expanding, string? culture = null, string? segment = null)
211215
{
212-
_content.VariationContextAccessor.ContextualizeVariation(_variations, _content.Id, ref culture, ref segment);
216+
_content.VariationContextAccessor.ContextualizeVariation(_variations, _content.Id, _propertyTypeAlias, ref culture, ref segment);
213217

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

0 commit comments

Comments
 (0)