-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8842 from umbraco/v8/bugfix/block-list-copying
Ensure new UDIs for blocks when copying
- Loading branch information
Showing
14 changed files
with
630 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/Umbraco.Core/PropertyEditors/ComplexPropertyEditorContentEventHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Umbraco.Core.Events; | ||
using Umbraco.Core.Models; | ||
using Umbraco.Core.Services; | ||
using Umbraco.Core.Services.Implement; | ||
|
||
namespace Umbraco.Core.PropertyEditors | ||
{ | ||
/// <summary> | ||
/// Utility class for dealing with <see cref="ContentService"/> Copying/Saving events for complex editors | ||
/// </summary> | ||
internal class ComplexPropertyEditorContentEventHandler : IDisposable | ||
{ | ||
private readonly string _editorAlias; | ||
private readonly Func<string, bool, string> _formatPropertyValue; | ||
private bool _disposedValue; | ||
|
||
public ComplexPropertyEditorContentEventHandler(string editorAlias, | ||
Func<string, bool, string> formatPropertyValue) | ||
{ | ||
_editorAlias = editorAlias; | ||
_formatPropertyValue = formatPropertyValue; | ||
ContentService.Copying += ContentService_Copying; | ||
ContentService.Saving += ContentService_Saving; | ||
} | ||
|
||
/// <summary> | ||
/// <see cref="ContentService"/> Copying event handler | ||
/// </summary> | ||
/// <param name="sender"></param> | ||
/// <param name="e"></param> | ||
private void ContentService_Copying(IContentService sender, CopyEventArgs<IContent> e) | ||
{ | ||
var props = e.Copy.GetPropertiesByEditor(_editorAlias); | ||
UpdatePropertyValues(props, false); | ||
} | ||
|
||
/// <summary> | ||
/// <see cref="ContentService"/> Saving event handler | ||
/// </summary> | ||
/// <param name="sender"></param> | ||
/// <param name="e"></param> | ||
private void ContentService_Saving(IContentService sender, ContentSavingEventArgs e) | ||
{ | ||
foreach (var entity in e.SavedEntities) | ||
{ | ||
var props = entity.GetPropertiesByEditor(_editorAlias); | ||
UpdatePropertyValues(props, true); | ||
} | ||
} | ||
|
||
private void UpdatePropertyValues(IEnumerable<Property> props, bool onlyMissingKeys) | ||
{ | ||
foreach (var prop in props) | ||
{ | ||
// A Property may have one or more values due to cultures | ||
var propVals = prop.Values; | ||
foreach (var cultureVal in propVals) | ||
{ | ||
// Remove keys from published value & any nested properties | ||
var updatedPublishedVal = _formatPropertyValue(cultureVal.PublishedValue?.ToString(), onlyMissingKeys); | ||
cultureVal.PublishedValue = updatedPublishedVal; | ||
|
||
// Remove keys from edited/draft value & any nested properties | ||
var updatedEditedVal = _formatPropertyValue(cultureVal.EditedValue?.ToString(), onlyMissingKeys); | ||
cultureVal.EditedValue = updatedEditedVal; | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Unbinds from events | ||
/// </summary> | ||
/// <param name="disposing"></param> | ||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (!_disposedValue) | ||
{ | ||
if (disposing) | ||
{ | ||
ContentService.Copying -= ContentService_Copying; | ||
ContentService.Saving -= ContentService_Saving; | ||
} | ||
_disposedValue = true; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Unbinds from events | ||
/// </summary> | ||
public void Dispose() | ||
{ | ||
Dispose(disposing: true); | ||
GC.SuppressFinalize(this); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.