Files
Umbraco-CMS/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/NestedContentSingleValueConverter.cs
Nikolaj Geisle 70a34105e0 V11: obsolete legacy data types (#13953)
* Obsolete the grid

* Obsolete the legacy media picker

* Obsolete nested content

---------

Co-authored-by: Zeegaan <nge@umbraco.dk>
2023-03-13 11:33:27 +01:00

79 lines
2.9 KiB
C#

// Copyright (c) Umbraco.
// See LICENSE for more details.
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Umbraco.Cms.Core.Logging;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.PublishedCache;
namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters;
/// <inheritdoc />
/// <summary>
/// Provides an implementation for <see cref="T:Umbraco.Core.PropertyEditors.IPropertyValueConverter" /> for nested
/// content.
/// </summary>
[DefaultPropertyValueConverter(typeof(JsonValueConverter))]
[Obsolete("Nested content is obsolete, will be removed in V13")]
public class NestedContentSingleValueConverter : NestedContentValueConverterBase
{
private readonly IProfilingLogger _proflog;
/// <summary>
/// Initializes a new instance of the <see cref="NestedContentSingleValueConverter" /> class.
/// </summary>
public NestedContentSingleValueConverter(
IPublishedSnapshotAccessor publishedSnapshotAccessor,
IPublishedModelFactory publishedModelFactory,
IProfilingLogger proflog)
: base(publishedSnapshotAccessor, publishedModelFactory)
=> _proflog = proflog;
/// <inheritdoc />
public override bool IsConverter(IPublishedPropertyType propertyType)
=> IsNestedSingle(propertyType);
/// <inheritdoc />
public override Type GetPropertyValueType(IPublishedPropertyType propertyType)
{
NestedContentConfiguration.ContentType[]? contentTypes =
propertyType.DataType.ConfigurationAs<NestedContentConfiguration>()?.ContentTypes;
return contentTypes?.Length == 1
? ModelType.For(contentTypes[0].Alias)
: typeof(IPublishedElement);
}
/// <inheritdoc />
public override PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType propertyType)
=> PropertyCacheLevel.Element;
/// <inheritdoc />
public override object? ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object? source, bool preview)
=> source?.ToString();
/// <inheritdoc />
public override object? ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object? inter, bool preview)
{
using (_proflog.DebugDuration<NestedContentSingleValueConverter>(
$"ConvertPropertyToNestedContent ({propertyType.DataType.Id})"))
{
var value = (string?)inter;
if (string.IsNullOrWhiteSpace(value))
{
return null;
}
List<JObject>? objects = JsonConvert.DeserializeObject<List<JObject>>(value)!;
if (objects.Count == 0)
{
return null;
}
// Only return the first (existing data might contain more than is currently configured)
return ConvertToElement(objects[0], referenceCacheLevel, preview);
}
}
}