2021-03-01 17:05:59 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
|
|
|
|
using Umbraco.Cms.Core.Events;
|
|
|
|
|
using Umbraco.Cms.Core.Models;
|
2021-05-11 14:33:49 +02:00
|
|
|
using Umbraco.Cms.Core.Notifications;
|
2021-03-01 17:05:59 +01:00
|
|
|
using Umbraco.Extensions;
|
|
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
namespace Umbraco.Cms.Core.PropertyEditors;
|
|
|
|
|
|
2024-01-10 12:22:36 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Handles nested Udi keys when
|
|
|
|
|
/// - saving: Empty keys get generated
|
|
|
|
|
/// - copy: keys get replaced by new ones while keeping references intact
|
|
|
|
|
/// - scaffolding: keys get replaced by new ones while keeping references intact
|
|
|
|
|
/// </summary>
|
2022-06-02 08:18:31 +02:00
|
|
|
public abstract class ComplexPropertyEditorContentNotificationHandler :
|
|
|
|
|
INotificationHandler<ContentSavingNotification>,
|
2024-01-10 12:22:36 +01:00
|
|
|
INotificationHandler<ContentCopyingNotification>,
|
|
|
|
|
INotificationHandler<ContentScaffoldedNotification>
|
2021-03-01 17:05:59 +01:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
protected abstract string EditorAlias { get; }
|
2021-03-01 17:05:59 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
public void Handle(ContentCopyingNotification notification)
|
|
|
|
|
{
|
|
|
|
|
IEnumerable<IProperty> props = notification.Copy.GetPropertiesByEditor(EditorAlias);
|
|
|
|
|
UpdatePropertyValues(props, false);
|
|
|
|
|
}
|
2021-03-01 17:05:59 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
public void Handle(ContentSavingNotification notification)
|
|
|
|
|
{
|
|
|
|
|
foreach (IContent entity in notification.SavedEntities)
|
2021-03-01 17:05:59 +01:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
IEnumerable<IProperty> props = entity.GetPropertiesByEditor(EditorAlias);
|
|
|
|
|
UpdatePropertyValues(props, true);
|
2021-03-01 17:05:59 +01:00
|
|
|
}
|
2022-06-02 08:18:31 +02:00
|
|
|
}
|
2021-03-01 17:05:59 +01:00
|
|
|
|
2024-01-10 12:22:36 +01:00
|
|
|
public void Handle(ContentScaffoldedNotification notification)
|
|
|
|
|
{
|
|
|
|
|
IEnumerable<IProperty> props = notification.Scaffold.GetPropertiesByEditor(EditorAlias);
|
|
|
|
|
UpdatePropertyValues(props, false);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
protected abstract string FormatPropertyValue(string rawJson, bool onlyMissingKeys);
|
2021-03-01 17:05:59 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
private void UpdatePropertyValues(IEnumerable<IProperty> props, bool onlyMissingKeys)
|
|
|
|
|
{
|
|
|
|
|
foreach (IProperty prop in props)
|
2021-03-01 17:05:59 +01:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
// A Property may have one or more values due to cultures
|
|
|
|
|
IReadOnlyCollection<IPropertyValue> propVals = prop.Values;
|
|
|
|
|
foreach (IPropertyValue cultureVal in propVals)
|
2021-03-01 17:05:59 +01:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
// Remove keys from published value & any nested properties
|
2024-03-01 12:51:21 +01:00
|
|
|
var publishedValue = cultureVal.PublishedValue?.ToString();
|
2022-06-02 08:18:31 +02:00
|
|
|
var updatedPublishedVal =
|
|
|
|
|
FormatPropertyValue(publishedValue!, onlyMissingKeys).NullOrWhiteSpaceAsNull();
|
|
|
|
|
cultureVal.PublishedValue = updatedPublishedVal;
|
|
|
|
|
|
|
|
|
|
// Remove keys from edited/draft value & any nested properties
|
2024-03-01 12:51:21 +01:00
|
|
|
var editedValue = cultureVal.EditedValue?.ToString();
|
2022-06-02 08:18:31 +02:00
|
|
|
var updatedEditedVal = FormatPropertyValue(editedValue!, onlyMissingKeys).NullOrWhiteSpaceAsNull();
|
|
|
|
|
cultureVal.EditedValue = updatedEditedVal;
|
2021-03-01 17:05:59 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|