Skip to content

Commit 1d81284

Browse files
Luuk PetersLuuk Peters
authored andcommitted
First version done, needs refinement
1 parent 4c570a5 commit 1d81284

File tree

1 file changed

+134
-80
lines changed

1 file changed

+134
-80
lines changed

16/umbraco-cms/customizing/property-editors/property-value-converters.md

Lines changed: 134 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -4,59 +4,34 @@ description: A guide to creating a custom property value converter in Umbraco
44

55
# Property Value Converters
66

7-
A Property Value Converter converts a property editor's database-stored value to another type. The converted value can be accessed from MVC Razor or any other Published Content API.
7+
A Property Value Converter converts a property editor's database-stored value into another type that is stored in the Umbraco cache. This way, the database stores only the most essential data, while Razor views, the Published Content API and the Content Delivery API can work with strong typed and cleaner models.
88

9-
Published property values have four "Values":
9+
For example, a Document picker typically only stores the Key of the picked node in the database, but when working with the published data, an IPublishedContent object is returned instead of just the key. This conversion is done by a Property Value Converter.
1010

11+
A PropertyValueConverter has three conversion levels:
1112
* **Source** - The raw data stored in the database, this is generally a `String`
12-
* **Intermediate** - An object of a type that is appropriate to the property, for example a nodeId should be an `Int` or a collection of nodeIds would be an integer array, `Int[]`
13-
* **Object** - The object to be used when accessing the property using a Published Content API, for example UmbracoHelper's `GetPropertyValue<T>` method
13+
* **Intermediate** - An object of a type that is appropriate to the property, for example a node key should be a `Guid` or a collection of node keys would be an Guid array `Guid[]`
14+
* **Object** - The object to be used when accessing the property using a Published Content API, for example UmbracoHelper's `GetPropertyValue<T>` method. Also, te models builder generates a property of the type of the object.
1415

15-
## Registering PropertyValueConverters
16-
17-
PropertyValueConverters are automatically registered when implementing the interface. Any given PropertyEditor can only utilize a single PropertyValueConverter.
18-
19-
If you are implementing a PropertyValueConverter for a PropertyEditor that doesn't already have one, creating the PropertyValueConverter will automatically enable it. No further actions are needed.
20-
21-
If you aim to override an existing PropertyValueConverter, possibly from Umbraco or a package, additional steps are necessary. Deregister the existing one to prevent conflicts in this scenario.
16+
## Create a PropertyValueConverter
17+
A class becomes a PropertyValueConverter when it implements the `IPropertyValueConverter` interface from the `Umbraco.Cms.Core.PropertyEditors` namespace. PropertyValueConverters are automatically registered when implementing the interface. Any given PropertyEditor can only utilize a single PropertyValueConverter.
2218

2319
```csharp
24-
using System.Linq;
25-
using Umbraco.Cms.Core.Composing;
26-
using Umbraco.Cms.Core.DependencyInjection;
27-
28-
public class MyComposer : IComposer
29-
{
30-
public void Compose(IUmbracoBuilder builder)
31-
{
32-
//If the type is accessible (not internal) you can deregister it by the type:
33-
builder.PropertyValueConverters().Remove<MyCustom.StandardValueConnector>();
34-
35-
//If the type is not accessible you will need to locate the instance and then remove it:
36-
var contentPickerValueConverter = builder.PropertyValueConverters().GetTypes().FirstOrDefault(x => x.Name == "ContentPickerValueConverter");
37-
if (contentPickerValueConverter != null)
38-
{
39-
builder.PropertyValueConverters().Remove(contentPickerValueConverter);
40-
}
41-
}
42-
}
20+
public class ContentPickerValueConverter : IPropertyValueConverter
4321
```
4422

