Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add propertyvalueconverter #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions StringListPropertyValueConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.PropertyEditors;

public class StringListPropertyValueConverter : IPropertyValueConverter, IPropertyValueConverterMeta
{
public bool IsConverter(PublishedPropertyType propertyType)
{
return propertyType.PropertyEditorAlias.Equals("Epiphany.SortableStringList");
}

public object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview)
{
if (source == null) { return null; }

return DeserializeFromJson(source.ToString());
}

public object ConvertSourceToObject(PublishedPropertyType propertyType, object source, bool preview)
{
return source;
}

public object ConvertSourceToXPath(PublishedPropertyType propertyType, object source, bool preview)
{
return source.ToString();
}

public Type GetPropertyValueType(PublishedPropertyType propertyType)
{
return typeof(IList<string>);
}

public PropertyCacheLevel GetPropertyCacheLevel(PublishedPropertyType propertyType, PropertyCacheValue cacheValue)
{
PropertyCacheLevel returnLevel;
switch (cacheValue)
{
case PropertyCacheValue.Object:
returnLevel = PropertyCacheLevel.ContentCache;
break;
case PropertyCacheValue.Source:
returnLevel = PropertyCacheLevel.Content;
break;
case PropertyCacheValue.XPath:
returnLevel = PropertyCacheLevel.Content;
break;
default:
returnLevel = PropertyCacheLevel.None;
break;
}
return returnLevel;
}

private static IList<string> DeserializeFromJson(string json)
{
var jsonObject = Newtonsoft.Json.JsonConvert.DeserializeObject<IList<string>>(json);
return jsonObject;
}
}