Created
January 13, 2014 16:17
-
-
Save kgiszewski/8403024 to your computer and use it in GitHub Desktop.
PVC Umbraco v7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Newtonsoft.Json; | |
using Umbraco.Core.PropertyEditors; | |
using Umbraco.Core.Models.PublishedContent; | |
using Archetype.Umbraco.Models; | |
using Archetype.Umbraco.Extensions; | |
namespace Archetype.Umbraco.PropertyConverters | |
{ | |
/* based on the Tim Geyssens sample at: https://github.com/TimGeyssens/MatrixPropEditor/blob/master/SamplePropertyValueConverter/SamplePropertyValueConverter/MatrixValueConverter.cs */ | |
[PropertyValueType(typeof(Archetype.Umbraco.Models.Archetype))] | |
[PropertyValueCache(PropertyCacheValue.All, PropertyCacheLevel.Content)] | |
public class ArchetypeValueConverter : PropertyValueConverterBase | |
{ | |
public override bool IsConverter(PublishedPropertyType propertyType) | |
{ | |
return propertyType.PropertyEditorAlias.Equals("Imulus.Archetype"); | |
} | |
public override object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview) | |
{ | |
if (source == null) return null; | |
var sourceString = source.ToString(); | |
if (sourceString.DetectIsJson()) | |
{ | |
try | |
{ | |
var archetype = JsonConvert.DeserializeObject <Archetype.Umbraco.Models.Archetype> (sourceString); | |
return archetype; | |
} | |
catch (Exception ex) | |
{ | |
return null; | |
} | |
} | |
return sourceString; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment