Skip to content

Commit aa10d2b

Browse files
authored
Merge pull request #7337 from Luuk1983/feature/117395875_value_converter_update_code_samples
Update PropertyValueConverter examples to Umbraco 15+
2 parents f319b80 + e797fab commit aa10d2b

File tree

6 files changed

+342
-263
lines changed

6 files changed

+342
-263
lines changed

15/umbraco-cms/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,10 @@
217217
* [Property Editor Schema](customizing/property-editors/composition/property-editor-schema.md)
218218
* [Property Editor UI](customizing/property-editors/composition/property-editor-ui.md)
219219
* [Property Value Converters](customizing/property-editors/property-value-converters.md)
220+
* [Property Value Converter Example](customizing/property-editors/full-examples-value-converters.md)
220221
* [Property Actions](customizing/property-editors/property-actions.md)
221222
* [Integrate Property Editors](customizing/property-editors/integrate-property-editors.md)
222223
* [Tracking References](customizing/property-editors/tracking.md)
223-
* [Content Picker Value Converter Example](customizing/property-editors/full-examples-value-converters.md)
224224
* [Property Dataset](customizing/property-editors/property-dataset.md)
225225
* [Workspaces](customizing/workspaces.md)
226226
* [Umbraco Package](customizing/umbraco-package.md)
Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Content Picker Value Converter Example
1+
# Property Value Converter full example
2+
This page includes an example of a complete Property Value Converter. The example is that of a Property Value Converter for a Content Picker, where the user picks a node from the Umbraco tree.
23

3-
{% include "../../.gitbook/includes/obsolete-warning-snapshot-publishedcache.md" %}
44

55
{% code title="ContentPickerPropertyConverter.cs" %}
66

@@ -12,44 +12,56 @@ using Umbraco.Cms.Core.PublishedCache;
1212

1313
namespace UmbracoDocs.Samples;
1414

15-
public class ContentPickerPropertyConverter : IPropertyValueConverter
15+
// Injecting the IPublishedContentCache for fetching content from the Umbraco cache
16+
public class ContentPickerPropertyConverter(IPublishedContentCache publishedContentCache) : IPropertyValueConverter
1617
{
17-
private readonly IPublishedSnapshotAccessor _publishedSnapshotAccessor;
18-
19-
// Injecting the PublishedSnapshotAccessor for fetching content
20-
public ContentPickerPropertyConverter(IPublishedSnapshotAccessor publishedSnapshotAccessor)
21-
=> _publishedSnapshotAccessor = publishedSnapshotAccessor;
22-
18+
// Make sure the Property Value Converter only applies to the Content Picker property editor
2319
public bool IsConverter(IPublishedPropertyType propertyType)
24-
=> propertyType.EditorAlias.Equals("Umbraco.ContentPicker");
20+
=> propertyType.EditorAlias.Equals(Constants.PropertyEditors.Aliases.ContentPicker);
2521

22+
23+
// We consider the value to be a value only when we have the actual IPublishedContent object,
24+
// meaning that there is a valid picked content item.
2625
public bool? IsValue(object? value, PropertyValueLevel level)
2726
{
2827
return level switch
2928
{
30-
PropertyValueLevel.Source => value is string stringValue && string.IsNullOrWhiteSpace(stringValue) is false,
31-
_ => throw new NotSupportedException($"Invalid level: {level}.")
29+
PropertyValueLevel.Source => null,
30+
PropertyValueLevel.Inter => null,
31+
PropertyValueLevel.Object => value is IPublishedContent,
32+
_ => throw new ArgumentOutOfRangeException(nameof(level), level, $"Invalid level: {level}.")
3233
};
3334
}
3435

36+
// The type returned by this converter is IPublishedContent
37+
// And the Models Builder will take care of returning the actual generated type
3538
public Type GetPropertyValueType(IPublishedPropertyType propertyType)
3639
=> typeof(IPublishedContent);
3740

41+
// Because we have a reference to another content item, we need to use the Elements cache level,
42+
// to make sure that changes to the referenced item are detected and the cache invalidated accordingly.
3843
public PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType propertyType)
3944
=> PropertyCacheLevel.Elements;
4045

41-
public object? ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object? source, bool preview)
42-
// parse the source string to a GuidUdi intermediate value
43-
=> source is string stringValue && UdiParser.TryParse(stringValue, out GuidUdi? guidUdi)
44-
? guidUdi
45-
: null;
46+
// Converts the source value (string) to an intermediate value (GuidUdi)
47+
public object? ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType,
48+
object? source, bool preview)
49+
{
50+
if (source is not string { Length: > 0 } stringValue)
51+
return null;
52+
53+
return UdiParser.TryParse(stringValue, out GuidUdi? guidUdi) ? guidUdi : null;
54+
}
4655

47-
public object? ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object? inter, bool preview)
48-
// inter is expected to be a GuidUdi at this point (see ConvertSourceToIntermediate)
56+
// Converts the intermediate value (GuidUdi) to the actual object value (IPublishedContent)
57+
public object? ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType,
58+
PropertyCacheLevel referenceCacheLevel, object? inter, bool preview)
4959
=> inter is GuidUdi guidUdi
50-
? _publishedSnapshotAccessor.GetRequiredPublishedSnapshot().Content?.GetById(guidUdi.Guid)
60+
? publishedContentCache.GetById(guidUdi.Guid)
5161
: null;
5262
}
63+
64+
5365
```
5466

5567
{% endcode %}

0 commit comments

Comments
 (0)