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: 16/umbraco-cms/customizing/property-editors/property-value-converters.md
+16-16Lines changed: 16 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,14 +4,14 @@ description: A guide to creating a custom property value converter in Umbraco
4
4
5
5
# Property Value Converters
6
6
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.
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
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.
10
10
11
11
A PropertyValueConverter has three conversion levels:
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 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.
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.
15
15
16
16
## Create a PropertyValueConverter
17
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.
@@ -21,7 +21,7 @@ public class ContentPickerValueConverter : IPropertyValueConverter
@@ -47,10 +47,10 @@ public bool IsConverter(IPublishedPropertyType propertyType)
47
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.
48
48
49
49
{% hint style="info" %}
50
-
There's a basic implementation of this function in `PropertyValueConverterBase` that's good enough for most scenario's.
50
+
There's a basic implementation of this function in `PropertyValueConverterBase` that's good enough for most scenarios.
51
51
{% endhint %}
52
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:
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:
54
54
55
55
```csharp
56
56
//If value is a simple string, it's enough to just check if string is null or empty
@@ -71,7 +71,7 @@ public bool? IsValue(object? value, PropertyValueLevel level)
71
71
}
72
72
73
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
74
+
//or empty, but also to check if the value is a valid int and if it doesn't contain the default value
This is where you can specify the type returned by this Converter. This type will be used by ModelsBuilder to return data from properties using this Converter in the proper type.
110
+
This is where you can specify the type returned by this converter. This type will be used by Models Builder to return data from properties using this converter in the proper type.
111
111
112
112
Example: Content Picker data is being converted to `IPublishedContent`.
113
113
@@ -162,7 +162,7 @@ public PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType propertyT
162
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.
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.
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, we check if the string value is a valid `Udi` and convert the string to a `Udi` as the intermediate value.
166
166
167
167
Include `using Umbraco.Extensions` to be able to use the `TryConvertTo` extension method.
168
168
@@ -183,7 +183,7 @@ public object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPro
183
183
returnnull;
184
184
}
185
185
186
-
//For simple property editors, like a textbox, that only store a text string
186
+
//For simple property editors, like a textbox, that only store a text string,
187
187
//you can just return the raw value since the intermediate and raw values are the same
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`.
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`.
197
197
198
-
The below example converts the nodeId (converted to `Int` or `Udi` by _ConvertSourceToIntermediate_) into an 'IPublishedContent' object.
198
+
The example below converts the node ID (converted to `int` or `Udi` by _ConvertSourceToIntermediate_) into an `IPublishedContent` object.
199
199
200
200
```csharp
201
201
// In this example _contentCache is an instance of IPublishedContentCache that is injected
@@ -224,10 +224,10 @@ public object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPro
224
224
## Override existing PropertyValueConverters
225
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
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.
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.
228
228
229
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.
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.
0 commit comments