Check if MediaPicker is configured as single or multiple and give back appropriate result for each (IPublishedContent vs IEnumerable<IPublishedContent>)
This commit is contained in:
@@ -8,8 +8,10 @@
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
using System;
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Core.Logging;
|
||||
using Umbraco.Core.Models;
|
||||
using Umbraco.Core.Models.PublishedContent;
|
||||
using Umbraco.Core.PropertyEditors;
|
||||
@@ -21,12 +23,72 @@ namespace Umbraco.Web.PropertyEditors.ValueConverters
|
||||
/// The media picker property value converter.
|
||||
/// </summary>
|
||||
[DefaultPropertyValueConverter]
|
||||
[PropertyValueType(typeof(IPublishedContent))]
|
||||
[PropertyValueCache(PropertyCacheValue.Object, PropertyCacheLevel.ContentCache)]
|
||||
[PropertyValueCache(PropertyCacheValue.Source, PropertyCacheLevel.Content)]
|
||||
[PropertyValueCache(PropertyCacheValue.XPath, PropertyCacheLevel.Content)]
|
||||
public class MediaPickerPropertyConverter : PropertyValueConverterBase
|
||||
public class MediaPickerPropertyConverter : PropertyValueConverterBase, IPropertyValueConverterMeta
|
||||
{
|
||||
public Type GetPropertyValueType(PublishedPropertyType propertyType)
|
||||
{
|
||||
return IsMultipleDataType(propertyType.DataTypeId) ? typeof(IEnumerable<IPublishedContent>) : typeof(IPublishedContent);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The is multiple data type.
|
||||
/// </summary>
|
||||
/// <param name="dataTypeId">
|
||||
/// The data type id.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The <see cref="bool"/>.
|
||||
/// </returns>
|
||||
private bool IsMultipleDataType(int dataTypeId)
|
||||
{
|
||||
var multipleItems = false;
|
||||
|
||||
try
|
||||
{
|
||||
var dts = ApplicationContext.Current.Services.DataTypeService;
|
||||
|
||||
var multiPickerPreValues =
|
||||
dts.GetPreValuesCollectionByDataTypeId(dataTypeId).PreValuesAsDictionary;
|
||||
|
||||
var multiPickerPreValue = multiPickerPreValues.FirstOrDefault(x => string.Equals(x.Key, "multiPicker", StringComparison.InvariantCultureIgnoreCase)).Value;
|
||||
|
||||
var attemptConvert = multiPickerPreValue.Value.TryConvertTo<bool>();
|
||||
|
||||
if (attemptConvert.Success)
|
||||
{
|
||||
multipleItems = attemptConvert.Result;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Error<MediaPickerPropertyConverter>(string.Format("Error finding multipleItems data type prevalue on data type Id {0}", dataTypeId), ex);
|
||||
}
|
||||
|
||||
return multipleItems;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if this converter can convert the property editor and registers if it can.
|
||||
/// </summary>
|
||||
@@ -62,13 +124,11 @@ namespace Umbraco.Web.PropertyEditors.ValueConverters
|
||||
/// </returns>
|
||||
public override object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview)
|
||||
{
|
||||
var attemptConvertInt = source.TryConvertTo<int>();
|
||||
if (attemptConvertInt.Success)
|
||||
return attemptConvertInt.Result;
|
||||
var attemptConvertUdi = source.TryConvertTo<Udi>();
|
||||
if (attemptConvertUdi.Success)
|
||||
return attemptConvertUdi.Result;
|
||||
return null;
|
||||
var nodeIds = source.ToString()
|
||||
.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)
|
||||
.Select(Udi.Parse)
|
||||
.ToArray();
|
||||
return nodeIds;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -93,12 +153,24 @@ namespace Umbraco.Web.PropertyEditors.ValueConverters
|
||||
return null;
|
||||
}
|
||||
|
||||
Udi udi;
|
||||
if (Udi.TryParse(source.ToString(), out udi))
|
||||
var udis = (Udi[])source;
|
||||
var mediaItems = new List<IPublishedContent>();
|
||||
if (udis.Any())
|
||||
{
|
||||
var media = udi.ToPublishedContent();
|
||||
if (media != null)
|
||||
return media;
|
||||
foreach (var udi in udis)
|
||||
{
|
||||
var item = udi.ToPublishedContent();
|
||||
if (item != null)
|
||||
mediaItems.Add(item);
|
||||
}
|
||||
if (IsMultipleDataType(propertyType.DataTypeId))
|
||||
{
|
||||
return mediaItems;
|
||||
}
|
||||
else
|
||||
{
|
||||
return mediaItems.FirstOrDefault();
|
||||
}
|
||||
}
|
||||
|
||||
return source;
|
||||
|
||||
Reference in New Issue
Block a user