Gibe / Umbraco-Link-Picker

Link picker property editor for Umbraco 7
12 stars 10 forks source link

PR: Value COnverter #19

Closed BarryFogarty closed 5 years ago

BarryFogarty commented 7 years ago

I have written a (very basic) value converter for the Link Picker, which simplifies usage in Views:

public class LinkPickerValueConverter : PropertyValueConverterBase
    {
        public override bool IsConverter(PublishedPropertyType propertyType)
        {
            return "Gibe.LinkPicker".Equals(propertyType.PropertyEditorAlias);
        }

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

            try
            {
                return JsonConvert.DeserializeObject<LinkPicker>(source.ToString());
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }

If you'd like to include this in the project let me know and I'll submit a PR.

karltynan commented 7 years ago

Hey Barry!

Yeh, sounds great. Pull request away! :)

bjarnef commented 7 years ago

I would be great if you could add an assembly to the package, which contains the model and this property value converter 👍

public class LinkPickerModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Url { get; set; }
    public string Target { get; set; }
    public string Hashtarget { get; set; }
}
bjarnef commented 7 years ago

I have this one for my own package Color Palettes, which also returns json https://github.com/bjarnef/color-palettes/blob/master/ColorPalettes/ColorPalettes/Converters/ColorPaletteEditorValueConverter.cs

bjarnef commented 7 years ago

I have added a PR for this: https://github.com/Gibe/Umbraco-Link-Picker/pull/21

stevetemple commented 5 years ago

PropertyValueConverter exists and is in the latest packages