Skip to content

Instantly share code, notes, and snippets.

@kgiszewski
Last active August 29, 2015 14:03
Show Gist options
  • Save kgiszewski/67787c06629723c7b329 to your computer and use it in GitHub Desktop.
Save kgiszewski/67787c06629723c7b329 to your computer and use it in GitHub Desktop.
Store Hours Prop Converter
//model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StoreHours.Models
{
public class StoreHoursModel
{
public string Sunday { get; set; }
public string Monday { get; set; }
public string Tuesday { get; set; }
public string Wednesday { get; set; }
public string Thursday { get; set; }
public string Friday { get; set; }
public string Saturday { get; set; }
}
}
//Prop converter
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Umbraco.Core;
using Umbraco.Web;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Logging;
using StoreHours.Models;
namespace StoreHours.PropertyConverter
{
[PropertyValueType(typeof(StoreHoursModel))]
[PropertyValueCache(PropertyCacheValue.All, PropertyCacheLevel.Content)]
public class TableEditorValueConverter : PropertyValueConverterBase
{
public override bool IsConverter(PublishedPropertyType propertyType)
{
return propertyType.PropertyEditorAlias.Equals("Imulus.StoreHours");
}
public override object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview)
{
if (source == null)
{
return new StoreHoursModel();
}
var sourceString = source.ToString();
try
{
var value = JsonConvert.DeserializeObject<StoreHoursModel>(sourceString);
return value;
}
catch (Exception ex)
{
LogHelper.Error<StoreHoursModel>(ex.Message, ex);
return new StoreHoursModel();
}
return sourceString;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment