Files
Umbraco-CMS/src/Umbraco.Core/Models/Mapping/ContentPropertyDisplayMapper.cs

76 lines
3.6 KiB
C#
Raw Normal View History

// Copyright (c) Umbraco.
// See LICENSE for more details.
2018-06-29 19:52:40 +02:00
using Microsoft.Extensions.Logging;
using Umbraco.Cms.Core.Dictionary;
using Umbraco.Cms.Core.Mapping;
using Umbraco.Cms.Core.Models.ContentEditing;
using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Cms.Core.Services;
using Umbraco.Extensions;
namespace Umbraco.Cms.Core.Models.Mapping
2018-06-29 19:52:40 +02:00
{
/// <summary>
/// Creates a ContentPropertyDisplay from a Property
/// </summary>
2019-03-26 10:39:50 +01:00
internal class ContentPropertyDisplayMapper : ContentPropertyBasicMapper<ContentPropertyDisplay>
2018-06-29 19:52:40 +02:00
{
private readonly ICultureDictionary _cultureDictionary;
2018-06-29 19:52:40 +02:00
private readonly ILocalizedTextService _textService;
public ContentPropertyDisplayMapper(ICultureDictionary cultureDictionary, IDataTypeService dataTypeService, IEntityService entityService, ILocalizedTextService textService, ILogger<ContentPropertyDisplayMapper> logger, PropertyEditorCollection propertyEditors)
: base(dataTypeService, entityService, logger, propertyEditors)
2018-06-29 19:52:40 +02:00
{
_cultureDictionary = cultureDictionary;
2018-06-29 19:52:40 +02:00
_textService = textService;
}
public override void Map(IProperty originalProp, ContentPropertyDisplay dest, MapperContext context)
2018-06-29 19:52:40 +02:00
{
2019-03-26 18:47:35 +01:00
base.Map(originalProp, dest, context);
2018-06-29 19:52:40 +02:00
2022-02-28 13:14:02 +01:00
var config = originalProp.PropertyType is null ? null : DataTypeService.GetDataType(originalProp.PropertyType.DataTypeId)?.Configuration;
2018-06-29 19:52:40 +02:00
// TODO: IDataValueEditor configuration - general issue
2018-06-29 19:52:40 +02:00
// GetValueEditor() returns a non-configured IDataValueEditor
// - for richtext and nested, configuration determines HideLabel, so we need to configure the value editor
// - could configuration also determines ValueType, everywhere?
// - does it make any sense to use a IDataValueEditor without configuring it?
// configure the editor for display with configuration
2022-01-21 11:43:58 +01:00
var valEditor = dest.PropertyEditor?.GetValueEditor(config);
2018-06-29 19:52:40 +02:00
//set the display properties after mapping
2019-03-26 18:47:35 +01:00
dest.Alias = originalProp.Alias;
2022-02-09 13:24:35 +01:00
dest.Description = originalProp.PropertyType?.Description;
dest.Label = originalProp.PropertyType?.Name;
2022-01-21 11:43:58 +01:00
dest.HideLabel = valEditor?.HideLabel ?? false;
2022-02-09 13:24:35 +01:00
dest.LabelOnTop = originalProp.PropertyType?.LabelOnTop;
2018-06-29 19:52:40 +02:00
//add the validation information
2022-02-21 12:49:17 +01:00
dest.Validation.Mandatory = originalProp.PropertyType?.Mandatory ?? false;
2022-02-09 13:24:35 +01:00
dest.Validation.MandatoryMessage = originalProp.PropertyType?.MandatoryMessage;
dest.Validation.Pattern = originalProp.PropertyType?.ValidationRegExp;
dest.Validation.PatternMessage = originalProp.PropertyType?.ValidationRegExpMessage;
2018-06-29 19:52:40 +02:00
2019-03-26 18:47:35 +01:00
if (dest.PropertyEditor == null)
2018-06-29 19:52:40 +02:00
{
//display.Config = PreValueCollection.AsDictionary(preVals);
//if there is no property editor it means that it is a legacy data type
// we cannot support editing with that so we'll just render the readonly value view.
2019-03-26 18:47:35 +01:00
dest.View = "views/propertyeditors/readonlyvalue/readonlyvalue.html";
2018-06-29 19:52:40 +02:00
}
else
{
//let the property editor format the pre-values
2019-03-26 18:47:35 +01:00
dest.Config = dest.PropertyEditor.GetConfigurationEditor().ToValueEditor(config);
2022-01-21 11:43:58 +01:00
dest.View = valEditor?.View;
2018-06-29 19:52:40 +02:00
}
//Translate
dest.Label = _textService.UmbracoDictionaryTranslate(_cultureDictionary, dest.Label);
dest.Description = _textService.UmbracoDictionaryTranslate(_cultureDictionary, dest.Description);
2018-06-29 19:52:40 +02:00
}
}
}