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
Discover how to customize the Content Delivery API's response for your custom
4
-
property editors.
3
+
Discover how to customize the Content Delivery API's response for custom property editors.
5
4
---
6
5
7
6
# Custom property editors support
8
7
9
-
Out of the box, the Delivery API supports custom property editors, ensuring they are rendered alongside the built-in ones in Umbraco. However, if the output generated by your property editor isn't optimal for a headless context, you have the ability to customize the API response. This customization won't impact the Razor rendering, allowing you to tailor the Content Delivery API response according to your specific requirements.
8
+
Out of the box, the Delivery API supports custom property editors, ensuring they are rendered alongside the built-in ones in Umbraco. However, if the output generated by a property editor isn't optimal for a headless context, developers can customize the API response. This customization won't impact the Razor rendering, allowing developers to tailor the Content Delivery API response according to their specific requirements.
10
9
11
-
In this article, we'll look into how you can work with the `IDeliveryApiPropertyValueConverter` interface and implement custom [property expansion](./property-expansion-and-limiting.md) for your custom property editors.
10
+
This article will demonstrate how to work with the `IDeliveryApiPropertyValueConverter` interface and implement custom [property expansion](./property-expansion-and-limiting.md) for custom property editors.
12
11
13
12
## Prerequisite
14
13
15
14
The examples in this article revolve around the fictional `My.Custom.Picker` property editor. This property editor stores the key of a single content item and is backed by a property value converter.
16
15
17
-
We will not dive into the details of creating a custom property editor for Umbraco in this article. If you need guidance on that, please refer to the [Creating a Property Editor](../../tutorials/creating-a-property-editor/README.md) and [Property Value Converters](../../customizing/property-editors/property-value-converters.md) articles.
16
+
This article will not dive into the details of creating a custom property editor for Umbraco. Guidance on that can be found in these articles: [Creating a Property Editor](../../tutorials/creating-a-property-editor/README.md) and [Property Value Converters](../../customizing/property-editors/property-value-converters.md).
18
17
19
18
## Implementation
20
19
21
-
To customize the output of a property value editor in the Delivery API, we need to opt-in by implementing the `IDeliveryApiPropertyValueConverter` interface.
20
+
To customize the output of a property value editor in the Delivery API, opt-in by implementing the `IDeliveryApiPropertyValueConverter` interface.
22
21
23
-
The code example below showcases the implementation of this interface in the property value converter for `My.Custom.Picker`. Our focus will be on the methods provided by the `IDeliveryApiPropertyValueConverter`, as they are responsible for customizing the Delivery API response.
22
+
The code example below showcases the implementation of this interface in the property value converter for `My.Custom.Picker`. The focus will be on customizing the methods provided by the `IDeliveryApiPropertyValueConverter`, as they are responsible for customizing the Delivery API response.
24
23
25
-
Towards the end of the example, you will find the response models that we will be using.
24
+
Response model classes can be found near the end of this article.
26
25
27
26
The `IsConverter()` and `GetPropertyValueType()` methods are inherited from the `PropertyValueConverterBase` class, which is covered in the [Property Value Converters](../../customizing/property-editors/property-value-converters.md) article.
28
27
29
-
{% include "../../.gitbook/includes/obsolete-warning-snapshot.md" %}
Umbraco developers will need to inject `IPublishedContentCache`, `IPublishedMediaCache`, `IPublishedMemberCache` and `IPublishedContentTypeCache` dependencies individually, instead of injecting the `IPublishedSnapshotAccessor` as would have been done previously.
95
+
{% endhint %}
96
+
97
+
{% include "../../.gitbook/includes/obsolete-warning-snapshot.md" %}
98
+
121
99
The Implementation of the `IDeliveryApiPropertyValueConverter` interface can be found in the following methods:
122
100
123
101
*`GetDeliveryApiPropertyCacheLevel()`: This method specifies the cache level used for our property representation in the Delivery API response.
@@ -129,14 +107,12 @@ In the given example, the content key (`Guid` value) is used when rendering with
129
107
130
108
The following example request shows how our custom implementation is reflected in the resulting API response. In this case, our custom property editor is configured under the alias `"pickedItem"`.
131
109
132
-
**Request**
133
-
110
+
**Sample Request**
134
111
```http
135
112
GET /umbraco/delivery/api/v2/content/item/blog
136
113
```
137
114
138
-
**Response**
139
-
115
+
**Sample Response**
140
116
```json
141
117
{
142
118
"name": "Blog",
@@ -163,57 +139,60 @@ GET /umbraco/delivery/api/v2/content/item/blog
163
139
164
140
## Property expansion support
165
141
166
-
Property expansion allows us to conditionally add another level of detail to the Delivery API output. Usually, these additional details are "expensive" to retrieve (for example, requiring database access to populate). By applying property expansion, we provide the option for the caller of the API to opt-in explicitly to this "expensive" operation. From the caller's perspective, the alternative might be an even more expensive additional round-trip to the server.
142
+
Property expansion allows developers to conditionally add another level of detail to the Delivery API output. Usually, these additional details are "expensive" to retrieve (for example, requiring database access to populate). By applying property expansion, Umbraco developers provide the option for the caller of the API to opt-in explicitly to this "expensive" operation. From the caller's perspective, the alternative might be an even more expensive additional round-trip to the server.
167
143
168
-
In our example, property expansion is implemented within `ConvertIntermediateToDeliveryApiObject()`. By considering the value of the `expanding` parameter, we can modify the `BuildDeliveryApiCustomPicker()` method as follows:
169
-
170
-
{% include "../../.gitbook/includes/obsolete-warning-ipublishedsnapshotaccessor.md" %}
144
+
In this example, property expansion is implemented within `ConvertIntermediateToDeliveryApiObject()`. By considering the value of the `expanding` parameter, the `BuildDeliveryApiCustomPicker()` method can be modified as follows:
If the `expanding` parameter is `false`, the method returns the same shallow representation of the referenced content item as before. Otherwise, we retrieve the corresponding `IPublishedContent` and construct our response object accordingly.
206
-
207
-
To see the expanded output in the API response, we need to add the `expand` query parameter to our request. We can use either `?expand=properties[$all]` to expand all properties or `?expand=properties[pickedItem]` to expand the specific `'pickedItem'` property.
186
+
If the `expanding` parameter is `false`, the method returns the same shallow representation of the referenced content item as before. Otherwise, retrieve the corresponding `IPublishedContent` and construct the response object accordingly.
208
187
209
-
**Request**
188
+
To see the expanded output in the API response, add the `expand` query parameter to the request. This parameter can be applied using either `?expand=properties[$all]` to expand all properties or `?expand=properties[pickedItem]` to expand the specific `'pickedItem'` property.
210
189
190
+
**Sample Request**
211
191
```http
212
192
GET /umbraco/delivery/api/v2/content/item/blog?expand=properties[pickedItem]
213
193
```
214
194
215
-
**Response**
216
-
195
+
**Sample Response**
217
196
```json
218
197
{
219
198
"name": "Blog",
@@ -250,3 +229,21 @@ GET /umbraco/delivery/api/v2/content/item/blog?expand=properties[pickedItem]
250
229
```
251
230
252
231
The `itemDetails` property of the `pickedItem` in the JSON response contains the additional details of the selected content item.
0 commit comments