Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save leekelleher/4682330 to your computer and use it in GitHub Desktop.
Save leekelleher/4682330 to your computer and use it in GitHub Desktop.
Umbraco: A bridge between RazorDataTypeModelBinding and PropertyEditorValueConverter.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using umbraco.MacroEngines;
using Umbraco.Core;
using Umbraco.Core.PropertyEditors;
namespace Our.Umbraco
{
public class RazorDataTypeModelBindingPropertyEditorValueConverter : IPropertyEditorValueConverter
{
private static Dictionary<Tuple<Guid, int>, Type> _razorDataTypeModelTypes = null;
private IRazorDataTypeModel SelectedModelBinder = null;
public bool IsConverterFor(Guid propertyEditorId, string docTypeAlias, string propertyTypeAlias)
{
if (_razorDataTypeModelTypes == null)
{
// dirty, dirty hack!
var internalProperty = typeof(DynamicNode).GetProperty("RazorDataTypeModelTypes", BindingFlags.Static | BindingFlags.NonPublic);
if (internalProperty != null)
{
_razorDataTypeModelTypes = internalProperty.GetValue(null, null) as Dictionary<Tuple<Guid, int>, Type>;
}
}
if (_razorDataTypeModelTypes != null && _razorDataTypeModelTypes.Any(x => x.Key.Item1.Equals(propertyEditorId)))
{
var type = _razorDataTypeModelTypes.FirstOrDefault(x => x.Key.Item1.Equals(propertyEditorId)).Value;
this.SelectedModelBinder = Activator.CreateInstance(type) as IRazorDataTypeModel;
return true;
}
return false;
}
public Attempt<object> ConvertPropertyValue(object value)
{
if (value is string)
{
var result = default(object);
if (this.SelectedModelBinder.Init(0, value.ToString(), out result))
{
return new Attempt<object>(true, result);
}
}
return new Attempt<object>(false, value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment