Files
Umbraco-CMS/src/Umbraco.Web/Models/Mapping/ContentPropertyBasicMapper.cs

75 lines
3.3 KiB
C#
Raw Normal View History

2018-06-29 19:52:40 +02:00
using System;
using System.Linq;
using Umbraco.Core;
using Umbraco.Core.Logging;
2019-03-26 10:39:50 +01:00
using Umbraco.Core.Mapping;
2018-06-29 19:52:40 +02:00
using Umbraco.Core.Models;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Services;
using Umbraco.Web.Models.ContentEditing;
namespace Umbraco.Web.Models.Mapping
{
/// <summary>
/// Creates a base generic ContentPropertyBasic from a Property
/// </summary>
2019-03-26 10:39:50 +01:00
internal class ContentPropertyBasicMapper<TDestination>
2018-06-29 19:52:40 +02:00
where TDestination : ContentPropertyBasic, new()
{
private readonly ILogger _logger;
private readonly PropertyEditorCollection _propertyEditors;
protected IDataTypeService DataTypeService { get; }
2019-03-26 10:39:50 +01:00
public ContentPropertyBasicMapper(IDataTypeService dataTypeService, ILogger logger, PropertyEditorCollection propertyEditors)
2018-06-29 19:52:40 +02:00
{
_logger = logger;
_propertyEditors = propertyEditors;
DataTypeService = dataTypeService;
}
/// <summary>
/// Assigns the PropertyEditor, Id, Alias and Value to the property
/// </summary>
/// <returns></returns>
2019-03-26 18:47:35 +01:00
public virtual void Map(Property property, TDestination dest, MapperContext context)
2018-06-29 19:52:40 +02:00
{
var editor = _propertyEditors[property.PropertyType.PropertyEditorAlias];
if (editor == null)
{
2019-03-26 10:39:50 +01:00
_logger.Error<ContentPropertyBasicMapper<TDestination>>(
new NullReferenceException("The property editor with alias " + property.PropertyType.PropertyEditorAlias + " does not exist"),
"No property editor '{PropertyEditorAlias}' found, converting to a Label",
property.PropertyType.PropertyEditorAlias);
2018-06-29 19:52:40 +02:00
editor = _propertyEditors[Constants.PropertyEditors.Aliases.Label];
2018-06-29 19:52:40 +02:00
}
2019-03-26 18:47:35 +01:00
dest.Id = property.Id;
dest.Alias = property.Alias;
dest.PropertyEditor = editor;
dest.Editor = editor.Alias;
2018-06-29 19:52:40 +02:00
// 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.
2019-03-26 10:39:50 +01:00
var includedProperties = context.GetIncludedProperties();
2018-09-17 13:19:24 +02:00
if (includedProperties != null && !includedProperties.Contains(property.Alias))
2019-03-26 18:47:35 +01:00
return;
//Get the culture from the context which will be set during the mapping operation for each property
2019-03-26 10:39:50 +01:00
var culture = context.GetCulture();
//a culture needs to be in the context for a property type that can vary
2018-06-20 14:18:57 +02:00
if (culture == null && property.PropertyType.VariesByCulture())
throw new InvalidOperationException($"No culture found in mapping operation when one is required for the culture variant property type {property.PropertyType.Alias}");
//set the culture to null if it's an invariant property type
2018-06-20 14:18:57 +02:00
culture = !property.PropertyType.VariesByCulture() ? null : culture;
2018-06-29 19:52:40 +02:00
2019-03-26 18:47:35 +01:00
dest.Culture = culture;
2018-06-29 19:52:40 +02:00
// if no 'IncludeProperties' were specified or this property is set to be included - we will map the value and return.
2019-03-26 18:47:35 +01:00
dest.Value = editor.GetValueEditor().ToEditor(property, DataTypeService, culture);
2018-06-29 19:52:40 +02:00
}
}
}