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
1313namespace 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