Skip to content

Commit 3b0f456

Browse files
Luuk PetersLuuk Peters
authored andcommitted
Processed PR comments
1 parent 5e30e86 commit 3b0f456

File tree

2 files changed

+52
-56
lines changed

2 files changed

+52
-56
lines changed

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

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
---
2-
description: A guide to creating a custom property value converter in Umbraco
2+
description: A guide to creating a Custom Property Value Converter in Umbraco
33
---
44

55
# Property Value Converters
66

77
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 strongly typed and cleaner models.
88

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.
9+
For example, a Content 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:
11+
A Property Value Converter has three conversion levels:
1212
* **Source** - The raw data stored in the database; this is generally a `string`.
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 a `Guid[]` array.
14-
* **Object** - The object to be used when accessing the property using the Published Content API; for example, UmbracoHelper's `GetPropertyValue<T>` method. Also, the Models Builder generates a property of the type of the object.
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 a `Guid[]`.
14+
* **Object** - The object to be used when accessing the property using the Published Content API; for example, the object returned by the `IPublishedContent.Value<T>(alias)` method. Also, the Models Builder generates a property of the type of the object.
1515

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.
16+
## Create a Property Value Converter
17+
A class becomes a Property Value Converter when it implements the `IPropertyValueConverter` interface from the `Umbraco.Cms.Core.PropertyEditors` namespace. Property Value Converters are automatically registered when implementing the interface. Any given PropertyEditor can only utilize a single Property Value Converter.
1818

1919
```csharp
2020
public class ContentPickerValueConverter : IPropertyValueConverter
2121
```
2222

2323
{% 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, it is assumed that you are using the `IPropertyValueConverter`, so functions are covered.
24+
Consider using the `PropertyValueConverterBase` class as the base of your Property Value Converter instead of the `IPropertyValueConverter` interface. The `PropertyValueConverterBase` class comes with a default implementation of `IPropertyValueConverter`, so you only have to override the methods you need to change. In contrast, if you use the `IPropertyValueConverter`, you are responsible for implementing all methods yourself. In this document, it is assumed that you are using the `IPropertyValueConverter`, so methods are covered.
2525
{% endhint %}
2626

27-
The `IPropertyValueConverter` interface exposes the following functions you need to implement:
27+
The `IPropertyValueConverter` interface exposes the following methods you need to implement:
2828

29-
## Implement information functions
30-
Implement the following functions, which provide Umbraco with context about the PropertyValueConverter.
29+
## Implement information methods
30+
Implement the following methods, which provide Umbraco with context about the Property Value Converter.
3131

3232
### IsConverter(IPublishedPropertyType propertyType)
3333

@@ -44,10 +44,10 @@ public bool IsConverter(IPublishedPropertyType propertyType)
4444
```
4545

4646
### 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.
47+
The IsValue method determines whether a property contains a meaningful value or should be considered "empty" at different stages of the value conversion process. This method is essentially an advanced 'HasValue' method and is essential for Umbraco's property.HasValue() method.
4848

4949
{% hint style="info" %}
50-
There's a basic implementation of this function in `PropertyValueConverterBase` that's good enough for most scenarios.
50+
There's a basic implementation of this method in `PropertyValueConverterBase` that's good enough for most scenarios.
5151
{% endhint %}
5252

5353
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 at what stage of the conversion you need to perform the validation to get the best results. Consider these scenarios:
@@ -122,11 +122,7 @@ public Type GetPropertyValueType(IPublishedPropertyType propertyType)
122122

123123
Here you specify which level the property value is cached at.
124124

125-
A property value can be cached at the following levels:
126-
127-
#### `PropertyCacheLevel.Unknown`
128-
129-
Do not use this cache level unless you know exactly what you're doing. It is recommend using the `PropertyCacheLevel.Element` level.
125+
A property value can be cached at the following levels. This is the most commonly used cache level and should be your default, unless you have specific reasons to do otherwise.
130126

131127
#### `PropertyCacheLevel.Element`
132128

@@ -135,8 +131,6 @@ The property value will be cached until its _element_ is modified. The element i
135131
* For properties used at the page level, the element is the entire page.
136132
* For properties contained within Block List items, the element is the individual Block List item.
137133

138-
This is the most commonly used cache level and should be your default, unless you have specific reasons to do otherwise.
139-
140134
#### `PropertyCacheLevel.Elements`
141135

142136
The property value will be cached until _any_ element (see above) is changed. This means that any change to any page will clear the property value cache.
@@ -151,18 +145,22 @@ The property value will _never_ be cached. Every time a property value is access
151145
Use this cache level with extreme caution, as it incurs a massive performance penalty.
152146
{% endhint %}
153147

148+
#### `PropertyCacheLevel.Unknown`
149+
150+
Do not use this cache level as it is a default fallback for the `PropertyCacheLevel` enum. It will throw an error when used.
151+
154152
```csharp
155153
public PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType propertyType)
156154
{
157155
return PropertyCacheLevel.Elements;
158156
}
159157
```
160158

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.
159+
## Implement conversion methods
160+
Implement the methods that perform the conversion from a raw database value to an intermediate value and then to the final type. Conversions happen in two steps.
163161

164162
### 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 an 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, check if the string value is a valid `Udi` and convert the string to a `Udi` as the intermediate value.
163+
This method converts the raw data value into an appropriate intermediate type that is needed for the final conversion step to an object. For example, for a Content 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, check if the string value is a valid `Udi` and convert the string to a `Udi` as the intermediate value.
166164

167165
Include `using Umbraco.Extensions` to be able to use the `TryConvertTo` extension method.
168166

@@ -193,7 +191,7 @@ public object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPro
193191

194192
### ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview)
195193

196-
This method converts the intermediate value 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`.
194+
This method converts the intermediate value to an object of the type specified in the `GetPropertyValueType()` method of the Property Value Converter. The returned value is used by the `GetPropertyValue<T>` method of `IPublishedContent`.
197195

198196
The example below converts the node ID (converted to `int` or `Udi` by _ConvertSourceToIntermediate_) into an `IPublishedContent` object.
199197

@@ -221,13 +219,13 @@ public object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPro
221219
}
222220
```
223221

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.
222+
## Override existing Property Value Converters
223+
If you are implementing a Property Value Converter for a PropertyEditor that doesn't already have one, creating the Property Value Converter will automatically enable it. No further actions are needed.
226224

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.
225+
If you aim to override an existing Property Value Converter, possibly from Umbraco or a package, additional steps are necessary. Deregister the existing one to prevent conflicts.
228226

229227
{% 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 its 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.
228+
The built-in Property Value Converters 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 its namespace. In order to remove such Property Value Converters, you will need to look up the instance by name and then deregister it by the instance. This could be the case for other Property Value Converters included by packages as well, depending on the implementation details.
231229
{% endhint %}
232230

233231
```csharp

0 commit comments

Comments
 (0)