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
@@ -4,59 +4,34 @@ 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 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.
8
8
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.
10
10
11
+
A PropertyValueConverter has three conversion levels:
11
12
***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.
14
15
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.
22
18
23
19
```csharp
24
-
usingSystem.Linq;
25
-
usingUmbraco.Cms.Core.Composing;
26
-
usingUmbraco.Cms.Core.DependencyInjection;
27
-
28
-
publicclassMyComposer : IComposer
29
-
{
30
-
publicvoidCompose(IUmbracoBuilderbuilder)
31
-
{
32
-
//If the type is accessible (not internal) you can deregister it by the type:
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.
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.
72
48
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
@@ -110,23 +143,13 @@ The property value will be cached until _any_ element (see above) is changed. Th
110
143
111
144
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.
112
145
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
-
125
146
#### `PropertyCacheLevel.None`
126
147
127
148
The property value will _never_ be cached. Every time a property value is accessed (even within the same snapshot) property conversion is performed explicitly.
128
149
150
+
{% hint style="danger" %}
129
151
Use this cache level with extreme caution, as it incurs a massive performance penalty.
@@ -135,17 +158,16 @@ public PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType propertyT
135
158
}
136
159
```
137
160
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.
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.
143
166
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.
147
168
148
169
```csharp
170
+
//Example of a conversion to an int or Guid for a node identifier
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`.
170
197
171
198
The below example converts the nodeId (converted to `Int` or `Udi` by _ConvertSourceToIntermediate_) into an 'IPublishedContent' object.
172
199
173
200
```csharp
201
+
// In this example _contentCache is an instance of IPublishedContentCache that is injected
if (content!=null&&content.ContentType.ItemType==PublishedItemType.Content)
195
-
returncontent;
196
-
}
209
+
varcontent=_contentCache.GetById(id);
210
+
if (content!=null)
211
+
returncontent;
212
+
}
213
+
elseif (interisGuidUdiudi)
214
+
{
215
+
varcontent=_contentCache.GetById(udi.Guid);
216
+
if (content!=null&&content.ContentType.ItemType==PublishedItemType.Content)
217
+
returncontent;
197
218
}
198
219
199
220
returninter;
200
221
}
201
222
```
202
223
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
+
usingSystem.Linq;
235
+
usingUmbraco.Cms.Core.Composing;
236
+
usingUmbraco.Cms.Core.DependencyInjection;
237
+
238
+
publicclassMyComposer : IComposer
239
+
{
240
+
publicvoidCompose(IUmbracoBuilderbuilder)
241
+
{
242
+
//If the type is accessible (not internal) you can deregister it by the type:
0 commit comments