45-
The built-in PropertyValueConverters included with Umbraco, are currently marked as internal. This means you will not be able to remove them by type since the type isn't accessible outside of the namespace. In order to remove such PropertyValueConverters, you will need to look up the instance by name and then deregister it by the instance. This could be the case for other PropertyValueConverters included by packages as well, depending on the implementation details.
46-
47-
## Implementing the Interface
23+
{% hint style="info" %}
24+
Consider using the `PropertyValueConverterBase` class as the base of your PropertyValueConverter instead of the `IPropertyValueConverter` interface. The `PropertyValueConverterBase` class comes with a default implementation of `IPropertyValueConverter`, so you only need to override the functions you need to change. In contrast, if you use the `IPropertyValueConverter` you are responsible for implementing all functions yourself. In this document we'll assume that you are using the `IPropertyValueConverter` so we cover all functions.
25+
{% endhint %}
4826

49-
Implement `IPropertyValueConverter` from the `Umbraco.Cms.Core.PropertyEditors` namespace on your class
27+
The `IPropertyValueConverter` interface exposes the following functions you need to implement:
5028

51-
```csharp
52-
public class ContentPickerValueConverter : IPropertyValueConverter
53-
```
54-
55-
## Methods - Information
29+
## Implement information functions
30+
Implement the following functions, which provide Umbraco with context about the PropertyValueConverter.
5631

5732
### IsConverter(IPublishedPropertyType propertyType)
5833

59-
This method is called for each PublishedPropertyType (Document Type Property) at application startup. By returning `True` your value converter will be registered for that property type and your conversion methods will be executed whenever that value is requested.
34+
This method is called for each PublishedPropertyType (Document Type Property) at Umbraco startup. By returning `True` your value converter will be registered for that property type and your conversion methods will be executed whenever that value is requested.
6035

