You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 15/umbraco-cms/customizing/property-editors/property-value-converters.md
+26-28Lines changed: 26 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,33 +1,33 @@
1
1
---
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
3
3
---
4
4
5
5
# Property Value Converters
6
6
7
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 strongly typed and cleaner models.
8
8
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.
10
10
11
-
A PropertyValueConverter has three conversion levels:
11
+
A Property Value Converter has three conversion levels:
12
12
***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.
15
15
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.
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.
48
48
49
49
{% 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.
51
51
{% endhint %}
52
52
53
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 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)
122
122
123
123
Here you specify which level the property value is cached at.
124
124
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.
130
126
131
127
#### `PropertyCacheLevel.Element`
132
128
@@ -135,8 +131,6 @@ The property value will be cached until its _element_ is modified. The element i
135
131
* For properties used at the page level, the element is the entire page.
136
132
* For properties contained within Block List items, the element is the individual Block List item.
137
133
138
-
This is the most commonly used cache level and should be your default, unless you have specific reasons to do otherwise.
139
-
140
134
#### `PropertyCacheLevel.Elements`
141
135
142
136
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
151
145
Use this cache level with extreme caution, as it incurs a massive performance penalty.
152
146
{% endhint %}
153
147
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.
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.
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.
166
164
167
165
Include `using Umbraco.Extensions` to be able to use the `TryConvertTo` extension method.
168
166
@@ -193,7 +191,7 @@ public object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPro
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`.
197
195
198
196
The example below converts the node ID (converted to `int` or `Udi` by _ConvertSourceToIntermediate_) into an `IPublishedContent` object.
199
197
@@ -221,13 +219,13 @@ public object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPro
221
219
}
222
220
```
223
221
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.
226
224
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.
228
226
229
227
{% 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.
0 commit comments