Custom value conversion for rendering
Add a Property Value Converter for custom Property Editor value conversion.
Last updated
Was this helpful?
Add a Property Value Converter for custom Property Editor value conversion.
Last updated
Was this helpful?
Was this helpful?
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.PropertyEditors;
namespace Umbraco.Docs.PropertyEditors;
// the Property Value Converter that handles the suggestions Property Editor
public class MySuggestionsPropertyValueConverter : PropertyValueConverterBase
{
// 1. converts properties with the property type editor UI alias "My.PropertyEditorUi.Suggestions"
public override bool IsConverter(IPublishedPropertyType propertyType)
=> propertyType.EditorUiAlias == "My.PropertyEditorUi.Suggestions";
// 2. yields outputs of type MySuggestionsModel
public override Type GetPropertyValueType(IPublishedPropertyType propertyType)
=> typeof(MySuggestionsModel);
// 3. converts the suggestion (string) to the output type (MySuggestionsModel)
public override object? ConvertIntermediateToObject(
IPublishedElement owner,
IPublishedPropertyType propertyType,
PropertyCacheLevel referenceCacheLevel,
object? inter,
bool preview)
=> inter is string suggestion
? new MySuggestionsModel
{
Suggestion = $"Here's a suggestion for you: {suggestion}"
}
: null;
}
// the custom rendering model for the suggestions Property Editor
public class MySuggestionsModel
{
public required string Suggestion { get; init; }
}