Skip to content

Instantly share code, notes, and snippets.

@dampee
Created December 23, 2013 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dampee/8099483 to your computer and use it in GitHub Desktop.
Save dampee/8099483 to your computer and use it in GitHub Desktop.
Media Picker Property Editor Value Convertor - without DB hits from uQuery
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Umbraco.Core;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Models;
using umbraco.editorControls;
using Umbraco.Web;
using umbraco;
namespace Our.Umbraco.PropertyEditorConverters
{
/// <summary>
/// original from here:
/// https://bitbucket.org/jeavon/umbraco-core-property-editor-converters/src/b1eb094077362bbbb5d86ff209c024d595b44457/Our.Umbraco.PropertyEditors/MediaPickerPropertyEditorValueConverter.cs?at=default
/// </summary>
public class MediaPickerPropertyEditorValueConverter : IPropertyEditorValueConverter
{
string _propertyTypeAlias = string.Empty;
string _docTypeAlias = string.Empty;
public bool IsConverterFor(Guid propertyEditorId, string docTypeAlias, string propertyTypeAlias)
{
_propertyTypeAlias = propertyTypeAlias;
_docTypeAlias = docTypeAlias;
return Guid.Parse("ead69342-f06d-4253-83ac-28000225583b").Equals(propertyEditorId);
}
public Attempt<object> ConvertPropertyValue(object value)
{
int nodeId; //check value is node id and not stored as node names
if (UmbracoContext.Current != null && int.TryParse(value.ToString(), out nodeId))
{
var umbHelper = new UmbracoHelper(UmbracoContext.Current);
var mediaPickerContent = umbHelper.TypedMedia(nodeId);
if (mediaPickerContent == null || mediaPickerContent.ItemType != PublishedItemType.Media)
{
return Attempt<object>.False;
}
return new Attempt<object>(true, mediaPickerContent);
}
else
{
return Attempt<object>.False;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment