Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add MediaPicker3 support #19

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Our.Umbraco.SuperValueConverters/Models/MediaWithCrops.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;

namespace Our.Umbraco.SuperValueConverters.Models
{
public class MediaWithCrops<T> : MediaWithCrops where T : class, IPublishedContent
{
public MediaWithCrops()
{

}

public MediaWithCrops(MediaWithCrops mediaWithCrops)
{
this.MediaItem = mediaWithCrops.MediaItem as T;
this.LocalCrops = mediaWithCrops.LocalCrops;
}

public new T MediaItem { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="UmbracoCms.Web" Version="8.1.0" />
<PackageReference Include="UmbracoCms.Web" Version="8.14.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Our.Umbraco.SuperValueConverters.Composing;
using Umbraco.Core.Composing;
using Umbraco.Web.PropertyEditors.ValueConverters;

namespace Our.Umbraco.SuperValueConverters.Startup
{
public class MediaPicker3Composer : IUserComposer
{
public void Compose(Composition composition)
{
composition.DisableConverter<MediaPickerWithCropsValueConverter>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using Our.Umbraco.SuperValueConverters.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.PropertyEditors;
using Umbraco.Web.PropertyEditors;
using Core = Umbraco.Web.PropertyEditors.ValueConverters;

namespace Our.Umbraco.SuperValueConverters.ValueConverters
{
public class MediaPicker3ValueConverter : SuperValueConverterBase
{
public MediaPicker3ValueConverter(Core.MediaPickerWithCropsValueConverter baseValueConverter)
: base(baseValueConverter)
{

}

public override Type GetPropertyValueType(IPublishedPropertyType propertyType)
{
var settings = GetSettings(propertyType);

var mediaGenericType = GetGenericMediaType(settings);

var type = settings.AllowsMultiple() ? typeof(IEnumerable<>).MakeGenericType(mediaGenericType) : mediaGenericType;

return type;
}

public override object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview)
{
var media = BaseValueConverter.ConvertIntermediateToObject(owner, propertyType, referenceCacheLevel, inter, preview);

if (media == null)
{
return media;
}

var settings = GetSettings(propertyType);

var mediaGenericType = GetGenericMediaType(settings);

if (media is IEnumerable<MediaWithCrops> multiMedia)
{
var typedMultiMedia = AdaptList(multiMedia, mediaGenericType);
return typedMultiMedia;
}

if (media is MediaWithCrops mediaWithCrops)
{
var typedMedia = Activator.CreateInstance(mediaGenericType, mediaWithCrops);
return typedMedia;
}

return null;
}

private IList AdaptList(IEnumerable<MediaWithCrops> media, Type mediaGeneric)
{
Type listType = typeof(List<>).MakeGenericType(new[] { mediaGeneric });
IList list = (IList)Activator.CreateInstance(listType);

foreach (var m in media)
{
var typedMedia = Activator.CreateInstance(mediaGeneric, m);
list.Add(typedMedia);
}

return list;
}

private Type GetGenericMediaType(IPickerSettings settings)
{
var mediaWithCropsType = settings.DefaultType ?? typeof(MediaWithCrops);

if (settings.AllowedTypes.Any() == true)
{
var mediaItemType = GetTypeForAllowedTypes(settings.AllowedTypes);

if (mediaItemType != null)
{
mediaWithCropsType = typeof(MediaWithCrops<>).MakeGenericType(mediaItemType);
}
}

return mediaWithCropsType;
}

public override IPickerSettings GetSettings(IPublishedPropertyType propertyType)
{
var configuration = propertyType.DataType.ConfigurationAs<MediaPicker3Configuration>();

var validationMax = configuration.ValidationLimit?.Max ?? 0;

var max = configuration.Multiple ? validationMax : 1;

var settings = new PickerSettings
{
MaxItems = max,
DefaultType = typeof(MediaWithCrops)
};

if (string.IsNullOrEmpty(configuration.Filter) == false)
{
settings.AllowedTypes = configuration.Filter.Split(',');
}

return settings;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ namespace Our.Umbraco.SuperValueConverters.ValueConverters
{
public abstract class SuperValueConverterBase : PropertyValueConverterBase
{
private IPropertyValueConverter _baseValueConverter;
protected IPropertyValueConverter BaseValueConverter;

public SuperValueConverterBase(IPropertyValueConverter baseValueConverter)
{
_baseValueConverter = baseValueConverter;
BaseValueConverter = baseValueConverter;
}

public override Type GetPropertyValueType(IPublishedPropertyType propertyType)
Expand All @@ -43,7 +43,7 @@ public override Type GetPropertyValueType(IPublishedPropertyType propertyType)
return modelType;
}

private Type GetTypeForAllowedTypes(string[] allowedTypes)
protected Type GetTypeForAllowedTypes(string[] allowedTypes)
{
var types = TypeHelper.GetPublishedModelTypes(allowedTypes);

Expand All @@ -66,7 +66,7 @@ private Type GetTypeForAllowedTypes(string[] allowedTypes)

public override object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview)
{
var value = _baseValueConverter.ConvertIntermediateToObject(owner, propertyType, referenceCacheLevel, inter, preview);
var value = BaseValueConverter.ConvertIntermediateToObject(owner, propertyType, referenceCacheLevel, inter, preview);

var clrType = propertyType.ClrType;

Expand Down Expand Up @@ -118,17 +118,17 @@ private static IList CastSourceToList(object source, Type modelType)

public override object ConvertIntermediateToXPath(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview)
{
return _baseValueConverter.ConvertIntermediateToXPath(owner, propertyType, referenceCacheLevel, inter, preview);
return BaseValueConverter.ConvertIntermediateToXPath(owner, propertyType, referenceCacheLevel, inter, preview);
}

public override object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object source, bool preview)
{
return _baseValueConverter.ConvertSourceToIntermediate(owner, propertyType, source, preview);
return BaseValueConverter.ConvertSourceToIntermediate(owner, propertyType, source, preview);
}

public override bool IsConverter(IPublishedPropertyType propertyType)
{
return _baseValueConverter.IsConverter(propertyType);
return BaseValueConverter.IsConverter(propertyType);
}

#endregion
Expand Down