Cleanup/fix content variants mapping

This commit is contained in:
Stephan
2018-09-17 13:19:24 +02:00
parent fb246de5fc
commit 95361da1f8
9 changed files with 82 additions and 87 deletions

View File

@@ -67,7 +67,7 @@ namespace Umbraco.Web.Editors.Binders
//map the property dto collection with the culture of the current variant
variant.PropertyCollectionDto = Mapper.Map<ContentPropertyCollectionDto>(
model.PersistedContent,
options => options.Items[ResolutionContextExtensions.CultureKey] = variant.Culture);
options => options.SetCulture(variant.Culture));
}
//now map all of the saved values to the dto

View File

@@ -494,15 +494,14 @@ namespace Umbraco.Web.Editors
Mapper.Map<IContent, ContentItemBasic<ContentPropertyBasic>>(content,
opts =>
{
opts.Items[ResolutionContextExtensions.CultureKey] = cultureName;
opts.SetCulture(cultureName);
// if there's a list of property aliases to map - we will make sure to store this in the mapping context.
if (string.IsNullOrWhiteSpace(includeProperties) == false)
{
opts.Items["IncludeProperties"] = includeProperties.Split(new[] { ", ", "," }, StringSplitOptions.RemoveEmptyEntries);
}
}));
if (!includeProperties.IsNullOrWhiteSpace())
opts.SetIncludedProperties(includeProperties.Split(new[] { ", ", "," }, StringSplitOptions.RemoveEmptyEntries));
}))
.ToList(); // evaluate now
return pagedResult;
}

View File

@@ -39,7 +39,7 @@ namespace Umbraco.Web.Models.Mapping
{
//We need to set the culture in the mapping context since this is needed to ensure that the correct property values
//are resolved during the mapping
context.Items[ResolutionContextExtensions.CultureKey] = x.IsoCode;
context.Options.SetCulture(x.IsoCode);
return context.Mapper.Map<IContent, ContentVariantDisplay>(source, null, context);
}).ToList();

View File

@@ -96,31 +96,20 @@ namespace Umbraco.Web.Models.Mapping
{
public DateTime Resolve(IContent source, ContentItemBasic<ContentPropertyBasic> destination, DateTime destMember, ResolutionContext context)
{
var culture = context.GetCulture();
// invariant = global date
if (!source.ContentType.VariesByCulture()) return source.UpdateDate;
//a culture needs to be in the context for a variant content item
if (culture == null || source.ContentType.VariesByCulture() == false)
return source.UpdateDate;
// variant = depends on culture
var culture = context.Options.GetCulture();
var pubDate = source.GetPublishDate(culture);
return pubDate.HasValue ? pubDate.Value : source.UpdateDate;
}
}
// if there's no culture here, the issue is somewhere else (UI, whatever) - throw!
if (culture == null)
throw new InvalidOperationException("Missing culture in mapping options.");
/// <summary>
/// Resolves the published flag for a content item/content variant
/// </summary>
private class PublishedResolver : IValueResolver<IContent, ContentItemBasic<ContentPropertyBasic>, bool>
{
public bool Resolve(IContent source, ContentItemBasic<ContentPropertyBasic> destination, bool destMember, ResolutionContext context)
{
var culture = context.GetCulture();
//a culture needs to be in the context for a variant content item
if (culture == null || source.ContentType.VariesByCulture() == false)
return source.Published;
return source.IsCulturePublished(culture);
// if we don't have a date for a culture, it means the culture is not available, and
// hey we should probably not be mapping it, but it's too late, return a fallback date
var date = source.GetCultureDate(culture);
return date ?? source.UpdateDate;
}
}
@@ -131,25 +120,20 @@ namespace Umbraco.Web.Models.Mapping
{
public string Resolve(IContent source, ContentItemBasic<ContentPropertyBasic> destination, string destMember, ResolutionContext context)
{
var culture = context.GetCulture();
// invariant = only 1 name
if (!source.ContentType.VariesByCulture()) return source.Name;
//a culture needs to be in the context for a variant content item
if (culture == null || source.ContentType.VariesByCulture() == false)
return source.Name;
// variant = depends on culture
var culture = context.Options.GetCulture();
if (source.CultureNames.TryGetValue(culture, out var name) && !string.IsNullOrWhiteSpace(name))
{
return name;
}
else
{
return $"({ source.Name })";
}
// if there's no culture here, the issue is somewhere else (UI, whatever) - throw!
if (culture == null)
throw new InvalidOperationException("Missing culture in mapping options.");
// if we don't have a name for a culture, it means the culture is not available, and
// hey we should probably not be mapping it, but it's too late, return a fallback name
return source.CultureNames.TryGetValue(culture, out var name) && !name.IsNullOrWhiteSpace() ? name : $"(({source.Name}))";
}
}
}
}
}

