Skip to content

Commit bb4e327

Browse files
Replace property value artifact migrators with prevalue artifact and property type migrators
1 parent 50e0f12 commit bb4e327

7 files changed

+193
-4
lines changed

src/Umbraco.Deploy.Contrib/Extensions/ArtifactMigratorCollectionBuilderExtensions.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ public static ArtifactMigratorCollectionBuilder AddLegacyMigrators(this Artifact
4444
.Append<TextboxDataTypeArtifactMigrator>()
4545
.Append<TextboxMultipleDataTypeArtifactMigrator>()
4646
.Append<TinyMCEv3DataTypeArtifactMigrator>()
47-
// Property values
48-
.Append<CheckBoxListPropertyValueArtifactMigrator>()
49-
.Append<DropDownListFlexiblePropertyValueArtifactMigrator>()
50-
.Append<RadioButtonListPropertyValueArtifactMigrator>();
47+
// Add prefixes to pre-value property editor aliases, triggering property type migrators
48+
.Append<PrevalueArtifactMigrator>();
5149
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Umbraco.Deploy.Contrib.Migrators.Legacy;
2+
using Umbraco.Deploy.Core.Migrators;
3+
4+
namespace Umbraco.Extensions;
5+
6+
public static class PropertyTypeMigratorCollectionBuilderExtensions
7+
{
8+
/// <summary>
9+
/// Adds the legacy property type migrators to allow importing from Umbraco 7.
10+
/// </summary>
11+
/// <returns>
12+
/// The property type migrator collection builder.
13+
/// </returns>
14+
public static PropertyTypeMigratorCollectionBuilder AddLegacyMigrators(this PropertyTypeMigratorCollectionBuilder propertyTypeMigratorCollectionBuilder)
15+
=> propertyTypeMigratorCollectionBuilder
16+
// Pre-values to a single value or JSON array
17+
.Append<CheckBoxListPropertyTypeMigrator>()
18+
.Append<DropDownListFlexiblePropertyTypeMigrator>()
19+
.Append<RadioButtonListPropertyTypeMigrator>();
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Umbraco.Cms.Core;
2+
using Umbraco.Cms.Core.Serialization;
3+
4+
namespace Umbraco.Deploy.Contrib.Migrators.Legacy;
5+
6+
/// <summary>
7+
/// Migrates the property value using the <see cref="Constants.PropertyEditors.Aliases.CheckBoxList" /> editor containing prevalues (seperated by <see cref="PrevaluePropertyTypeMigratorBase.Delimiter" />) from Umbraco 7 to a JSON array.
8+
/// </summary>
9+
public sealed class CheckBoxListPropertyTypeMigrator : PrevaluePropertyTypeMigratorBase
10+
{
11+
/// <inheritdoc />
12+
protected override bool Multiple => true;
13+
14+
/// <summary>
15+
/// Initializes a new instance of the <see cref="CheckBoxListPropertyTypeMigrator" /> class.
16+
/// </summary>
17+
/// <param name="jsonSerializer">The JSON serializer.</param>
18+
public CheckBoxListPropertyTypeMigrator(IJsonSerializer jsonSerializer)
19+
: base(Constants.PropertyEditors.Aliases.CheckBoxList, jsonSerializer)
20+
{ }
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Umbraco.Cms.Core;
2+
using Umbraco.Cms.Core.Serialization;
3+
4+
namespace Umbraco.Deploy.Contrib.Migrators.Legacy;
5+
6+
/// <summary>
7+
/// Migrates the property value using the <see cref="Constants.PropertyEditors.Aliases.DropDownListFlexible" /> editor containing prevalues (seperated by <see cref="PrevaluePropertyTypeMigratorBase.Delimiter" />) from Umbraco 7 to a JSON array.
8+
/// </summary>
9+
public sealed class DropDownListFlexiblePropertyTypeMigrator : PrevaluePropertyTypeMigratorBase
10+
{
11+
/// <inheritdoc />
12+
protected override bool Multiple => true;
13+
14+
/// <summary>
15+
/// Initializes a new instance of the <see cref="DropDownListFlexiblePropertyTypeMigrator" /> class.
16+
/// </summary>
17+
/// <param name="jsonSerializer">The JSON serializer.</param>
18+
public DropDownListFlexiblePropertyTypeMigrator(IJsonSerializer jsonSerializer)
19+
: base(Constants.PropertyEditors.Aliases.DropDownListFlexible, jsonSerializer)
20+
{ }
21+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Linq;
3+
using Umbraco.Cms.Core;
4+
using Umbraco.Cms.Core.Semver;
5+
using Umbraco.Deploy.Core.Migrators;
6+
using Umbraco.Deploy.Infrastructure.Artifacts.Content;
7+
using Umbraco.Extensions;
8+
9+
namespace Umbraco.Deploy.Contrib.Migrators.Legacy;
10+
11+
/// <summary>
12+
/// Migrates the <see cref="ContentArtifactBase" /> property editor aliases to add a prefix to Umbraco 7 prevalue editors, triggering property type migrators.
13+
/// </summary>
14+
public class PrevalueArtifactMigrator : ArtifactMigratorBase<ContentArtifactBase>
15+
{
16+
internal const string EditorAliasPrefix = "MigratePrevalue.";
17+
18+
private readonly string[] _editorAliases;
19+
20+
/// <summary>
21+
/// Initializes a new instance of the <see cref="PrevalueArtifactMigrator" /> class.
22+
/// </summary>
23+
public PrevalueArtifactMigrator()
24+
: this(
25+
Constants.PropertyEditors.Aliases.CheckBoxList,
26+
Constants.PropertyEditors.Aliases.DropDownListFlexible,
27+
Constants.PropertyEditors.Aliases.RadioButtonList)
28+
{ }
29+
30+
/// <summary>
31+
/// Initializes a new instance of the <see cref="PrevalueArtifactMigrator" /> class.
32+
/// </summary>
33+
/// <param name="editorAliases">The editor aliases.</param>
34+
protected PrevalueArtifactMigrator(params string[] editorAliases)
35+
{
36+
_editorAliases = editorAliases;
37+
38+
MaxVersion = new SemVersion(3, 0, 0);
39+
}
40+
41+
/// <inheritdoc />
42+
protected override bool CanMigrateType(Type type)
43+
=> type.Inherits<ContentArtifactBase>();
44+
45+
/// <inheritdoc />
46+
protected override ContentArtifactBase? Migrate(ContentArtifactBase artifact)
47+
{
48+
// Add prefix to matching property editor aliases
49+
artifact.PropertyEditorAliases = artifact.PropertyEditorAliases?.ToDictionary(x => x.Key, x => _editorAliases.Contains(x.Value) ? EditorAliasPrefix + x.Value : x.Value);
50+
51+
return artifact;
52+
}
53+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Umbraco.Cms.Core.Deploy;
4+
using Umbraco.Cms.Core.Models;
5+
using Umbraco.Cms.Core.Serialization;
6+
using Umbraco.Deploy.Core.Migrators;
7+
8+
namespace Umbraco.Deploy.Contrib.Migrators.Legacy;
9+
10+
/// <summary>
11+
/// Migrates the property value containing prevalues (seperated by <see cref="Delimiter" />) from Umbraco 7 to a single value or JSON array.
12+
/// </summary>
13+
public abstract class PrevaluePropertyTypeMigratorBase : PropertyTypeMigratorBase
14+
{
15+
private const string EditorAliasPrefix = PrevalueArtifactMigrator.EditorAliasPrefix;
16+
private const string Delimiter = ";;";
17+
18+
private readonly IJsonSerializer _jsonSerializer;
19+
20+
/// <summary>
21+
/// Gets a value indicating whether the property type stores multiple prevalues as a JSON array or single value.
22+
/// </summary>
23+
/// <value>
24+
/// <c>true</c> if multiple prevalues are stored as a JSON array; otherwise, <c>false</c>.
25+
/// </value>
26+
protected abstract bool Multiple { get; }
27+
28+
/// <summary>
29+
/// Initializes a new instance of the <see cref="PrevaluePropertyTypeMigratorBase" /> class.
30+
/// </summary>
31+
/// <param name="editorAlias">The editor alias.</param>
32+
/// <param name="jsonSerializer">The JSON serializer.</param>
33+
protected PrevaluePropertyTypeMigratorBase(string editorAlias, IJsonSerializer jsonSerializer)
34+
: base(EditorAliasPrefix + editorAlias, editorAlias)
35+
=> _jsonSerializer = jsonSerializer;
36+
37+
/// <inheritdoc />
38+
public override object? Migrate(IPropertyType propertyType, object? value, IDictionary<string, string> propertyEditorAliases, IContextCache contextCache)
39+
{
40+
if (value is not string stringValue)
41+
{
42+
return null;
43+
}
44+
45+
var values = stringValue.Split(new[] { Delimiter }, StringSplitOptions.RemoveEmptyEntries);
46+
if (values.Length == 0)
47+
{
48+
return null;
49+
}
50+
51+
return Multiple
52+
? _jsonSerializer.Serialize(values)
53+
: values[0];
54+
}
55+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Umbraco.Cms.Core;
2+
using Umbraco.Cms.Core.Serialization;
3+
4+
namespace Umbraco.Deploy.Contrib.Migrators.Legacy;
5+
6+
/// <summary>
7+
/// Migrates the property value using the <see cref="Constants.PropertyEditors.Aliases.RadioButtonList" /> editor containing prevalues (seperated by <see cref="PrevaluePropertyTypeMigratorBase.Delimiter" />) from Umbraco 7 to a single value.
8+
/// </summary>
9+
public sealed class RadioButtonListPropertyTypeMigrator : PrevaluePropertyTypeMigratorBase
10+
{
11+
/// <inheritdoc />
12+
protected override bool Multiple => false;
13+
14+
/// <summary>
15+
/// Initializes a new instance of the <see cref="RadioButtonListPropertyTypeMigrator" /> class.
16+
/// </summary>
17+
/// <param name="jsonSerializer">The JSON serializer.</param>
18+
public RadioButtonListPropertyTypeMigrator(IJsonSerializer jsonSerializer)
19+
: base(Constants.PropertyEditors.Aliases.RadioButtonList, jsonSerializer)
20+
{ }
21+
}

0 commit comments

Comments
 (0)