Custom property editors support
Customize the Content Delivery API's response for custom property editors.
Prerequisite
Implementation
using Umbraco.Cms.Core.DeliveryApi;
using Umbraco.Cms.Core.Models.DeliveryApi;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Cms.Core.PropertyEditors.DeliveryApi;
using Umbraco.Cms.Core.PublishedCache;
public class MyCustomPickerValueConverter(
IPublishedContentCache publishedContentCache,
IApiContentRouteBuilder apiContentRouteBuilder):
PropertyValueConverterBase, IDeliveryApiPropertyValueConverter
{
public override bool IsConverter(IPublishedPropertyType propertyType)
=> propertyType.EditorAlias.Equals("My.Custom.Picker");
public override Type GetPropertyValueType(IPublishedPropertyType propertyType)
=> typeof(Guid?);
public PropertyCacheLevel GetDeliveryApiPropertyCacheLevel(IPublishedPropertyType propertyType)
=> PropertyCacheLevel.Elements;
public PropertyCacheLevel GetDeliveryApiPropertyCacheLevelForExpansion(IPublishedPropertyType propertyType)
=> PropertyCacheLevel.Snapshot;
public Type GetDeliveryApiPropertyValueType(IPublishedPropertyType propertyType)
=> typeof(DeliveryApiCustomPicker);
public object? ConvertIntermediateToDeliveryApiObject(
IPublishedElement owner,
IPublishedPropertyType propertyType,
PropertyCacheLevel referenceCacheLevel,
object? inter,
bool preview,
bool expanding)
{
if (inter is null)
{
return null;
}
return BuildDeliveryApiCustomPicker(inter, expanding);
}
private DeliveryApiCustomPicker? BuildDeliveryApiCustomPicker(object inter, bool expanding)
{
if (!Guid.TryParse(inter as string, out Guid id))
{
return null;
}
var content = publishedContentCache.GetById(id);
if (content is null)
{
return null;
}
return new DeliveryApiCustomPicker { Id = id };
}
}Property expansion support
Supporting Model Classes
Last updated
Was this helpful?