6136
Example: Checking if the IPublishedPropertyType EditorAlias property is equal to the alias of the core content editor.\
6237
This check is a string comparison but we recommend creating a constant for it to avoid spelling errors:
@@ -69,8 +44,66 @@ public bool IsConverter(IPublishedPropertyType propertyType)
6944
```
7045

7146
### IsValue(object value, PropertyValueLevel level)
47+
The IsValue function determines whether a property contains a meaningful value or should be considered "empty" at different stages of the value conversion process. This function is essentially an advanced 'HasValue' function and is essential for Umbraco's property.HasValue() method.
7248

73-
This method is called to determine if the passed-in value is a value, and is of the level specified. There's a basic implementation of this in `PropertyValueConverterBase`.
49+
{% hint style="info" %}
50+
There's a basic implementation of this function in `PropertyValueConverterBase` that's good enough for most scenario's.
51+
{% endhint %}
52+
53+
When Umbraco needs to check if a property has a valid value, it calls IsValue progressively through three conversion levels until one returns true. They are called in the order of Source > Inter > Object. This allows you to choose in what stage of the conversion you need to perform the validation to get the best results. Consider these scenario's:
54+
55+
```csharp
56+
//If value is a simple string, it's enough to just check if string is null or empty
57+
//This is the default implementation in PropertyValueConverterBase
58+
public bool? IsValue(object? value, PropertyValueLevel level)
59+
{
60+
switch (level)
61+
{
62+
case PropertyValueLevel.Source:
63+
return value != null && (!(value is string stringValue) || !string.IsNullOrWhiteSpace(stringValue));
64+
case PropertyValueLevel.Inter:
65+
return null;
66+
case PropertyValueLevel.Object:
67+
return null;
68+
default:
69+
throw new NotSupportedException($"Invalid level: {level}.");
70+
}
71+
}
72+
73+
//If the value is numeric, it's usually not enough to check if the raw string value is null
74+
//or empty, but also to check if the value is a valid int and if doesn't contain the default value
75+
public bool? IsValue(object? value, PropertyValueLevel level)
76+
{
77+
switch (level)
78+
{
79+
case PropertyValueLevel.Source:
80+
return null;
81+
case PropertyValueLevel.Inter:
82+
return value is int intValue && intValue > 0;
83+
case PropertyValueLevel.Object:
84+
return null;
85+
default:
86+
throw new NotSupportedException($"Invalid level: {level}.");
87+
}
88+
}
89+
90+
//If the value is a complex object, you can consider checking
91+
//the object level
92+
public bool? IsValue(object? value, PropertyValueLevel level)
93+
{
94+
switch (level)
95+
{
96+
case PropertyValueLevel.Source:
97+
return null;
98+
case PropertyValueLevel.Inter:
99+
return null;
100+
case PropertyValueLevel.Object:
101+
return value is ComplexObject objectValue && objectValue.SomeProperty == "value";
102+
default:
103+
throw new NotSupportedException($"Invalid level: {level}.");
104+
}
105+
}
106+
```
74107

75108
### GetPropertyValueType(IPublishedPropertyType propertyType)
76109

@@ -110,23 +143,13 @@ The property value will be cached until _any_ element (see above) is changed. Th
110143

111144
This is particularly useful for property values that contain references to other content or elements. For example, this cache level is utilized by the Content Picker to clear its property values from the cache upon content updates.
112145

113-
#### `PropertyCacheLevel.Snapshot`
114-
115-
{% hint style="warning" %}
116-
`PropertyCacheLevel.Snapshot` is obsolete in Umbraco 15 and will be removed in a future version.
117-
{% endhint %}
118-
119-
The property value will only be cached for the duration of the current _snapshot_.
120-
121-
A snapshot represents a point in time. For example, a snapshot is created for every content request from the frontend. When accessing a property in a snapshot using this cache level, it gets converted, cached throughout the snapshot, and later cleared.
122-
123-
For all intents and purposes, think of this cache level as "per request". If your property value should _only_ be cached per request, this is the cache level you should use. Use it with caution, as the added property conversions incur a performance penalty.
124-
125146
#### `PropertyCacheLevel.None`
126147

127148
The property value will _never_ be cached. Every time a property value is accessed (even within the same snapshot) property conversion is performed explicitly.
128149

150+
{% hint style="danger" %}
129151
Use this cache level with extreme caution, as it incurs a massive performance penalty.
152+
{% endhint %}
130153

131154
```csharp
132155
public PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType propertyType)
@@ -135,17 +158,16 @@ public PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType propertyT
135158
}
136159
```
137160

138-
## Methods - Conversion
139-
140-
There are a few different levels of conversion which can occur.
161+
## Implement conversion functions
162+
Implement the functions that perform the conversion from a raw database value to an intermediate value and then to the final type. Conversions happen in two steps.
141163

142164
### ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object source, bool preview)
165+
This method converts the raw data value into an appropriate intermediate type that is needed for the final conversion step to object. For example, for a node picker the node identifier's raw value is saved as a `String`, but to get to an `IPublishedContent` in the final conversion step, we need a `Udi` instead of a `String`. So in the intermediate step, we check if the string value is a valid `Uid` and convert the string to a `Uid` as intermediate value.
143166

144-
This method should convert the raw data value into an appropriate type. For example, a node identifier stored as a `String` should be converted to an `Int` or `Udi`.
145-
146-
Include a `using Umbraco.Extensions;` to be able to use the `TryConvertTo` extension method.
167+
Include `using Umbraco.Extensions` to be able to use the `TryConvertTo` extension method.
147168

148169
```csharp
170+
//Example of a conversion to an int or Guid for a node identifier
149171
public object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object source, bool preview)
150172
{
151173
if (source == null) return null;
@@ -160,46 +182,78 @@ public object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPro
160182

161183
return null;
162184
}
185+
186+
//For simple property editors, like a textbox, that only store a text string
187+
//you can just return the raw value since the intermediate and raw values are the same
188+
public object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object source, bool preview)
189+
{
190+
return source as string;
191+
}
163192
```
164193

165194
### ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview)
166195

167-
This method converts the Intermediate to an Object. The returned value is used by the `GetPropertyValue<T>` method of `IPublishedContent`.
168-
169-
{% include "../../.gitbook/includes/obsolete-warning-ipublishedsnapshotaccessor.md" %}
196+
This method converts the Intermediate to an Object of the type specified in the `GetPropertyValueType()` function of the PropertyValueConverter. The returned value is used by the `GetPropertyValue<T>` method of `IPublishedContent`.
170197

171198
The below example converts the nodeId (converted to `Int` or `Udi` by _ConvertSourceToIntermediate_) into an 'IPublishedContent' object.
172199

173200
```csharp
201+
// In this example _contentCache is an instance of IPublishedContentCache that is injected
174202
public object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview)
175203
{
176204
if (inter == null)
177205
return null;
178206

179-
if ((propertyType.Alias != null && PropertiesToExclude.Contains(propertyType.Alias.ToLower(CultureInfo.InvariantCulture))) == false)
207+
if (inter is int id)
180208
{
181-
IPublishedContent content;
182-
if (inter is int id)
183-
{
184-
content = _publishedSnapshotAccessor.PublishedSnapshot.Content.GetById(id);
185-
if (content != null)
186-
return content;
187-
}
188-
else
189-
{
190-
var udi = inter as GuidUdi;
191-
if (udi == null)
192-
return null;
193-
content = _publishedSnapshotAccessor.PublishedSnapshot.Content.GetById(udi.Guid);
194-
if (content != null && content.ContentType.ItemType == PublishedItemType.Content)
195-
return content;
196-
}
209+
var content = _contentCache.GetById(id);
210+
if (content != null)
211+
return content;
212+
}
213+
else if (inter is GuidUdi udi)
214+
{
215+
var content = _contentCache.GetById(udi.Guid);
216+
if (content != null && content.ContentType.ItemType == PublishedItemType.Content)
217+
return content;
197218
}
198219

199220
return inter;
200221
}
201222
```
202223

203-
## Sample
224+
## Override existing PropertyValueConverters
225+
If you are implementing a PropertyValueConverter for a PropertyEditor that doesn't already have one, creating the PropertyValueConverter will automatically enable it. No further actions are needed.
226+
227+
If you aim to override an existing PropertyValueConverter, possibly from Umbraco or a package, additional steps are necessary. Deregister the existing one to prevent conflicts in this scenario.
228+
229+
{% hint style="info" %}
230+
The built-in PropertyValueConverters included with Umbraco, are currently marked as internal. This means you will not be able to remove them by type since the type isn't accessible outside of the namespace. In order to remove such PropertyValueConverters, you will need to look up the instance by name and then deregister it by the instance. This could be the case for other PropertyValueConverters included by packages as well, depending on the implementation details.
231+
{% endhint %}
232+
233+
```csharp
234+
using System.Linq;
235+
using Umbraco.Cms.Core.Composing;
236+
using Umbraco.Cms.Core.DependencyInjection;
237+
238+
public class MyComposer : IComposer
239+
{
240+
public void Compose(IUmbracoBuilder builder)
241+
{
242+
//If the type is accessible (not internal) you can deregister it by the type:
243+
builder.PropertyValueConverters().Remove<MyCustom.StandardValueConnector>();
244+
245+
//If the type is not accessible you will need to locate the instance and then remove it:
246+
var contentPickerValueConverter = builder.PropertyValueConverters().GetTypes()
247+
.FirstOrDefault(converter => converter.Name == "ContentPickerValueConverter");
248+
249+
if (contentPickerValueConverter != null)
250+
{
251+
builder.PropertyValueConverters().Remove(contentPickerValueConverter);
252+
}
253+
}
254+
}
255+
```
256+
257+
## Full example
204258

205259
[Content Picker to `IPublishedContent` using `IPropertyValueConverter` interface](full-examples-value-converters.md)

0 commit comments

Comments
 (0)