View File

@@ -57,17 +57,12 @@ namespace Umbraco.Web.Models.Mapping
// if there's a set of property aliases specified, we will check if the current property's value should be mapped.
// if it isn't one of the ones specified in 'includeProperties', we will just return the result without mapping the Value.
if (context.Options.Items.ContainsKey("IncludeProperties"))
{
if (context.Options.Items["IncludeProperties"] is IEnumerable<string> includeProperties
&& includeProperties.Contains(property.Alias) == false)
{
return result;
}
}
var includedProperties = context.Options.GetIncludedProperties();
if (includedProperties != null && !includedProperties.Contains(property.Alias))
return result;
//Get the culture from the context which will be set during the mapping operation for each property
var culture = context.GetCulture();
var culture = context.Options.GetCulture();
//a culture needs to be in the context for a property type that can vary
if (culture == null && property.PropertyType.VariesByCulture())

View File

@@ -37,7 +37,7 @@ namespace Umbraco.Web.Models.Mapping
if (source.ContentType.VariesByCulture())
{
//Get the culture from the context which will be set during the mapping operation for each variant
var culture = context.GetCulture();
var culture = context.Options.GetCulture();
//a culture needs to be in the context for a variant content item
if (culture == null)

View File

@@ -0,0 +1,45 @@
using AutoMapper;
namespace Umbraco.Web.Models.Mapping
{
/// <summary>
/// Provides extension methods for AutoMapper's <see cref="IMappingOperationOptions"/>.
/// </summary>
internal static class MappingOperationOptionsExtensions
{
private const string CultureKey = "MappingOperationOptions.Culture";
private const string IncludedPropertiesKey = "MappingOperationOptions.IncludeProperties";
/// <summary>
/// Gets the context culture.
/// </summary>
public static string GetCulture(this IMappingOperationOptions options)
{
return options.Items.TryGetValue(CultureKey, out var obj) && obj is string s ? s : null;
}
/// <summary>
/// Sets a context culture.
/// </summary>
public static void SetCulture(this IMappingOperationOptions options, string culture)
{
options.Items[CultureKey] = culture;
}
/// <summary>
/// Get included properties.
/// </summary>
public static string[] GetIncludedProperties(this IMappingOperationOptions options)
{
return options.Items.TryGetValue(IncludedPropertiesKey, out var obj) && obj is string[] s ? s : null;
}
/// <summary>
/// Sets included properties.
/// </summary>
public static void SetIncludedProperties(this IMappingOperationOptions options, string[] properties)
{
options.Items[IncludedPropertiesKey] = properties;
}
}
}

View File

@@ -1,28 +0,0 @@
using AutoMapper;
namespace Umbraco.Web.Models.Mapping
{
/// <summary>
/// Extension methods for AutoMapper's <see cref="ResolutionContext"/>
/// </summary>
internal static class ResolutionContextExtensions
{
public const string CultureKey = "ContextMapper.Culture";
/// <summary>
/// Returns the language Id in the mapping context if one is found
/// </summary>
/// <param name="resolutionContext"></param>
/// <returns></returns>
public static string GetCulture(this ResolutionContext resolutionContext)
{
if (!resolutionContext.Options.Items.TryGetValue(CultureKey, out var obj)) return null;
if (obj is string s)
return s;
return null;
}
}
}

View File

@@ -142,6 +142,7 @@
<Compile Include="Media\Exif\Utility.cs" />
<Compile Include="Media\ImageHelper.cs" />
<Compile Include="Media\UploadAutoFillProperties.cs" />
<Compile Include="Models\Mapping\MappingOperationOptionsExtensions.cs" />
<Compile Include="Security\ActiveDirectoryBackOfficeUserPasswordChecker.cs" />
<Compile Include="Security\BackOfficeClaimsIdentityFactory.cs" />
<Compile Include="Security\BackOfficeUserManagerMarker.cs" />
@@ -302,7 +303,6 @@
<Compile Include="Models\Mapping\ContentAppResolverExtensions.cs" />
<Compile Include="Models\Mapping\ContentItemDisplayNameResolver.cs" />
<Compile Include="Models\Mapping\ContentTypeVariationsResolver.cs" />
<Compile Include="Models\Mapping\ResolutionContextExtensions.cs" />
<Compile Include="Models\Mapping\ContentChildOfListViewResolver.cs" />
<Compile Include="Models\Mapping\ContentUrlResolver.cs" />
<Compile Include="Models\Mapping\DefaultTemplateResolver.cs" />