Custom property editors support
Discover how to customize the Content Delivery API's response for your 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;
namespace Umbraco.Docs.Samples;
public class MyCustomPickerValueConverter : PropertyValueConverterBase, IDeliveryApiPropertyValueConverter
{
private readonly IPublishedSnapshotAccessor _publishedSnapshotAccessor;
private readonly IApiContentRouteBuilder _apiContentRouteBuilder;
public MyCustomPickerValueConverter(
IPublishedSnapshotAccessor publishedSnapshotAccessor,
IApiContentRouteBuilder apiContentRouteBuilder)
{
_publishedSnapshotAccessor = publishedSnapshotAccessor;
_apiContentRouteBuilder = apiContentRouteBuilder;
}
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 (!_publishedSnapshotAccessor.TryGetPublishedSnapshot(out IPublishedSnapshot? publishedSnapshot) ||
publishedSnapshot?.Content is null)
{
return null;
}
if (!Guid.TryParse(inter as string, out Guid id))
{
return null;
}
return new DeliveryApiCustomPicker { Id = id };
}
}
public class DeliveryApiCustomPicker
{
public Guid Id { get; set; }
public DeliveryApiItemDetails? ItemDetails { get; set; }
}
public class DeliveryApiItemDetails
{
public string? Name { get; set; }
public IApiContentRoute? Route { get; set; }
}Property expansion support
Last updated
Was this helpful?