Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add in Umbraco.DropdownlistPublishingKeys migrator #302

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Newtonsoft.Json;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.PropertyEditors;

namespace uSync.Migrations.Migrators.Core;

[SyncMigrator("Umbraco.DropdownlistPublishingKeys")]
public class DropdownlistPublishingKeysMigrator : SyncPropertyMigratorBase
{
public override string GetDatabaseType(SyncMigrationDataTypeProperty dataTypeProperty, SyncMigrationContext context)
=> nameof(ValueStorageType.Nvarchar).ToLower();

public override string GetEditorAlias(SyncMigrationDataTypeProperty dataTypeProperty, SyncMigrationContext context)
=> UmbConstants.PropertyEditors.Aliases.DropDownListFlexible;

public override object? GetConfigValues(SyncMigrationDataTypeProperty dataTypeProperty,
SyncMigrationContext context)
{
var config = new DropDownFlexibleConfiguration();

if (dataTypeProperty.PreValues is null)
return config;

int index = 0;

foreach (var preValue in dataTypeProperty.PreValues)
{
config.Items.Add(new ValueListConfiguration.ValueListItem
{
Id = index++,
Value = preValue.Value,
});
}

context.Migrators.AddCustomValues(
$"dataType_{dataTypeProperty.DataTypeAlias}_items",
config.Items.ToDictionary(x => x.Id.ToString(), x => (object)x.Value!));

return config;
}

public override string? GetContentValue(SyncMigrationContentProperty contentProperty, SyncMigrationContext context)
{
if (string.IsNullOrEmpty(contentProperty.Value))
return "[]";

var dataTypeAlias = context.ContentTypes
.GetDataTypeAlias(contentProperty.ContentTypeAlias, contentProperty.PropertyAlias);

var items = context.Migrators.GetCustomValues($"dataType_{dataTypeAlias}_items");

if (items.TryGetValue(contentProperty.Value, out var value) && value is string stringValue)
return JsonConvert.SerializeObject(new[] { stringValue });

return "[]";
}
}
Loading